Xbase++
Xbase++
Shippo Create a Batch
See more Shippo Examples
Demonstrates how to create and purchase up to 10,000 shipments in a single API request.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oJson
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL cObject_id
LOCAL cObject_owner
LOCAL cStatus
LOCAL cObject_created
LOCAL cObject_updated
LOCAL cMetadata
LOCAL cDefault_carrier_account
LOCAL cDefault_servicelevel_token
LOCAL cLabel_filetype
LOCAL cBatch_shipmentsNext
LOCAL cBatch_shipmentsPrevious
LOCAL nObject_resultsPurchase_succeeded
LOCAL nObject_resultsPurchase_failed
LOCAL nObject_resultsCreation_failed
LOCAL nObject_resultsCreation_succeeded
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")
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "default_carrier_account": "078870331023437cb917f5187429b093",
// "default_servicelevel_token": "usps_priority",
// "label_filetype": "PDF_4x6",
// "metadata": "BATCH #170",
// "batch_shipments": [
// {
// "shipment": {
// "address_from": {
// "name": "Mr Hippo",
// "street1": "965 Mission St",
// "street2": "Ste 201",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US",
// "phone": "4151234567",
// "email": "mrhippo@goshippo.com"
// },
// "address_to": {
// "name": "Mrs Hippo",
// "company": "",
// "street1": "Broadway 1",
// "street2": "",
// "city": "New York",
// "state": "NY",
// "zip": "10007",
// "country": "US",
// "phone": "4151234567",
// "email": "mrshippo@goshippo.com"
// },
// "parcels": [
// {
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "2",
// "mass_unit": "oz"
// }
// ]
// }
// },
// {
// "shipment": {
// "address_from": {
// "name": "Mr Hippo",
// "street1": "1092 Indian Summer Ct",
// "city": "San Jose",
// "state": "CA",
// "zip": "95122",
// "country": "US",
// "phone": "4151234567",
// "email": "mrhippo@goshippo.com"
// },
// "address_to": {
// "name": "Mrs Hippo",
// "company": "",
// "street1": "Broadway 1",
// "street2": "",
// "city": "New York",
// "state": "NY",
// "zip": "10007",
// "country": "US",
// "phone": "4151234567",
// "email": "mrshippo@goshippo.com"
// },
// "parcels": [
// {
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "20",
// "mass_unit": "lb"
// }
// ]
// },
// "carrier_account": "a4391cd4ab974f478f55dc08b5c8e3b3",
// "servicelevel_token": "fedex_2_day"
// }
// ]
// }
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("default_carrier_account", "078870331023437cb917f5187429b093")
oJson:UpdateString("default_servicelevel_token", "usps_priority")
oJson:UpdateString("label_filetype", "PDF_4x6")
oJson:UpdateString("metadata", "BATCH #170")
oJson:UpdateString("batch_shipments[0].shipment.address_from.name", "Mr Hippo")
oJson:UpdateString("batch_shipments[0].shipment.address_from.street1", "965 Mission St")
oJson:UpdateString("batch_shipments[0].shipment.address_from.street2", "Ste 201")
oJson:UpdateString("batch_shipments[0].shipment.address_from.city", "San Francisco")
oJson:UpdateString("batch_shipments[0].shipment.address_from.state", "CA")
oJson:UpdateString("batch_shipments[0].shipment.address_from.zip", "94103")
oJson:UpdateString("batch_shipments[0].shipment.address_from.country", "US")
oJson:UpdateString("batch_shipments[0].shipment.address_from.phone", "4151234567")
oJson:UpdateString("batch_shipments[0].shipment.address_from.email", "mrhippo@goshippo.com")
oJson:UpdateString("batch_shipments[0].shipment.address_to.name", "Mrs Hippo")
oJson:UpdateString("batch_shipments[0].shipment.address_to.company", "")
oJson:UpdateString("batch_shipments[0].shipment.address_to.street1", "Broadway 1")
oJson:UpdateString("batch_shipments[0].shipment.address_to.street2", "")
oJson:UpdateString("batch_shipments[0].shipment.address_to.city", "New York")
oJson:UpdateString("batch_shipments[0].shipment.address_to.state", "NY")
oJson:UpdateString("batch_shipments[0].shipment.address_to.zip", "10007")
oJson:UpdateString("batch_shipments[0].shipment.address_to.country", "US")
oJson:UpdateString("batch_shipments[0].shipment.address_to.phone", "4151234567")
oJson:UpdateString("batch_shipments[0].shipment.address_to.email", "mrshippo@goshippo.com")
oJson:UpdateString("batch_shipments[0].shipment.parcels[0].length", "5")
oJson:UpdateString("batch_shipments[0].shipment.parcels[0].width", "5")
oJson:UpdateString("batch_shipments[0].shipment.parcels[0].height", "5")
oJson:UpdateString("batch_shipments[0].shipment.parcels[0].distance_unit", "in")
oJson:UpdateString("batch_shipments[0].shipment.parcels[0].weight", "2")
oJson:UpdateString("batch_shipments[0].shipment.parcels[0].mass_unit", "oz")
oJson:UpdateString("batch_shipments[1].shipment.address_from.name", "Mr Hippo")
oJson:UpdateString("batch_shipments[1].shipment.address_from.street1", "1092 Indian Summer Ct")
oJson:UpdateString("batch_shipments[1].shipment.address_from.city", "San Jose")
oJson:UpdateString("batch_shipments[1].shipment.address_from.state", "CA")
oJson:UpdateString("batch_shipments[1].shipment.address_from.zip", "95122")
oJson:UpdateString("batch_shipments[1].shipment.address_from.country", "US")
oJson:UpdateString("batch_shipments[1].shipment.address_from.phone", "4151234567")
oJson:UpdateString("batch_shipments[1].shipment.address_from.email", "mrhippo@goshippo.com")
oJson:UpdateString("batch_shipments[1].shipment.address_to.name", "Mrs Hippo")
oJson:UpdateString("batch_shipments[1].shipment.address_to.company", "")
oJson:UpdateString("batch_shipments[1].shipment.address_to.street1", "Broadway 1")
oJson:UpdateString("batch_shipments[1].shipment.address_to.street2", "")
oJson:UpdateString("batch_shipments[1].shipment.address_to.city", "New York")
oJson:UpdateString("batch_shipments[1].shipment.address_to.state", "NY")
oJson:UpdateString("batch_shipments[1].shipment.address_to.zip", "10007")
oJson:UpdateString("batch_shipments[1].shipment.address_to.country", "US")
oJson:UpdateString("batch_shipments[1].shipment.address_to.phone", "4151234567")
oJson:UpdateString("batch_shipments[1].shipment.address_to.email", "mrshippo@goshippo.com")
oJson:UpdateString("batch_shipments[1].shipment.parcels[0].length", "5")
oJson:UpdateString("batch_shipments[1].shipment.parcels[0].width", "5")
oJson:UpdateString("batch_shipments[1].shipment.parcels[0].height", "5")
oJson:UpdateString("batch_shipments[1].shipment.parcels[0].distance_unit", "in")
oJson:UpdateString("batch_shipments[1].shipment.parcels[0].weight", "20")
oJson:UpdateString("batch_shipments[1].shipment.parcels[0].mass_unit", "lb")
oJson:UpdateString("batch_shipments[1].carrier_account", "a4391cd4ab974f478f55dc08b5c8e3b3")
oJson:UpdateString("batch_shipments[1].servicelevel_token", "fedex_2_day")
oHttp:SetRequestHeader("Authorization", "ShippoToken shippo_test_3af5d574e2f845d30efcaf9b2f47d9c2aef4807a")
oHttp:SetRequestHeader("Content-Type", "application/json")
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://api.goshippo.com/batches/", 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_id": "a015eb693cca465dbb6523ce6d2e3c65",
// "object_owner": "admin",
// "status": "VALIDATING",
// "object_created": "2016-09-12T15:25:43.465Z",
// "object_updated": "2016-09-12T15:25:43.465Z",
// "metadata": "BATCH #170",
// "default_carrier_account": "078870331023437cb917f5187429b093",
// "default_servicelevel_token": "usps_priority",
// "label_filetype": "PDF_4x6",
// "batch_shipments": {
// "next": null,
// "previous": null,
// "results": [
// ]
// },
// "object_results": {
// "purchase_succeeded": 0,
// "purchase_failed": 0,
// "creation_failed": 0,
// "creation_succeeded": 0
// },
// "label_url": [
// ]
// }
// 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_id := oJResp:StringOf("object_id")
cObject_owner := oJResp:StringOf("object_owner")
cStatus := oJResp:StringOf("status")
cObject_created := oJResp:StringOf("object_created")
cObject_updated := oJResp:StringOf("object_updated")
cMetadata := oJResp:StringOf("metadata")
cDefault_carrier_account := oJResp:StringOf("default_carrier_account")
cDefault_servicelevel_token := oJResp:StringOf("default_servicelevel_token")
cLabel_filetype := oJResp:StringOf("label_filetype")
cBatch_shipmentsNext := oJResp:StringOf("batch_shipments.next")
cBatch_shipmentsPrevious := oJResp:StringOf("batch_shipments.previous")
nObject_resultsPurchase_succeeded := oJResp:IntOf("object_results.purchase_succeeded")
nObject_resultsPurchase_failed := oJResp:IntOf("object_results.purchase_failed")
nObject_resultsCreation_failed := oJResp:IntOf("object_results.creation_failed")
nObject_resultsCreation_succeeded := oJResp:IntOf("object_results.creation_succeeded")
i := 0
nCount_i := oJResp:SizeOfArray("batch_shipments.results")
DO WHILE i < nCount_i
oJResp:I := i
i := i + 1
ENDDO
i := 0
nCount_i := oJResp:SizeOfArray("label_url")
DO WHILE i < nCount_i
oJResp:I := i
i := i + 1
ENDDO
oHttp:destroy()
oJson:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()