Sample code for 30+ languages & platforms
Xbase++

Shippo Create the Shipment, Get Rates, and Purchase Label

See more Shippo Examples

Demonstrates how retrieve rates and create labels for international shipments.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oJson
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL cTemplate
LOCAL cLength
LOCAL cWidth
LOCAL cHeight
LOCAL cDistance_unit
LOCAL cWeight
LOCAL cMass_unit
LOCAL cValue_amount
LOCAL cValue_currency
LOCAL nTest
LOCAL j
LOCAL nCount_j
LOCAL nIntVal
LOCAL cObject_created
LOCAL cObject_updated
LOCAL cObject_id
LOCAL cObject_owner
LOCAL cStatus
LOCAL cAddress_fromObject_id
LOCAL nAddress_fromIs_complete
LOCAL cAddress_fromCompany
LOCAL cAddress_fromStreet_no
LOCAL cAddress_fromName
LOCAL cAddress_fromStreet1
LOCAL cAddress_fromStreet2
LOCAL cAddress_fromCity
LOCAL cAddress_fromState
LOCAL cAddress_fromZip
LOCAL cAddress_fromCountry
LOCAL cAddress_fromPhone
LOCAL cAddress_fromEmail
LOCAL cAddress_fromIs_residential
LOCAL cAddress_toObject_id
LOCAL nAddress_toIs_complete
LOCAL cAddress_toName
LOCAL cAddress_toStreet1
LOCAL cAddress_toCity
LOCAL cAddress_toState
LOCAL cAddress_toZip
LOCAL cAddress_toCountry
LOCAL cAddress_toPhone
LOCAL cAddress_toEmail
LOCAL nAddress_toIs_residential
LOCAL cAddress_return
LOCAL cShipment_date
LOCAL cExtraInsuranceCurrency
LOCAL cExtraInsuranceAmount
LOCAL cExtraReference_1
LOCAL cExtraReference_2
LOCAL cCustoms_declaration
LOCAL cMetadata
LOCAL i
LOCAL nCount_i

nSuccess := 0

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

oHttp := CreateObject("Chilkat.Http")

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

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("contents_type", "MERCHANDISE")
oJson:UpdateString("non_delivery_option", "RETURN")
oJson:UpdateBool("certify", 1)
oJson:UpdateString("certify_signer", "Simon Kreuz")
oJson:UpdateString("incoterm", "DDU")
oJson:UpdateString("items[0].description", "T-shirt")
oJson:UpdateInt("items[0].quantity", 20)
oJson:UpdateString("items[0].net_weight", "5")
oJson:UpdateString("items[0].mass_unit", "lb")
oJson:UpdateString("items[0].value_amount", "200")
oJson:UpdateString("items[0].value_currency", "USD")
oJson:UpdateString("items[0].tariff_number", "")
oJson:UpdateString("items[0].origin_country", "US")

oHttp:SetRequestHeader("Authorization", "ShippoToken <API_TOKEN>")
oHttp:SetRequestHeader("Content-Type", "application/json")

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://api.goshippo.com/customs/declarations/", oJson, "application/json", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oJson:destroy()
    oResp:destroy()
    RETURN
ENDIF

oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)
oJResp := CreateObject("Chilkat.JsonObject")
oJResp:LoadSb(oSbResponseBody)
oJResp:EmitCompact := 0

? "Response Body:"
? oJResp:Emit()

nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode >= 400)
    ? "Response Header:"
    ? oResp:Header
    ? "Failed."
    oHttp:destroy()
    oJson:destroy()
    oResp:destroy()
    oSbResponseBody:destroy()
    oJResp:destroy()
    RETURN
ENDIF

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

