Lua
Lua
Shippo Create Customs Declaration
See more Shippo Examples
Demonstrates how to create a customs declaration and send a POST request with the necessary information to the Customs Declarations endpoint.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()
-- 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": "2019-07-04T16:00:29.043Z",
-- "object_updated": "2019-07-04T16:00:29.043Z",
-- "object_id": "4a1e6ab7b1ba49ed9bc6cb1a8798e0fd",
-- "object_owner": "admin@chilkatsoft.com",
-- "object_state": "VALID",
-- "address_importer": null,
-- "certify_signer": "Simon Kreuz",
-- "certify": true,
-- "items": [
-- "4096c68693364b7ea0af72fb869ee861"
-- ],
-- "non_delivery_option": "RETURN",
-- "contents_type": "MERCHANDISE",
-- "contents_explanation": "",
-- "exporter_reference": "",
-- "importer_reference": "",
-- "invoice": "",
-- "commercial_invoice": false,
-- "license": "",
-- "certificate": "",
-- "notes": "",
-- "eel_pfc": "",
-- "aes_itn": "",
-- "disclaimer": "",
-- "incoterm": "DDU",
-- "metadata": "",
-- "test": true,
-- "duties_payor": 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
local strVal = ""
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 object_state = jResp:string_of("object_state")
local address_importer = jResp:string_of("address_importer")
local certify_signer = jResp:string_of("certify_signer")
local certify = jResp:bool_of("certify")
local non_delivery_option = jResp:string_of("non_delivery_option")
local contents_type = jResp:string_of("contents_type")
local contents_explanation = jResp:string_of("contents_explanation")
local exporter_reference = jResp:string_of("exporter_reference")
local importer_reference = jResp:string_of("importer_reference")
local invoice = jResp:string_of("invoice")
local commercial_invoice = jResp:bool_of("commercial_invoice")
local license = jResp:string_of("license")
local certificate = jResp:string_of("certificate")
local notes = jResp:string_of("notes")
local eel_pfc = jResp:string_of("eel_pfc")
local aes_itn = jResp:string_of("aes_itn")
local disclaimer = jResp:string_of("disclaimer")
local incoterm = jResp:string_of("incoterm")
local metadata = jResp:string_of("metadata")
local test = jResp:bool_of("test")
local duties_payor = jResp:string_of("duties_payor")
local i = 0
local count_i = jResp:size_of_array("items")
while i < count_i do
jResp.i = i
strVal = jResp:string_of("items[i]")
i = i + 1
end