Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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"
    ;     }
    ;   ]
    ; }

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"contents_type","MERCHANDISE")
    CkJsonObject::ckUpdateString(json,"non_delivery_option","RETURN")
    CkJsonObject::ckUpdateBool(json,"certify",1)
    CkJsonObject::ckUpdateString(json,"certify_signer","Simon Kreuz")
    CkJsonObject::ckUpdateString(json,"incoterm","DDU")
    CkJsonObject::ckUpdateString(json,"items[0].description","T-shirt")
    CkJsonObject::ckUpdateInt(json,"items[0].quantity",20)
    CkJsonObject::ckUpdateString(json,"items[0].net_weight","5")
    CkJsonObject::ckUpdateString(json,"items[0].mass_unit","lb")
    CkJsonObject::ckUpdateString(json,"items[0].value_amount","200")
    CkJsonObject::ckUpdateString(json,"items[0].value_currency","USD")
    CkJsonObject::ckUpdateString(json,"items[0].tariff_number","")
    CkJsonObject::ckUpdateString(json,"items[0].origin_country","US")

    CkHttp::ckSetRequestHeader(http,"Authorization","ShippoToken <API_TOKEN>")
    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpJson(http,"POST","https://api.goshippo.com/customs/declarations/",json,"application/json",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)
    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkHttpResponse::ckDispose(resp)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    ; 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

    strVal.s

    object_created.s = CkJsonObject::ckStringOf(jResp,"object_created")
    object_updated.s = CkJsonObject::ckStringOf(jResp,"object_updated")
    object_id.s = CkJsonObject::ckStringOf(jResp,"object_id")
    object_owner.s = CkJsonObject::ckStringOf(jResp,"object_owner")
    object_state.s = CkJsonObject::ckStringOf(jResp,"object_state")
    address_importer.s = CkJsonObject::ckStringOf(jResp,"address_importer")
    certify_signer.s = CkJsonObject::ckStringOf(jResp,"certify_signer")
    certify.i = CkJsonObject::ckBoolOf(jResp,"certify")
    non_delivery_option.s = CkJsonObject::ckStringOf(jResp,"non_delivery_option")
    contents_type.s = CkJsonObject::ckStringOf(jResp,"contents_type")
    contents_explanation.s = CkJsonObject::ckStringOf(jResp,"contents_explanation")
    exporter_reference.s = CkJsonObject::ckStringOf(jResp,"exporter_reference")
    importer_reference.s = CkJsonObject::ckStringOf(jResp,"importer_reference")
    invoice.s = CkJsonObject::ckStringOf(jResp,"invoice")
    commercial_invoice.i = CkJsonObject::ckBoolOf(jResp,"commercial_invoice")
    license.s = CkJsonObject::ckStringOf(jResp,"license")
    certificate.s = CkJsonObject::ckStringOf(jResp,"certificate")
    notes.s = CkJsonObject::ckStringOf(jResp,"notes")
    eel_pfc.s = CkJsonObject::ckStringOf(jResp,"eel_pfc")
    aes_itn.s = CkJsonObject::ckStringOf(jResp,"aes_itn")
    disclaimer.s = CkJsonObject::ckStringOf(jResp,"disclaimer")
    incoterm.s = CkJsonObject::ckStringOf(jResp,"incoterm")
    metadata.s = CkJsonObject::ckStringOf(jResp,"metadata")
    test.i = CkJsonObject::ckBoolOf(jResp,"test")
    duties_payor.s = CkJsonObject::ckStringOf(jResp,"duties_payor")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"items")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        strVal = CkJsonObject::ckStringOf(jResp,"items[i]")
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkHttpResponse::ckDispose(resp)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure