Sample code for 30+ languages & platforms
Swift

Shippo Create Shipment Object

See more Shippo Examples

Create a Shipment object to retrieve rates. It represents a request to ship a given package from the sender to the recipient address.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!

    // Implements the following CURL command:

    // curl https://api.goshippo.com/shipments/  \
    //     -H "Authorization: ShippoToken <API_TOKEN>" \
    //     -H "Content-Type: application/json"  \
    //     -d '{
    //        "address_from":{
    //           "name":"Mr. Hippo",
    //           "street1":"215 Clayton St.",
    //           "city":"San Francisco",
    //           "state":"CA",
    //           "zip":"94117",
    //           "country":"US"
    //        },
    //        "address_to":{
    //           "name":"Mrs. Hippo",
    //           "street1":"965 Mission St.",
    //           "city":"San Francisco",
    //           "state":"CA",
    //           "zip":"94105",
    //           "country":"US"
    //        },
    //        "parcels":[{
    //           "length":"5",
    //           "width":"5",
    //           "height":"5",
    //           "distance_unit":"in",
    //           "weight":"2",
    //           "mass_unit":"lb"
    //        }],
    //        "async": false
    //     }'

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "address_from": {
    //     "name": "Mr. Hippo",
    //     "street1": "215 Clayton St.",
    //     "city": "San Francisco",
    //     "state": "CA",
    //     "zip": "94117",
    //     "country": "US"
    //   },
    //   "address_to": {
    //     "name": "Mrs. Hippo",
    //     "street1": "965 Mission St.",
    //     "city": "San Francisco",
    //     "state": "CA",
    //     "zip": "94105",
    //     "country": "US"
    //   },
    //   "parcels": [
    //     {
    //       "length": "5",
    //       "width": "5",
    //       "height": "5",
    //       "distance_unit": "in",
    //       "weight": "2",
    //       "mass_unit": "lb"
    //     }
    //   ],
    //   "async": false
    // }

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "address_from.name", value: "Mr. Hippo")
    json.updateString(jsonPath: "address_from.street1", value: "215 Clayton St.")
    json.updateString(jsonPath: "address_from.city", value: "San Francisco")
    json.updateString(jsonPath: "address_from.state", value: "CA")
    json.updateString(jsonPath: "address_from.zip", value: "94117")
    json.updateString(jsonPath: "address_from.country", value: "US")
    json.updateString(jsonPath: "address_to.name", value: "Mrs. Hippo")
    json.updateString(jsonPath: "address_to.street1", value: "965 Mission St.")
    json.updateString(jsonPath: "address_to.city", value: "San Francisco")
    json.updateString(jsonPath: "address_to.state", value: "CA")
    json.updateString(jsonPath: "address_to.zip", value: "94105")
    json.updateString(jsonPath: "address_to.country", value: "US")
    json.updateString(jsonPath: "parcels[0].length", value: "5")
    json.updateString(jsonPath: "parcels[0].width", value: "5")
    json.updateString(jsonPath: "parcels[0].height", value: "5")
    json.updateString(jsonPath: "parcels[0].distance_unit", value: "in")
    json.updateString(jsonPath: "parcels[0].weight", value: "2")
    json.updateString(jsonPath: "parcels[0].mass_unit", value: "lb")
    json.updateBool(jsonPath: "async", value: false)

    http.setRequestHeader(name: "Authorization", value: "ShippoToken <API_TOKEN>")
    http.setRequestHeader(name: "Content-Type", value: "application/json")

    let resp = CkoHttpResponse()!
    success = http.httpJson(verb: "POST", url: "https://api.goshippo.com/shipments/", json: json, contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    let sbResponseBody = CkoStringBuilder()!
    resp.getBodySb(sb: sbResponseBody)
    let jResp = CkoJsonObject()!
    jResp.loadSb(sb: sbResponseBody)
    jResp.emitCompact = false

    print("Response Body:")
    print("\(jResp.emit()!)")

    var respStatusCode: Int = resp.statusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp.header!)")
        print("Failed.")
        return
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "carrier_accounts": [
    //   ],
    //   "object_created": "2019-06-28T17:45:20.441Z",
    //   "object_updated": "2019-06-28T17:45:20.511Z",
    //   "object_id": "17b01795a4884d8a9002ccc17c9d09d1",
    //   "object_owner": "admin@chilkatsoft.com",
    //   "status": "SUCCESS",
    //   "address_from": {
    //     "object_id": "427489906b604f498f7bd2429ab7d9a1",
    //     "is_complete": true,
    //     "name": "Mr. Hippo",
    //     "company": "",
    //     "street_no": "",
    //     "street1": "215 Clayton St.",
    //     "validation_results": {},
    //     "street2": "",
    //     "street3": "",
    //     "city": "San Francisco",
    //     "state": "CA",
    //     "zip": "94117",
    //     "country": "US",
    //     "phone": "",
    //     "email": "",
    //     "is_residential": null,
    //     "test": true
    //   },
    //   "address_to": {
    //     "object_id": "3d7166269a2844b7842ef9ace942ec7c",
    //     "is_complete": true,
    //     "name": "Mrs. Hippo",
    //     "company": "",
    //     "street_no": "",
    //     "street1": "965 Mission St.",
    //     "validation_results": {},
    //     "street2": "",
    //     "street3": "",
    //     "city": "San Francisco",
    //     "state": "CA",
    //     "zip": "94105",
    //     "country": "US",
    //     "phone": "",
    //     "email": "",
    //     "is_residential": null,
    //     "test": true
    //   },
    //   "parcels": [
    //     {
    //       "object_state": "VALID",
    //       "object_created": "2019-06-28T17:45:20.411Z",
    //       "object_updated": "2019-06-28T17:45:20.455Z",
    //       "object_id": "5af766ff15684a4186b0e3c833348fac",
    //       "object_owner": "admin@chilkatsoft.com",
    //       "template": null,
    //       "extra": {},
    //       "length": "5.0000",
    //       "width": "5.0000",
    //       "height": "5.0000",
    //       "distance_unit": "in",
    //       "weight": "2.0000",
    //       "mass_unit": "lb",
    //       "value_amount": null,
    //       "value_currency": null,
    //       "metadata": "",
    //       "line_items": [
    //       ],
    //       "test": true
    //     }
    //   ],
    //   "shipment_date": "2019-06-28T17:45:20.511Z",
    //   "address_return": {
    //     "object_id": "427489906b604f498f7bd2429ab7d9a1",
    //     "is_complete": true,
    //     "name": "Mr. Hippo",
    //     "company": "",
    //     "street_no": "",
    //     "street1": "215 Clayton St.",
    //     "validation_results": {},
    //     "street2": "",
    //     "street3": "",
    //     "city": "San Francisco",
    //     "state": "CA",
    //     "zip": "94117",
    //     "country": "US",
    //     "phone": "",
    //     "email": "",
    //     "is_residential": null,
    //     "test": true
    //   },
    //   "alternate_address_to": null,
    //   "customs_declaration": null,
    //   "extra": {},
    //   "rates": [
    //     {
    //       "object_created": "2019-06-28T17:45:20.789Z",
    //       "object_id": "dd7556c284e8444294d2ab7099e662d2",
    //       "object_owner": "admin@chilkatsoft.com",
    //       "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
    //       "attributes": [
    //         "FASTEST"
    //       ],
    //       "amount": "22.78",
    //       "currency": "USD",
    //       "amount_local": "22.78",
    //       "currency_local": "USD",
    //       "provider": "USPS",
    //       "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
    //       "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
    //       "servicelevel": {
    //         "name": "Priority Mail Express",
    //         "token": "usps_priority_express",
    //         "terms": ""
    //       },
    //       "estimated_days": 2,
    //       "arrives_by": null,
    //       "duration_terms": "Overnight delivery to most U.S. locations.",
    //       "messages": [
    //       ],
    //       "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
    //       "test": true,
    //       "zone": "1"
    //     },
    //     {
    //       "object_created": "2019-06-28T17:45:20.786Z",
    //       "object_id": "ec11cc297e2f4583986097f6d409e5c6",
    //       "object_owner": "admin@chilkatsoft.com",
    //       "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
    //       "attributes": [
    //         "BESTVALUE",
    //         "CHEAPEST",
    //         "FASTEST"
    //       ],
    //       "amount": "6.95",
    //       "currency": "USD",
    //       "amount_local": "6.95",
    //       "currency_local": "USD",
    //       "provider": "USPS",
    //       "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
    //       "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
    //       "servicelevel": {
    //         "name": "Priority Mail",
    //         "token": "usps_priority",
    //         "terms": ""
    //       },
    //       "estimated_days": 2,
    //       "arrives_by": null,
    //       "duration_terms": "Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.",
    //       "messages": [
    //       ],
    //       "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
    //       "test": true,
    //       "zone": "1"
    //     },
    //     {
    //       "object_created": "2019-06-28T17:45:20.785Z",
    //       "object_id": "d256586f539f4c93a2f02cc11f4942ac",
    //       "object_owner": "admin@chilkatsoft.com",
    //       "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
    //       "attributes": [
    //       ],
    //       "amount": "7.32",
    //       "currency": "USD",
    //       "amount_local": "7.32",
    //       "currency_local": "USD",
    //       "provider": "USPS",
    //       "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
    //       "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
    //       "servicelevel": {
    //         "name": "Parcel Select",
    //         "token": "usps_parcel_select",
    //         "terms": ""
    //       },
    //       "estimated_days": 7,
    //       "arrives_by": null,
    //       "duration_terms": "Delivery in 2 to 8 days.",
    //       "messages": [
    //       ],
    //       "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
    //       "test": true,
    //       "zone": "1"
    //     }
    //   ],
    //   "messages": [
    //     {
    //       "source": "DHLExpress",
    //       "code": "",
    //       "text": "Shippo's DHL Express master account doesn't support shipments to inside of the US"
    //     }
    //   ],
    //   "metadata": "",
    //   "test": true,
    //   "order": null
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var object_state: String?
    var template: String?
    var length: String?
    var width: String?
    var height: String?
    var distance_unit: String?
    var weight: String?
    var mass_unit: String?
    var value_amount: String?
    var value_currency: String?
    var j: Int
    var count_j: Int
    var shipment: String?
    var amount: String?
    var currency: String?
    var amount_local: String?
    var currency_local: String?
    var provider: String?
    var provider_image_75: String?
    var provider_image_200: String?
    var servicelevelName: String?
    var servicelevelToken: String?
    var servicelevelTerms: String?
    var estimated_days: Int
    var arrives_by: String?
    var duration_terms: String?
    var carrier_account: String?
    var zone: String?
    var strVal: String?
    var source: String?
    var code: String?
    var text: String?

    var object_created: String? = jResp.string(of: "object_created")
    var object_updated: String? = jResp.string(of: "object_updated")
    var object_id: String? = jResp.string(of: "object_id")
    var object_owner: String? = jResp.string(of: "object_owner")
    var status: String? = jResp.string(of: "status")
    var address_fromObject_id: String? = jResp.string(of: "address_from.object_id")
    var address_fromIs_complete: Bool = jResp.bool(of: "address_from.is_complete")
    var address_fromName: String? = jResp.string(of: "address_from.name")
    var address_fromCompany: String? = jResp.string(of: "address_from.company")
    var address_fromStreet_no: String? = jResp.string(of: "address_from.street_no")
    var address_fromStreet1: String? = jResp.string(of: "address_from.street1")
    var address_fromStreet2: String? = jResp.string(of: "address_from.street2")
    var address_fromStreet3: String? = jResp.string(of: "address_from.street3")
    var address_fromCity: String? = jResp.string(of: "address_from.city")
    var address_fromState: String? = jResp.string(of: "address_from.state")
    var address_fromZip: String? = jResp.string(of: "address_from.zip")
    var address_fromCountry: String? = jResp.string(of: "address_from.country")
    var address_fromPhone: String? = jResp.string(of: "address_from.phone")
    var address_fromEmail: String? = jResp.string(of: "address_from.email")
    var address_fromIs_residential: String? = jResp.string(of: "address_from.is_residential")
    var address_fromTest: Bool = jResp.bool(of: "address_from.test")
    var address_toObject_id: String? = jResp.string(of: "address_to.object_id")
    var address_toIs_complete: Bool = jResp.bool(of: "address_to.is_complete")
    var address_toName: String? = jResp.string(of: "address_to.name")
    var address_toCompany: String? = jResp.string(of: "address_to.company")
    var address_toStreet_no: String? = jResp.string(of: "address_to.street_no")
    var address_toStreet1: String? = jResp.string(of: "address_to.street1")
    var address_toStreet2: String? = jResp.string(of: "address_to.street2")
    var address_toStreet3: String? = jResp.string(of: "address_to.street3")
    var address_toCity: String? = jResp.string(of: "address_to.city")
    var address_toState: String? = jResp.string(of: "address_to.state")
    var address_toZip: String? = jResp.string(of: "address_to.zip")
    var address_toCountry: String? = jResp.string(of: "address_to.country")
    var address_toPhone: String? = jResp.string(of: "address_to.phone")
    var address_toEmail: String? = jResp.string(of: "address_to.email")
    var address_toIs_residential: String? = jResp.string(of: "address_to.is_residential")
    var address_toTest: Bool = jResp.bool(of: "address_to.test")
    var shipment_date: String? = jResp.string(of: "shipment_date")
    var address_returnObject_id: String? = jResp.string(of: "address_return.object_id")
    var address_returnIs_complete: Bool = jResp.bool(of: "address_return.is_complete")
    var address_returnName: String? = jResp.string(of: "address_return.name")
    var address_returnCompany: String? = jResp.string(of: "address_return.company")
    var address_returnStreet_no: String? = jResp.string(of: "address_return.street_no")
    var address_returnStreet1: String? = jResp.string(of: "address_return.street1")
    var address_returnStreet2: String? = jResp.string(of: "address_return.street2")
    var address_returnStreet3: String? = jResp.string(of: "address_return.street3")
    var address_returnCity: String? = jResp.string(of: "address_return.city")
    var address_returnState: String? = jResp.string(of: "address_return.state")
    var address_returnZip: String? = jResp.string(of: "address_return.zip")
    var address_returnCountry: String? = jResp.string(of: "address_return.country")
    var address_returnPhone: String? = jResp.string(of: "address_return.phone")
    var address_returnEmail: String? = jResp.string(of: "address_return.email")
    var address_returnIs_residential: String? = jResp.string(of: "address_return.is_residential")
    var address_returnTest: Bool = jResp.bool(of: "address_return.test")
    var alternate_address_to: String? = jResp.string(of: "alternate_address_to")
    var customs_declaration: String? = jResp.string(of: "customs_declaration")
    var metadata: String? = jResp.string(of: "metadata")
    var test: Bool = jResp.bool(of: "test")
    var order: String? = jResp.string(of: "order")
    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "carrier_accounts").intValue
    while i < count_i {
        jResp.i = i
        i = i + 1
    }

    i = 0
    count_i = jResp.size(ofArray: "parcels").intValue
    while i < count_i {
        jResp.i = i
        object_state = jResp.string(of: "parcels[i].object_state")
        object_created = jResp.string(of: "parcels[i].object_created")
        object_updated = jResp.string(of: "parcels[i].object_updated")
        object_id = jResp.string(of: "parcels[i].object_id")
        object_owner = jResp.string(of: "parcels[i].object_owner")
        template = jResp.string(of: "parcels[i].template")
        length = jResp.string(of: "parcels[i].length")
        width = jResp.string(of: "parcels[i].width")
        height = jResp.string(of: "parcels[i].height")
        distance_unit = jResp.string(of: "parcels[i].distance_unit")
        weight = jResp.string(of: "parcels[i].weight")
        mass_unit = jResp.string(of: "parcels[i].mass_unit")
        value_amount = jResp.string(of: "parcels[i].value_amount")
        value_currency = jResp.string(of: "parcels[i].value_currency")
        metadata = jResp.string(of: "parcels[i].metadata")
        test = jResp.bool(of: "parcels[i].test")
        j = 0
        count_j = jResp.size(ofArray: "parcels[i].line_items").intValue
        while j < count_j {
            jResp.j = j
            j = j + 1
        }

        i = i + 1
    }

    i = 0
    count_i = jResp.size(ofArray: "rates").intValue
    while i < count_i {
        jResp.i = i
        object_created = jResp.string(of: "rates[i].object_created")
        object_id = jResp.string(of: "rates[i].object_id")
        object_owner = jResp.string(of: "rates[i].object_owner")
        shipment = jResp.string(of: "rates[i].shipment")
        amount = jResp.string(of: "rates[i].amount")
        currency = jResp.string(of: "rates[i].currency")
        amount_local = jResp.string(of: "rates[i].amount_local")
        currency_local = jResp.string(of: "rates[i].currency_local")
        provider = jResp.string(of: "rates[i].provider")
        provider_image_75 = jResp.string(of: "rates[i].provider_image_75")
        provider_image_200 = jResp.string(of: "rates[i].provider_image_200")
        servicelevelName = jResp.string(of: "rates[i].servicelevel.name")
        servicelevelToken = jResp.string(of: "rates[i].servicelevel.token")
        servicelevelTerms = jResp.string(of: "rates[i].servicelevel.terms")
        estimated_days = jResp.int(of: "rates[i].estimated_days").intValue
        arrives_by = jResp.string(of: "rates[i].arrives_by")
        duration_terms = jResp.string(of: "rates[i].duration_terms")
        carrier_account = jResp.string(of: "rates[i].carrier_account")
        test = jResp.bool(of: "rates[i].test")
        zone = jResp.string(of: "rates[i].zone")
        j = 0
        count_j = jResp.size(ofArray: "rates[i].attributes").intValue
        while j < count_j {
            jResp.j = j
            strVal = jResp.string(of: "rates[i].attributes[j]")
            j = j + 1
        }

        j = 0
        count_j = jResp.size(ofArray: "rates[i].messages").intValue
        while j < count_j {
            jResp.j = j
            j = j + 1
        }

        i = i + 1
    }

    i = 0
    count_i = jResp.size(ofArray: "messages").intValue
    while i < count_i {
        jResp.i = i
        source = jResp.string(of: "messages[i].source")
        code = jResp.string(of: "messages[i].code")
        text = jResp.string(of: "messages[i].text")
        i = i + 1
    }


}