PureBasic
PureBasic
Shippo Create Shipment Object
See more Shippo Examples
Create a Shipment object to retrieve rates. It represents a request to ship a given package from the sender to the recipient address.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/shipments/ \
; -H "Authorization: ShippoToken <API_TOKEN>" \
; -H "Content-Type: application/json" \
; -d '{
; "address_from":{
; "name":"Mr. Hippo",
; "street1":"215 Clayton St.",
; "city":"San Francisco",
; "state":"CA",
; "zip":"94117",
; "country":"US"
; },
; "address_to":{
; "name":"Mrs. Hippo",
; "street1":"965 Mission St.",
; "city":"San Francisco",
; "state":"CA",
; "zip":"94105",
; "country":"US"
; },
; "parcels":[{
; "length":"5",
; "width":"5",
; "height":"5",
; "distance_unit":"in",
; "weight":"2",
; "mass_unit":"lb"
; }],
; "async": false
; }'
; Use this online tool to generate code from sample JSON:
; Generate Code to Create JSON
; The following JSON is sent in the request body.
; {
; "address_from": {
; "name": "Mr. Hippo",
; "street1": "215 Clayton St.",
; "city": "San Francisco",
; "state": "CA",
; "zip": "94117",
; "country": "US"
; },
; "address_to": {
; "name": "Mrs. Hippo",
; "street1": "965 Mission St.",
; "city": "San Francisco",
; "state": "CA",
; "zip": "94105",
; "country": "US"
; },
; "parcels": [
; {
; "length": "5",
; "width": "5",
; "height": "5",
; "distance_unit": "in",
; "weight": "2",
; "mass_unit": "lb"
; }
; ],
; "async": false
; }
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"address_from.name","Mr. Hippo")
CkJsonObject::ckUpdateString(json,"address_from.street1","215 Clayton St.")
CkJsonObject::ckUpdateString(json,"address_from.city","San Francisco")
CkJsonObject::ckUpdateString(json,"address_from.state","CA")
CkJsonObject::ckUpdateString(json,"address_from.zip","94117")
CkJsonObject::ckUpdateString(json,"address_from.country","US")
CkJsonObject::ckUpdateString(json,"address_to.name","Mrs. Hippo")
CkJsonObject::ckUpdateString(json,"address_to.street1","965 Mission St.")
CkJsonObject::ckUpdateString(json,"address_to.city","San Francisco")
CkJsonObject::ckUpdateString(json,"address_to.state","CA")
CkJsonObject::ckUpdateString(json,"address_to.zip","94105")
CkJsonObject::ckUpdateString(json,"address_to.country","US")
CkJsonObject::ckUpdateString(json,"parcels[0].length","5")
CkJsonObject::ckUpdateString(json,"parcels[0].width","5")
CkJsonObject::ckUpdateString(json,"parcels[0].height","5")
CkJsonObject::ckUpdateString(json,"parcels[0].distance_unit","in")
CkJsonObject::ckUpdateString(json,"parcels[0].weight","2")
CkJsonObject::ckUpdateString(json,"parcels[0].mass_unit","lb")
CkJsonObject::ckUpdateBool(json,"async",0)
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/shipments/",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)
; {
; "carrier_accounts": [
; ],
; "object_created": "2019-06-28T17:45:20.441Z",
; "object_updated": "2019-06-28T17:45:20.511Z",
; "object_id": "17b01795a4884d8a9002ccc17c9d09d1",
; "object_owner": "admin@chilkatsoft.com",
; "status": "SUCCESS",
; "address_from": {
; "object_id": "427489906b604f498f7bd2429ab7d9a1",
; "is_complete": true,
; "name": "Mr. Hippo",
; "company": "",
; "street_no": "",
; "street1": "215 Clayton St.",
; "validation_results": {},
; "street2": "",
; "street3": "",
; "city": "San Francisco",
; "state": "CA",
; "zip": "94117",
; "country": "US",
; "phone": "",
; "email": "",
; "is_residential": null,
; "test": true
; },
; "address_to": {
; "object_id": "3d7166269a2844b7842ef9ace942ec7c",
; "is_complete": true,
; "name": "Mrs. Hippo",
; "company": "",
; "street_no": "",
; "street1": "965 Mission St.",
; "validation_results": {},
; "street2": "",
; "street3": "",
; "city": "San Francisco",
; "state": "CA",
; "zip": "94105",
; "country": "US",
; "phone": "",
; "email": "",
; "is_residential": null,
; "test": true
; },
; "parcels": [
; {
; "object_state": "VALID",
; "object_created": "2019-06-28T17:45:20.411Z",
; "object_updated": "2019-06-28T17:45:20.455Z",
; "object_id": "5af766ff15684a4186b0e3c833348fac",
; "object_owner": "admin@chilkatsoft.com",
; "template": null,
; "extra": {},
; "length": "5.0000",
; "width": "5.0000",
; "height": "5.0000",
; "distance_unit": "in",
; "weight": "2.0000",
; "mass_unit": "lb",
; "value_amount": null,
; "value_currency": null,
; "metadata": "",
; "line_items": [
; ],
; "test": true
; }
; ],
; "shipment_date": "2019-06-28T17:45:20.511Z",
; "address_return": {
; "object_id": "427489906b604f498f7bd2429ab7d9a1",
; "is_complete": true,
; "name": "Mr. Hippo",
; "company": "",
; "street_no": "",
; "street1": "215 Clayton St.",
; "validation_results": {},
; "street2": "",
; "street3": "",
; "city": "San Francisco",
; "state": "CA",
; "zip": "94117",
; "country": "US",
; "phone": "",
; "email": "",
; "is_residential": null,
; "test": true
; },
; "alternate_address_to": null,
; "customs_declaration": null,
; "extra": {},
; "rates": [
; {
; "object_created": "2019-06-28T17:45:20.789Z",
; "object_id": "dd7556c284e8444294d2ab7099e662d2",
; "object_owner": "admin@chilkatsoft.com",
; "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
; "attributes": [
; "FASTEST"
; ],
; "amount": "22.78",
; "currency": "USD",
; "amount_local": "22.78",
; "currency_local": "USD",
; "provider": "USPS",
; "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
; "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
; "servicelevel": {
; "name": "Priority Mail Express",
; "token": "usps_priority_express",
; "terms": ""
; },
; "estimated_days": 2,
; "arrives_by": null,
; "duration_terms": "Overnight delivery to most U.S. locations.",
; "messages": [
; ],
; "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
; "test": true,
; "zone": "1"
; },
; {
; "object_created": "2019-06-28T17:45:20.786Z",
; "object_id": "ec11cc297e2f4583986097f6d409e5c6",
; "object_owner": "admin@chilkatsoft.com",
; "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
; "attributes": [
; "BESTVALUE",
; "CHEAPEST",
; "FASTEST"
; ],
; "amount": "6.95",
; "currency": "USD",
; "amount_local": "6.95",
; "currency_local": "USD",
; "provider": "USPS",
; "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
; "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
; "servicelevel": {
; "name": "Priority Mail",
; "token": "usps_priority",
; "terms": ""
; },
; "estimated_days": 2,
; "arrives_by": null,
; "duration_terms": "Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.",
; "messages": [
; ],
; "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
; "test": true,
; "zone": "1"
; },
; {
; "object_created": "2019-06-28T17:45:20.785Z",
; "object_id": "d256586f539f4c93a2f02cc11f4942ac",
; "object_owner": "admin@chilkatsoft.com",
; "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
; "attributes": [
; ],
; "amount": "7.32",
; "currency": "USD",
; "amount_local": "7.32",
; "currency_local": "USD",
; "provider": "USPS",
; "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
; "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
; "servicelevel": {
; "name": "Parcel Select",
; "token": "usps_parcel_select",
; "terms": ""
; },
; "estimated_days": 7,
; "arrives_by": null,
; "duration_terms": "Delivery in 2 to 8 days.",
; "messages": [
; ],
; "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
; "test": true,
; "zone": "1"
; }
; ],
; "messages": [
; {
; "source": "DHLExpress",
; "code": "",
; "text": "Shippo's DHL Express master account doesn't support shipments to inside of the US"
; }
; ],
; "metadata": "",
; "test": true,
; "order": 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
object_state.s
template.s
length.s
width.s
height.s
distance_unit.s
weight.s
mass_unit.s
value_amount.s
value_currency.s
j.i
count_j.i
shipment.s
amount.s
currency.s
amount_local.s
currency_local.s
provider.s
provider_image_75.s
provider_image_200.s
servicelevelName.s
servicelevelToken.s
servicelevelTerms.s
estimated_days.i
arrives_by.s
duration_terms.s
carrier_account.s
zone.s
strVal.s
source.s
code.s
text.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")
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_fromName.s = CkJsonObject::ckStringOf(jResp,"address_from.name")
address_fromCompany.s = CkJsonObject::ckStringOf(jResp,"address_from.company")
address_fromStreet_no.s = CkJsonObject::ckStringOf(jResp,"address_from.street_no")
address_fromStreet1.s = CkJsonObject::ckStringOf(jResp,"address_from.street1")
address_fromStreet2.s = CkJsonObject::ckStringOf(jResp,"address_from.street2")
address_fromStreet3.s = CkJsonObject::ckStringOf(jResp,"address_from.street3")
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_fromTest.i = CkJsonObject::ckBoolOf(jResp,"address_from.test")
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_toCompany.s = CkJsonObject::ckStringOf(jResp,"address_to.company")
address_toStreet_no.s = CkJsonObject::ckStringOf(jResp,"address_to.street_no")
address_toStreet1.s = CkJsonObject::ckStringOf(jResp,"address_to.street1")
address_toStreet2.s = CkJsonObject::ckStringOf(jResp,"address_to.street2")
address_toStreet3.s = CkJsonObject::ckStringOf(jResp,"address_to.street3")
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.s = CkJsonObject::ckStringOf(jResp,"address_to.is_residential")
address_toTest.i = CkJsonObject::ckBoolOf(jResp,"address_to.test")
shipment_date.s = CkJsonObject::ckStringOf(jResp,"shipment_date")
address_returnObject_id.s = CkJsonObject::ckStringOf(jResp,"address_return.object_id")
address_returnIs_complete.i = CkJsonObject::ckBoolOf(jResp,"address_return.is_complete")
address_returnName.s = CkJsonObject::ckStringOf(jResp,"address_return.name")
address_returnCompany.s = CkJsonObject::ckStringOf(jResp,"address_return.company")
address_returnStreet_no.s = CkJsonObject::ckStringOf(jResp,"address_return.street_no")
address_returnStreet1.s = CkJsonObject::ckStringOf(jResp,"address_return.street1")
address_returnStreet2.s = CkJsonObject::ckStringOf(jResp,"address_return.street2")
address_returnStreet3.s = CkJsonObject::ckStringOf(jResp,"address_return.street3")
address_returnCity.s = CkJsonObject::ckStringOf(jResp,"address_return.city")
address_returnState.s = CkJsonObject::ckStringOf(jResp,"address_return.state")
address_returnZip.s = CkJsonObject::ckStringOf(jResp,"address_return.zip")
address_returnCountry.s = CkJsonObject::ckStringOf(jResp,"address_return.country")
address_returnPhone.s = CkJsonObject::ckStringOf(jResp,"address_return.phone")
address_returnEmail.s = CkJsonObject::ckStringOf(jResp,"address_return.email")
address_returnIs_residential.s = CkJsonObject::ckStringOf(jResp,"address_return.is_residential")
address_returnTest.i = CkJsonObject::ckBoolOf(jResp,"address_return.test")
alternate_address_to.s = CkJsonObject::ckStringOf(jResp,"alternate_address_to")
customs_declaration.s = CkJsonObject::ckStringOf(jResp,"customs_declaration")
metadata.s = CkJsonObject::ckStringOf(jResp,"metadata")
test.i = CkJsonObject::ckBoolOf(jResp,"test")
order.s = CkJsonObject::ckStringOf(jResp,"order")
i.i = 0
count_i.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,"parcels")
While i < count_i
CkJsonObject::setCkI(jResp, i)
object_state = CkJsonObject::ckStringOf(jResp,"parcels[i].object_state")
object_created = CkJsonObject::ckStringOf(jResp,"parcels[i].object_created")
object_updated = CkJsonObject::ckStringOf(jResp,"parcels[i].object_updated")
object_id = CkJsonObject::ckStringOf(jResp,"parcels[i].object_id")
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)
object_created = CkJsonObject::ckStringOf(jResp,"rates[i].object_created")
object_id = CkJsonObject::ckStringOf(jResp,"rates[i].object_id")
object_owner = CkJsonObject::ckStringOf(jResp,"rates[i].object_owner")
shipment = CkJsonObject::ckStringOf(jResp,"rates[i].shipment")
amount = CkJsonObject::ckStringOf(jResp,"rates[i].amount")
currency = CkJsonObject::ckStringOf(jResp,"rates[i].currency")
amount_local = CkJsonObject::ckStringOf(jResp,"rates[i].amount_local")
currency_local = CkJsonObject::ckStringOf(jResp,"rates[i].currency_local")
provider = CkJsonObject::ckStringOf(jResp,"rates[i].provider")
provider_image_75 = CkJsonObject::ckStringOf(jResp,"rates[i].provider_image_75")
provider_image_200 = CkJsonObject::ckStringOf(jResp,"rates[i].provider_image_200")
servicelevelName = CkJsonObject::ckStringOf(jResp,"rates[i].servicelevel.name")
servicelevelToken = CkJsonObject::ckStringOf(jResp,"rates[i].servicelevel.token")
servicelevelTerms = CkJsonObject::ckStringOf(jResp,"rates[i].servicelevel.terms")
estimated_days = CkJsonObject::ckIntOf(jResp,"rates[i].estimated_days")
arrives_by = CkJsonObject::ckStringOf(jResp,"rates[i].arrives_by")
duration_terms = CkJsonObject::ckStringOf(jResp,"rates[i].duration_terms")
carrier_account = CkJsonObject::ckStringOf(jResp,"rates[i].carrier_account")
test = CkJsonObject::ckBoolOf(jResp,"rates[i].test")
zone = CkJsonObject::ckStringOf(jResp,"rates[i].zone")
j = 0
count_j = CkJsonObject::ckSizeOfArray(jResp,"rates[i].attributes")
While j < count_j
CkJsonObject::setCkJ(jResp, j)
strVal = CkJsonObject::ckStringOf(jResp,"rates[i].attributes[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,"messages")
While i < count_i
CkJsonObject::setCkI(jResp, i)
source = CkJsonObject::ckStringOf(jResp,"messages[i].source")
code = CkJsonObject::ckStringOf(jResp,"messages[i].code")
text = CkJsonObject::ckStringOf(jResp,"messages[i].text")
i = i + 1
Wend
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
CkHttpResponse::ckDispose(resp)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndProcedure