Sample code for 30+ languages & platforms
Swift

Shippo Create Customs Declaration

See more Shippo Examples

Demonstrates how to create a customs declaration and send a POST request with the necessary information to the Customs Declarations endpoint.

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/customs/declarations/ \
    //     -H "Authorization: ShippoToken <API_TOKEN>" \
    //     -H "Content-Type: application/json"  \
    //     -d '{          
    //           "contents_type": "MERCHANDISE",
    //           "non_delivery_option": "RETURN",
    //           "certify": true,
    //           "certify_signer": "Simon Kreuz",
    //           "incoterm": "DDU",
    //           "items": [{
    //                     "description": "T-shirt",
    //                     "quantity": 20,
    //                     "net_weight": "5",
    //                     "mass_unit": "lb",
    //                     "value_amount": "200",
    //                     "value_currency": "USD",
    //                     "tariff_number": "",
    //                     "origin_country": "US"
    //             }]
    //     }'

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

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

    // {
    //   "contents_type": "MERCHANDISE",
    //   "non_delivery_option": "RETURN",
    //   "certify": true,
    //   "certify_signer": "Simon Kreuz",
    //   "incoterm": "DDU",
    //   "items": [
    //     {
    //       "description": "T-shirt",
    //       "quantity": 20,
    //       "net_weight": "5",
    //       "mass_unit": "lb",
    //       "value_amount": "200",
    //       "value_currency": "USD",
    //       "tariff_number": "",
    //       "origin_country": "US"
    //     }
    //   ]
    // }

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "contents_type", value: "MERCHANDISE")
    json.updateString(jsonPath: "non_delivery_option", value: "RETURN")
    json.updateBool(jsonPath: "certify", value: true)
    json.updateString(jsonPath: "certify_signer", value: "Simon Kreuz")
    json.updateString(jsonPath: "incoterm", value: "DDU")
    json.updateString(jsonPath: "items[0].description", value: "T-shirt")
    json.updateInt(jsonPath: "items[0].quantity", value: 20)
    json.updateString(jsonPath: "items[0].net_weight", value: "5")
    json.updateString(jsonPath: "items[0].mass_unit", value: "lb")
    json.updateString(jsonPath: "items[0].value_amount", value: "200")
    json.updateString(jsonPath: "items[0].value_currency", value: "USD")
    json.updateString(jsonPath: "items[0].tariff_number", value: "")
    json.updateString(jsonPath: "items[0].origin_country", value: "US")

    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/customs/declarations/", 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)

    // {
    //   "object_created": "2019-07-04T16:00:29.043Z",
    //   "object_updated": "2019-07-04T16:00:29.043Z",
    //   "object_id": "4a1e6ab7b1ba49ed9bc6cb1a8798e0fd",
    //   "object_owner": "admin@chilkatsoft.com",
    //   "object_state": "VALID",
    //   "address_importer": null,
    //   "certify_signer": "Simon Kreuz",
    //   "certify": true,
    //   "items": [
    //     "4096c68693364b7ea0af72fb869ee861"
    //   ],
    //   "non_delivery_option": "RETURN",
    //   "contents_type": "MERCHANDISE",
    //   "contents_explanation": "",
    //   "exporter_reference": "",
    //   "importer_reference": "",
    //   "invoice": "",
    //   "commercial_invoice": false,
    //   "license": "",
    //   "certificate": "",
    //   "notes": "",
    //   "eel_pfc": "",
    //   "aes_itn": "",
    //   "disclaimer": "",
    //   "incoterm": "DDU",
    //   "metadata": "",
    //   "test": true,
    //   "duties_payor": 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 strVal: 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 object_state: String? = jResp.string(of: "object_state")
    var address_importer: String? = jResp.string(of: "address_importer")
    var certify_signer: String? = jResp.string(of: "certify_signer")
    var certify: Bool = jResp.bool(of: "certify")
    var non_delivery_option: String? = jResp.string(of: "non_delivery_option")
    var contents_type: String? = jResp.string(of: "contents_type")
    var contents_explanation: String? = jResp.string(of: "contents_explanation")
    var exporter_reference: String? = jResp.string(of: "exporter_reference")
    var importer_reference: String? = jResp.string(of: "importer_reference")
    var invoice: String? = jResp.string(of: "invoice")
    var commercial_invoice: Bool = jResp.bool(of: "commercial_invoice")
    var license: String? = jResp.string(of: "license")
    var certificate: String? = jResp.string(of: "certificate")
    var notes: String? = jResp.string(of: "notes")
    var eel_pfc: String? = jResp.string(of: "eel_pfc")
    var aes_itn: String? = jResp.string(of: "aes_itn")
    var disclaimer: String? = jResp.string(of: "disclaimer")
    var incoterm: String? = jResp.string(of: "incoterm")
    var metadata: String? = jResp.string(of: "metadata")
    var test: Bool = jResp.bool(of: "test")
    var duties_payor: String? = jResp.string(of: "duties_payor")
    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "items").intValue
    while i < count_i {
        jResp.i = i
        strVal = jResp.string(of: "items[i]")
        i = i + 1
    }


}