//  {
//    "object_created": "2014-07-17T00:04:06.163Z",
//    "object_updated": "2014-07-17T00:04:06.163Z",
//    "object_id": "89436997a794439ab47999701e60392e",
//    "object_owner": "shippotle@goshippo.com",
//    "status": "SUCCESS",
//    "address_from": {
//      "object_id": "0943ae4e373e4120a99c337e496dcce8",
//      "validation_results": {},
//      "is_complete": true,
//      "company": "",
//      "street_no": "",
//      "name": "Mr. Hippo",
//      "street1": "215 Clayton St.",
//      "street2": "",
//      "city": "San Francisco",
//      "state": "CA",
//      "zip": "94117",
//      "country": "US",
//      "phone": "+15553419393",
//      "email": "support@goshippo.com",
//      "is_residential": null
//    },
//    "address_to": {
//      "object_id": "4c7185d353764d0985a6a7825aed8ffb",
//      "validation_results": {},
//      "is_complete": true,
//      "name": "Mrs. Hippo",
//      "street1": "200 University Ave W",
//      "city": "Waterloo",
//      "state": "ON",
//      "zip": "N2L 3G1",
//      "country": "CA",
//      "phone": "+1 555 341 9393",
//      "email": "support@goshippo.com",
//      "is_residential": false
//    },
//    "address_return": null,
//    "parcels": [
//      {
//        "object_id": "ec952343dd4843c39b42aca620471fd5",
//        "object_created": "2013-12-01T06:24:21.121Z",
//        "object_updated": "2013-12-01T06:24:21.121Z",
//        "object_owner": "shippotle@goshippo.com",
//        "template": null,
//        "length": "5",
//        "width": "5",
//        "height": "5",
//        "distance_unit": "in",
//        "weight": "2",
//        "mass_unit": "lb",
//        "value_amount": null,
//        "value_currency": null,
//        "metadata": "",
//        "line_items": [
//        ],
//        "test": true
//      }
//    ],
//    "shipment_date": "2013-12-03T12:00:00Z",
//    "extra": {
//      "insurance": {
//        "currency": "",
//        "amount": ""
//      },
//      "reference_1": "",
//      "reference_2": ""
//    },
//    "customs_declaration": "b741b99f95e841639b54272834bc478c",
//    "rates": [
//      {
//        "object_created": "2014-07-17T00:04:06.263Z",
//        "object_id": "545ab0a1a6ea4c9f9adb2512a57d6d8b",
//        "object_owner": "shippotle@goshippo.com",
//        "shipment": "89436997a794439ab47999701e60392e",
//        "attributes": [
//        ],
//        "amount": "5.50",
//        "currency": "USD",
//        "amount_local": "5.50",
//        "currency_local": "USD",
//        "provider": "USPS",
//        "provider_image_75": "https://cdn2.goshippo.com/providers/75/USPS.png",
//        "provider_image_200": "https://cdn2.goshippo.com/providers/200/USPS.png",
//        "servicelevel": {
//          "name": "Priority Mail",
//          "token": "usps_priority",
//          "terms": ""
//        },
//        "days": 2,
//        "arrives_by": null,
//        "duration_terms": "Delivery in 1 to 3 business days.",
//        "messages": [
//        ],
//        "carrier_account": "078870331023437cb917f5187429b093",
//        "test": false,
//        "zone": 1
//      },
//      ...
//    ],
//    "carrier_accounts": [
//    ],
//    "messages": [
//    ],
//    "metadata": "Customer ID 123456"
//  }

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

