Sample code for 30+ languages & platforms
Lua

Shippo Create Transaction Object

See more Shippo Examples

Creates a Transaction object that represents a shipping label purchase and is based on the shipping rate.

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/transactions \
--     -H "Authorization: ShippoToken <API_TOKEN>" \
--     -d rate="478fb066930e4e3195a3686f7ea5b612"
--     -d label_file_type="PDF"
--     -d async=false

local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = "/transactions"
req.content_type = "application/x-www-form-urlencoded"
req:add_param("rate", "478fb066930e4e3195a3686f7ea5b612")
req:add_param("label_file_type", "PDF")
req:add_param("async", "false")

req:add_header("Authorization", "ShippoToken <API_TOKEN>")

local resp = chilkat.HttpResponse.new()
local success = http:http_req("https://api.goshippo.com/transactions", req, 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_state": "VALID",
--   "status": "SUCCESS",
--   "object_created": "2019-06-28T18:04:08.679Z",
--   "object_updated": "2019-06-28T18:04:10.516Z",
--   "object_id": "6178e155da304f10aa4b4271fdca84fb",
--   "object_owner": "admin@chilkatsoft.com",
--   "test": true,
--   "rate": "dd7556c284e8444294d2ab7099e662d2",
--   "tracking_number": "92701902416755000000000015",
--   "tracking_status": "UNKNOWN",
--   "eta": null,
--   "tracking_url_provider": "https://tools.usps.com/go/TrackConfirmAction_input?origTrackNum=92701902416755000000000015",
--   "label_url": "https://shippo-delivery-east.s3.amazonaws.com/6178e155da304f10aa4b4271fdca84fb.pdf?Signature=1LU0Qf11nQJGei%2FeLZr%2BQ5EwJS4%3D&Expires=1593281050&AWSAccessKeyId=AKIAJGLCC5MYLLWIG42A",
--   "commercial_invoice_url": null,
--   "messages": [
--   ],
--   "order": null,
--   "metadata": "",
--   "parcel": "5af766ff15684a4186b0e3c833348fac",
--   "billing": {
--     "payments": [
--     ]
--   }
-- }

-- 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_state = jResp:string_of("object_state")
local status = jResp:string_of("status")
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 test = jResp:bool_of("test")
local rate = jResp:string_of("rate")
local tracking_number = jResp:string_of("tracking_number")
local tracking_status = jResp:string_of("tracking_status")
local eta = jResp:string_of("eta")
local tracking_url_provider = jResp:string_of("tracking_url_provider")
local label_url = jResp:string_of("label_url")
local commercial_invoice_url = jResp:string_of("commercial_invoice_url")
local order = jResp:string_of("order")
local metadata = jResp:string_of("metadata")
local parcel = jResp:string_of("parcel")
local i = 0
local count_i = jResp:size_of_array("messages")
while i < count_i do
    jResp.i = i
    i = i + 1
end

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