Lianja
Lianja
Shippo Create the Shipment, Get Rates, and Purchase Label
See more Shippo Examples
Demonstrates how retrieve rates and create labels for international shipments.Chilkat Lianja Downloads
llSuccess = .F.
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loHttp = createobject("CkHttp")
// 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"
// }
// ]
// }
loJson = createobject("CkJsonObject")
loJson.UpdateString("contents_type","MERCHANDISE")
loJson.UpdateString("non_delivery_option","RETURN")
loJson.UpdateBool("certify",.T.)
loJson.UpdateString("certify_signer","Simon Kreuz")
loJson.UpdateString("incoterm","DDU")
loJson.UpdateString("items[0].description","T-shirt")
loJson.UpdateInt("items[0].quantity",20)
loJson.UpdateString("items[0].net_weight","5")
loJson.UpdateString("items[0].mass_unit","lb")
loJson.UpdateString("items[0].value_amount","200")
loJson.UpdateString("items[0].value_currency","USD")
loJson.UpdateString("items[0].tariff_number","")
loJson.UpdateString("items[0].origin_country","US")
loHttp.SetRequestHeader("Authorization","ShippoToken <API_TOKEN>")
loHttp.SetRequestHeader("Content-Type","application/json")
loResp = createobject("CkHttpResponse")
llSuccess = loHttp.HttpJson("POST","https://api.goshippo.com/customs/declarations/",loJson,"application/json",loResp)
if (llSuccess = .F.) then
? loHttp.LastErrorText
release loHttp
release loJson
release loResp
return
endif
loSbResponseBody = createobject("CkStringBuilder")
loResp.GetBodySb(loSbResponseBody)
loJResp = createobject("CkJsonObject")
loJResp.LoadSb(loSbResponseBody)
loJResp.EmitCompact = .F.
? "Response Body:"
? loJResp.Emit()
lnRespStatusCode = loResp.StatusCode
? "Response Status Code = " + str(lnRespStatusCode)
if (lnRespStatusCode >= 400) then
? "Response Header:"
? loResp.Header
? "Failed."
release loHttp
release loJson
release loResp
release loSbResponseBody
release loJResp
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
lcObject_created = loJResp.StringOf("object_created")
lcObject_updated = loJResp.StringOf("object_updated")
lcObject_id = loJResp.StringOf("object_id")
lcObject_owner = loJResp.StringOf("object_owner")
lcStatus = loJResp.StringOf("status")
lcAddress_fromObject_id = loJResp.StringOf("address_from.object_id")
llAddress_fromIs_complete = loJResp.BoolOf("address_from.is_complete")
lcAddress_fromCompany = loJResp.StringOf("address_from.company")
lcAddress_fromStreet_no = loJResp.StringOf("address_from.street_no")
lcAddress_fromName = loJResp.StringOf("address_from.name")
lcAddress_fromStreet1 = loJResp.StringOf("address_from.street1")
lcAddress_fromStreet2 = loJResp.StringOf("address_from.street2")
lcAddress_fromCity = loJResp.StringOf("address_from.city")
lcAddress_fromState = loJResp.StringOf("address_from.state")
lcAddress_fromZip = loJResp.StringOf("address_from.zip")
lcAddress_fromCountry = loJResp.StringOf("address_from.country")
lcAddress_fromPhone = loJResp.StringOf("address_from.phone")
lcAddress_fromEmail = loJResp.StringOf("address_from.email")
lcAddress_fromIs_residential = loJResp.StringOf("address_from.is_residential")
lcAddress_toObject_id = loJResp.StringOf("address_to.object_id")
llAddress_toIs_complete = loJResp.BoolOf("address_to.is_complete")
lcAddress_toName = loJResp.StringOf("address_to.name")
lcAddress_toStreet1 = loJResp.StringOf("address_to.street1")
lcAddress_toCity = loJResp.StringOf("address_to.city")
lcAddress_toState = loJResp.StringOf("address_to.state")
lcAddress_toZip = loJResp.StringOf("address_to.zip")
lcAddress_toCountry = loJResp.StringOf("address_to.country")
lcAddress_toPhone = loJResp.StringOf("address_to.phone")
lcAddress_toEmail = loJResp.StringOf("address_to.email")
llAddress_toIs_residential = loJResp.BoolOf("address_to.is_residential")
lcAddress_return = loJResp.StringOf("address_return")
lcShipment_date = loJResp.StringOf("shipment_date")
lcExtraInsuranceCurrency = loJResp.StringOf("extra.insurance.currency")
lcExtraInsuranceAmount = loJResp.StringOf("extra.insurance.amount")
lcExtraReference_1 = loJResp.StringOf("extra.reference_1")
lcExtraReference_2 = loJResp.StringOf("extra.reference_2")
lcCustoms_declaration = loJResp.StringOf("customs_declaration")
lcMetadata = loJResp.StringOf("metadata")
i = 0
lnCount_i = loJResp.SizeOfArray("parcels")
do while i < lnCount_i
loJResp.I = i
lcObject_id = loJResp.StringOf("parcels[i].object_id")
lcObject_created = loJResp.StringOf("parcels[i].object_created")
lcObject_updated = loJResp.StringOf("parcels[i].object_updated")
lcObject_owner = loJResp.StringOf("parcels[i].object_owner")
lcTemplate = loJResp.StringOf("parcels[i].template")
lcLength = loJResp.StringOf("parcels[i].length")
lcWidth = loJResp.StringOf("parcels[i].width")
lcHeight = loJResp.StringOf("parcels[i].height")
lcDistance_unit = loJResp.StringOf("parcels[i].distance_unit")
lcWeight = loJResp.StringOf("parcels[i].weight")
lcMass_unit = loJResp.StringOf("parcels[i].mass_unit")
lcValue_amount = loJResp.StringOf("parcels[i].value_amount")
lcValue_currency = loJResp.StringOf("parcels[i].value_currency")
lcMetadata = loJResp.StringOf("parcels[i].metadata")
llTest = loJResp.BoolOf("parcels[i].test")
j = 0
lnCount_j = loJResp.SizeOfArray("parcels[i].line_items")
do while j < lnCount_j
loJResp.J = j
j = j + 1
enddo
i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("rates")
do while i < lnCount_i
loJResp.I = i
lnIntVal = loJResp.IntOf("rates[i]")
j = 0
lnCount_j = loJResp.SizeOfArray("rates[i].attributes")
do while j < lnCount_j
loJResp.J = j
j = j + 1
enddo
j = 0
lnCount_j = loJResp.SizeOfArray("rates[i].messages")
do while j < lnCount_j
loJResp.J = j
j = j + 1
enddo
i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("carrier_accounts")
do while i < lnCount_i
loJResp.I = i
i = i + 1
enddo
i = 0
lnCount_i = loJResp.SizeOfArray("messages")
do while i < lnCount_i
loJResp.I = i
i = i + 1
enddo
release loHttp
release loJson
release loResp
release loSbResponseBody
release loJResp