Sample code for 30+ languages & platforms
Lua

Shippo Create the Shipment, Get Rates, and Purchase Label

See more Shippo Examples

Demonstrates how retrieve rates and create labels for international shipments.

Chilkat Lua Downloads

Lua
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()

-- 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"
--     }
--   ]
-- }

local json = chilkat.JsonObject.new()
json:update_string("contents_type", "MERCHANDISE")
json:update_string("non_delivery_option", "RETURN")
json:update_bool("certify", true)
json:update_string("certify_signer", "Simon Kreuz")
json:update_string("incoterm", "DDU")
json:update_string("items[0].description", "T-shirt")
json:update_int("items[0].quantity", 20)
json:update_string("items[0].net_weight", "5")
json:update_string("items[0].mass_unit", "lb")
json:update_string("items[0].value_amount", "200")
json:update_string("items[0].value_currency", "USD")
json:update_string("items[0].tariff_number", "")
json:update_string("items[0].origin_country", "US")

http:set_request_header("Authorization", "ShippoToken <API_TOKEN>")
http:set_request_header("Content-Type", "application/json")

local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "https://api.goshippo.com/customs/declarations/", 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_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

local template = ""
local length = ""
local width = ""
local height = ""
local distance_unit = ""
local weight = ""
local mass_unit = ""
local value_amount = ""
local value_currency = ""
local test = false
local j = 0
local count_j = 0
local intVal = 0

local object_created = jResp:string_of("object_created")
local object_updated = jResp:string_of("object_updated")
local object_id = jResp:string_of("object_id")
local object_owner = jResp:string_of("object_owner")
local status = jResp:string_of("status")
local address_fromObject_id = jResp:string_of("address_from.object_id")
local address_fromIs_complete = jResp:bool_of("address_from.is_complete")
local address_fromCompany = jResp:string_of("address_from.company")
local address_fromStreet_no = jResp:string_of("address_from.street_no")
local address_fromName = jResp:string_of("address_from.name")
local address_fromStreet1 = jResp:string_of("address_from.street1")
local address_fromStreet2 = jResp:string_of("address_from.street2")
local address_fromCity = jResp:string_of("address_from.city")
local address_fromState = jResp:string_of("address_from.state")
local address_fromZip = jResp:string_of("address_from.zip")
local address_fromCountry = jResp:string_of("address_from.country")
local address_fromPhone = jResp:string_of("address_from.phone")
local address_fromEmail = jResp:string_of("address_from.email")
local address_fromIs_residential = jResp:string_of("address_from.is_residential")
local address_toObject_id = jResp:string_of("address_to.object_id")
local address_toIs_complete = jResp:bool_of("address_to.is_complete")
local address_toName = jResp:string_of("address_to.name")
local address_toStreet1 = jResp:string_of("address_to.street1")
local address_toCity = jResp:string_of("address_to.city")
local address_toState = jResp:string_of("address_to.state")
local address_toZip = jResp:string_of("address_to.zip")
local address_toCountry = jResp:string_of("address_to.country")
local address_toPhone = jResp:string_of("address_to.phone")
local address_toEmail = jResp:string_of("address_to.email")
local address_toIs_residential = jResp:bool_of("address_to.is_residential")
local address_return = jResp:string_of("address_return")
local shipment_date = jResp:string_of("shipment_date")
local extraInsuranceCurrency = jResp:string_of("extra.insurance.currency")
local extraInsuranceAmount = jResp:string_of("extra.insurance.amount")
local extraReference_1 = jResp:string_of("extra.reference_1")
local extraReference_2 = jResp:string_of("extra.reference_2")
local customs_declaration = jResp:string_of("customs_declaration")
local metadata = jResp:string_of("metadata")
local i = 0
local count_i = jResp:size_of_array("parcels")
while i < count_i do
    jResp.i = i
    object_id = jResp:string_of("parcels[i].object_id")
    object_created = jResp:string_of("parcels[i].object_created")
    object_updated = jResp:string_of("parcels[i].object_updated")
    object_owner = jResp:string_of("parcels[i].object_owner")
    template = jResp:string_of("parcels[i].template")
    length = jResp:string_of("parcels[i].length")
    width = jResp:string_of("parcels[i].width")
    height = jResp:string_of("parcels[i].height")
    distance_unit = jResp:string_of("parcels[i].distance_unit")
    weight = jResp:string_of("parcels[i].weight")
    mass_unit = jResp:string_of("parcels[i].mass_unit")
    value_amount = jResp:string_of("parcels[i].value_amount")
    value_currency = jResp:string_of("parcels[i].value_currency")
    metadata = jResp:string_of("parcels[i].metadata")
    test = jResp:bool_of("parcels[i].test")
    j = 0
    count_j = jResp:size_of_array("parcels[i].line_items")
    while j < count_j do
        jResp.j = j
        j = j + 1
    end

    i = i + 1
end

i = 0
count_i = jResp:size_of_array("rates")
while i < count_i do
    jResp.i = i
    intVal = jResp:int_of("rates[i]")
    j = 0
    count_j = jResp:size_of_array("rates[i].attributes")
    while j < count_j do
        jResp.j = j
        j = j + 1
    end

    j = 0
    count_j = jResp:size_of_array("rates[i].messages")
    while j < count_j do
        jResp.j = j
        j = j + 1
    end

    i = i + 1
end

i = 0
count_i = jResp:size_of_array("carrier_accounts")
while i < count_i do
    jResp.i = i
    i = i + 1
end

i = 0
count_i = jResp:size_of_array("messages")
while i < count_i do
    jResp.i = i
    i = i + 1
end