PureBasic
PureBasic
Shippo Create the Shipment, Get Rates, and Purchase Label
See more Shippo Examples
Demonstrates how retrieve rates and create labels for international shipments.Chilkat PureBasic Downloads
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": "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
template.s
length.s
width.s
height.s
distance_unit.s
weight.s
mass_unit.s
value_amount.s
value_currency.s
test.i
j.i
count_j.i
intVal.i
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")
status.s = CkJsonObject::ckStringOf(jResp,"status")
address_fromObject_id.s = CkJsonObject::ckStringOf(jResp,"address_from.object_id")
address_fromIs_complete.i = CkJsonObject::ckBoolOf(jResp,"address_from.is_complete")
address_fromCompany.s = CkJsonObject::ckStringOf(jResp,"address_from.company")
address_fromStreet_no.s = CkJsonObject::ckStringOf(jResp,"address_from.street_no")
address_fromName.s = CkJsonObject::ckStringOf(jResp,"address_from.name")
address_fromStreet1.s = CkJsonObject::ckStringOf(jResp,"address_from.street1")
address_fromStreet2.s = CkJsonObject::ckStringOf(jResp,"address_from.street2")
address_fromCity.s = CkJsonObject::ckStringOf(jResp,"address_from.city")
address_fromState.s = CkJsonObject::ckStringOf(jResp,"address_from.state")
address_fromZip.s = CkJsonObject::ckStringOf(jResp,"address_from.zip")
address_fromCountry.s = CkJsonObject::ckStringOf(jResp,"address_from.country")
address_fromPhone.s = CkJsonObject::ckStringOf(jResp,"address_from.phone")
address_fromEmail.s = CkJsonObject::ckStringOf(jResp,"address_from.email")
address_fromIs_residential.s = CkJsonObject::ckStringOf(jResp,"address_from.is_residential")
address_toObject_id.s = CkJsonObject::ckStringOf(jResp,"address_to.object_id")
address_toIs_complete.i = CkJsonObject::ckBoolOf(jResp,"address_to.is_complete")
address_toName.s = CkJsonObject::ckStringOf(jResp,"address_to.name")
address_toStreet1.s = CkJsonObject::ckStringOf(jResp,"address_to.street1")
address_toCity.s = CkJsonObject::ckStringOf(jResp,"address_to.city")
address_toState.s = CkJsonObject::ckStringOf(jResp,"address_to.state")
address_toZip.s = CkJsonObject::ckStringOf(jResp,"address_to.zip")
address_toCountry.s = CkJsonObject::ckStringOf(jResp,"address_to.country")
address_toPhone.s = CkJsonObject::ckStringOf(jResp,"address_to.phone")
address_toEmail.s = CkJsonObject::ckStringOf(jResp,"address_to.email")
address_toIs_residential.i = CkJsonObject::ckBoolOf(jResp,"address_to.is_residential")
address_return.s = CkJsonObject::ckStringOf(jResp,"address_return")
shipment_date.s = CkJsonObject::ckStringOf(jResp,"shipment_date")
extraInsuranceCurrency.s = CkJsonObject::ckStringOf(jResp,"extra.insurance.currency")
extraInsuranceAmount.s = CkJsonObject::ckStringOf(jResp,"extra.insurance.amount")
extraReference_1.s = CkJsonObject::ckStringOf(jResp,"extra.reference_1")
extraReference_2.s = CkJsonObject::ckStringOf(jResp,"extra.reference_2")
customs_declaration.s = CkJsonObject::ckStringOf(jResp,"customs_declaration")
metadata.s = CkJsonObject::ckStringOf(jResp,"metadata")
i.i = 0
count_i.i = CkJsonObject::ckSizeOfArray(jResp,"parcels")
While i < count_i
CkJsonObject::setCkI(jResp, i)
object_id = CkJsonObject::ckStringOf(jResp,"parcels[i].object_id")
object_created = CkJsonObject::ckStringOf(jResp,"parcels[i].object_created")
object_updated = CkJsonObject::ckStringOf(jResp,"parcels[i].object_updated")
object_owner = CkJsonObject::ckStringOf(jResp,"parcels[i].object_owner")
template = CkJsonObject::ckStringOf(jResp,"parcels[i].template")
length = CkJsonObject::ckStringOf(jResp,"parcels[i].length")
width = CkJsonObject::ckStringOf(jResp,"parcels[i].width")
height = CkJsonObject::ckStringOf(jResp,"parcels[i].height")
distance_unit = CkJsonObject::ckStringOf(jResp,"parcels[i].distance_unit")
weight = CkJsonObject::ckStringOf(jResp,"parcels[i].weight")
mass_unit = CkJsonObject::ckStringOf(jResp,"parcels[i].mass_unit")
value_amount = CkJsonObject::ckStringOf(jResp,"parcels[i].value_amount")
value_currency = CkJsonObject::ckStringOf(jResp,"parcels[i].value_currency")
metadata = CkJsonObject::ckStringOf(jResp,"parcels[i].metadata")
test = CkJsonObject::ckBoolOf(jResp,"parcels[i].test")
j = 0
count_j = CkJsonObject::ckSizeOfArray(jResp,"parcels[i].line_items")
While j < count_j
CkJsonObject::setCkJ(jResp, j)
j = j + 1
Wend
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"rates")
While i < count_i
CkJsonObject::setCkI(jResp, i)
intVal = CkJsonObject::ckIntOf(jResp,"rates[i]")
j = 0
count_j = CkJsonObject::ckSizeOfArray(jResp,"rates[i].attributes")
While j < count_j
CkJsonObject::setCkJ(jResp, j)
j = j + 1
Wend
j = 0
count_j = CkJsonObject::ckSizeOfArray(jResp,"rates[i].messages")
While j < count_j
CkJsonObject::setCkJ(jResp, j)
j = j + 1
Wend
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"carrier_accounts")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"messages")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
CkHttpResponse::ckDispose(resp)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndProcedure