Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- 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"
-- }
-- ]
-- }
local json = chilkat.JsonObject.new()
json:update_string("default_carrier_account", "078870331023437cb917f5187429b093")
json:update_string("default_servicelevel_token", "usps_priority")
json:update_string("label_filetype", "PDF_4x6")
json:update_string("metadata", "BATCH #170")
json:update_string("batch_shipments[0].shipment.address_from.name", "Mr Hippo")
json:update_string("batch_shipments[0].shipment.address_from.street1", "965 Mission St")
json:update_string("batch_shipments[0].shipment.address_from.street2", "Ste 201")
json:update_string("batch_shipments[0].shipment.address_from.city", "San Francisco")
json:update_string("batch_shipments[0].shipment.address_from.state", "CA")
json:update_string("batch_shipments[0].shipment.address_from.zip", "94103")
json:update_string("batch_shipments[0].shipment.address_from.country", "US")
json:update_string("batch_shipments[0].shipment.address_from.phone", "4151234567")
json:update_string("batch_shipments[0].shipment.address_from.email", "mrhippo@goshippo.com")
json:update_string("batch_shipments[0].shipment.address_to.name", "Mrs Hippo")
json:update_string("batch_shipments[0].shipment.address_to.company", "")
json:update_string("batch_shipments[0].shipment.address_to.street1", "Broadway 1")
json:update_string("batch_shipments[0].shipment.address_to.street2", "")
json:update_string("batch_shipments[0].shipment.address_to.city", "New York")
json:update_string("batch_shipments[0].shipment.address_to.state", "NY")
json:update_string("batch_shipments[0].shipment.address_to.zip", "10007")
json:update_string("batch_shipments[0].shipment.address_to.country", "US")
json:update_string("batch_shipments[0].shipment.address_to.phone", "4151234567")
json:update_string("batch_shipments[0].shipment.address_to.email", "mrshippo@goshippo.com")
json:update_string("batch_shipments[0].shipment.parcels[0].length", "5")
json:update_string("batch_shipments[0].shipment.parcels[0].width", "5")
json:update_string("batch_shipments[0].shipment.parcels[0].height", "5")
json:update_string("batch_shipments[0].shipment.parcels[0].distance_unit", "in")
json:update_string("batch_shipments[0].shipment.parcels[0].weight", "2")
json:update_string("batch_shipments[0].shipment.parcels[0].mass_unit", "oz")
json:update_string("batch_shipments[1].shipment.address_from.name", "Mr Hippo")
json:update_string("batch_shipments[1].shipment.address_from.street1", "1092 Indian Summer Ct")
json:update_string("batch_shipments[1].shipment.address_from.city", "San Jose")
json:update_string("batch_shipments[1].shipment.address_from.state", "CA")
json:update_string("batch_shipments[1].shipment.address_from.zip", "95122")
json:update_string("batch_shipments[1].shipment.address_from.country", "US")
json:update_string("batch_shipments[1].shipment.address_from.phone", "4151234567")
json:update_string("batch_shipments[1].shipment.address_from.email", "mrhippo@goshippo.com")
json:update_string("batch_shipments[1].shipment.address_to.name", "Mrs Hippo")
json:update_string("batch_shipments[1].shipment.address_to.company", "")
json:update_string("batch_shipments[1].shipment.address_to.street1", "Broadway 1")
json:update_string("batch_shipments[1].shipment.address_to.street2", "")
json:update_string("batch_shipments[1].shipment.address_to.city", "New York")
json:update_string("batch_shipments[1].shipment.address_to.state", "NY")
json:update_string("batch_shipments[1].shipment.address_to.zip", "10007")
json:update_string("batch_shipments[1].shipment.address_to.country", "US")
json:update_string("batch_shipments[1].shipment.address_to.phone", "4151234567")
json:update_string("batch_shipments[1].shipment.address_to.email", "mrshippo@goshippo.com")
json:update_string("batch_shipments[1].shipment.parcels[0].length", "5")
json:update_string("batch_shipments[1].shipment.parcels[0].width", "5")
json:update_string("batch_shipments[1].shipment.parcels[0].height", "5")
json:update_string("batch_shipments[1].shipment.parcels[0].distance_unit", "in")
json:update_string("batch_shipments[1].shipment.parcels[0].weight", "20")
json:update_string("batch_shipments[1].shipment.parcels[0].mass_unit", "lb")
json:update_string("batch_shipments[1].carrier_account", "a4391cd4ab974f478f55dc08b5c8e3b3")
json:update_string("batch_shipments[1].servicelevel_token", "fedex_2_day")
http:set_request_header("Authorization", "ShippoToken shippo_test_3af5d574e2f845d30efcaf9b2f47d9c2aef4807a")
http:set_request_header("Content-Type", "application/json")
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "https://api.goshippo.com/batches/", json, "application/json", resp)
if not success then
print(http.last_error_text)
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(resp.header)
print("Failed.")
return
end
-- 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
local object_id = jResp:string_of("object_id")
local object_owner = jResp:string_of("object_owner")
local status = jResp:string_of("status")
local object_created = jResp:string_of("object_created")
local object_updated = jResp:string_of("object_updated")
local metadata = jResp:string_of("metadata")
local default_carrier_account = jResp:string_of("default_carrier_account")
local default_servicelevel_token = jResp:string_of("default_servicelevel_token")
local label_filetype = jResp:string_of("label_filetype")
local batch_shipmentsNext = jResp:string_of("batch_shipments.next")
local batch_shipmentsPrevious = jResp:string_of("batch_shipments.previous")
local object_resultsPurchase_succeeded = jResp:int_of("object_results.purchase_succeeded")
local object_resultsPurchase_failed = jResp:int_of("object_results.purchase_failed")
local object_resultsCreation_failed = jResp:int_of("object_results.creation_failed")
local object_resultsCreation_succeeded = jResp:int_of("object_results.creation_succeeded")
local i = 0
local count_i = jResp:size_of_array("batch_shipments.results")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("label_url")
while i < count_i do
jResp.i = i
i = i + 1
end