cObject_created := oJResp:StringOf("object_created")
cObject_updated := oJResp:StringOf("object_updated")
cObject_id := oJResp:StringOf("object_id")
cObject_owner := oJResp:StringOf("object_owner")
cStatus := oJResp:StringOf("status")
cAddress_fromObject_id := oJResp:StringOf("address_from.object_id")
nAddress_fromIs_complete := oJResp:BoolOf("address_from.is_complete")
cAddress_fromCompany := oJResp:StringOf("address_from.company")
cAddress_fromStreet_no := oJResp:StringOf("address_from.street_no")
cAddress_fromName := oJResp:StringOf("address_from.name")
cAddress_fromStreet1 := oJResp:StringOf("address_from.street1")
cAddress_fromStreet2 := oJResp:StringOf("address_from.street2")
cAddress_fromCity := oJResp:StringOf("address_from.city")
cAddress_fromState := oJResp:StringOf("address_from.state")
cAddress_fromZip := oJResp:StringOf("address_from.zip")
cAddress_fromCountry := oJResp:StringOf("address_from.country")
cAddress_fromPhone := oJResp:StringOf("address_from.phone")
cAddress_fromEmail := oJResp:StringOf("address_from.email")
cAddress_fromIs_residential := oJResp:StringOf("address_from.is_residential")
cAddress_toObject_id := oJResp:StringOf("address_to.object_id")
nAddress_toIs_complete := oJResp:BoolOf("address_to.is_complete")
cAddress_toName := oJResp:StringOf("address_to.name")
cAddress_toStreet1 := oJResp:StringOf("address_to.street1")
cAddress_toCity := oJResp:StringOf("address_to.city")
cAddress_toState := oJResp:StringOf("address_to.state")
cAddress_toZip := oJResp:StringOf("address_to.zip")
cAddress_toCountry := oJResp:StringOf("address_to.country")
cAddress_toPhone := oJResp:StringOf("address_to.phone")
cAddress_toEmail := oJResp:StringOf("address_to.email")
nAddress_toIs_residential := oJResp:BoolOf("address_to.is_residential")
cAddress_return := oJResp:StringOf("address_return")
cShipment_date := oJResp:StringOf("shipment_date")
cExtraInsuranceCurrency := oJResp:StringOf("extra.insurance.currency")
cExtraInsuranceAmount := oJResp:StringOf("extra.insurance.amount")
cExtraReference_1 := oJResp:StringOf("extra.reference_1")
cExtraReference_2 := oJResp:StringOf("extra.reference_2")
cCustoms_declaration := oJResp:StringOf("customs_declaration")
cMetadata := oJResp:StringOf("metadata")
i := 0
nCount_i := oJResp:SizeOfArray("parcels")
DO WHILE i < nCount_i
    oJResp:I := i
    cObject_id := oJResp:StringOf("parcels[i].object_id")
    cObject_created := oJResp:StringOf("parcels[i].object_created")
    cObject_updated := oJResp:StringOf("parcels[i].object_updated")
    cObject_owner := oJResp:StringOf("parcels[i].object_owner")
    cTemplate := oJResp:StringOf("parcels[i].template")
    cLength := oJResp:StringOf("parcels[i].length")
    cWidth := oJResp:StringOf("parcels[i].width")
    cHeight := oJResp:StringOf("parcels[i].height")
    cDistance_unit := oJResp:StringOf("parcels[i].distance_unit")
    cWeight := oJResp:StringOf("parcels[i].weight")
    cMass_unit := oJResp:StringOf("parcels[i].mass_unit")
    cValue_amount := oJResp:StringOf("parcels[i].value_amount")
    cValue_currency := oJResp:StringOf("parcels[i].value_currency")
    cMetadata := oJResp:StringOf("parcels[i].metadata")
    nTest := oJResp:BoolOf("parcels[i].test")
    j := 0
    nCount_j := oJResp:SizeOfArray("parcels[i].line_items")
    DO WHILE j < nCount_j
        oJResp:J := j
        j := j + 1
    ENDDO
    i := i + 1
ENDDO
i := 0
nCount_i := oJResp:SizeOfArray("rates")
DO WHILE i < nCount_i
    oJResp:I := i
    nIntVal := oJResp:IntOf("rates[i]")
    j := 0
    nCount_j := oJResp:SizeOfArray("rates[i].attributes")
    DO WHILE j < nCount_j
        oJResp:J := j
        j := j + 1
    ENDDO
    j := 0
    nCount_j := oJResp:SizeOfArray("rates[i].messages")
    DO WHILE j < nCount_j
        oJResp:J := j
        j := j + 1
    ENDDO
    i := i + 1
ENDDO
i := 0
nCount_i := oJResp:SizeOfArray("carrier_accounts")
DO WHILE i < nCount_i
    oJResp:I := i
    i := i + 1
ENDDO
i := 0
nCount_i := oJResp:SizeOfArray("messages")
DO WHILE i < nCount_i
    oJResp:I := i
    i := i + 1
ENDDO

oHttp:destroy()
oJson:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()