Lua
Lua
Shippo Create Shipment Object
See more Shippo Examples
Create a Shipment object to retrieve rates. It represents a request to ship a given package from the sender to the recipient address.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/shipments/ \
-- -H "Authorization: ShippoToken <API_TOKEN>" \
-- -H "Content-Type: application/json" \
-- -d '{
-- "address_from":{
-- "name":"Mr. Hippo",
-- "street1":"215 Clayton St.",
-- "city":"San Francisco",
-- "state":"CA",
-- "zip":"94117",
-- "country":"US"
-- },
-- "address_to":{
-- "name":"Mrs. Hippo",
-- "street1":"965 Mission St.",
-- "city":"San Francisco",
-- "state":"CA",
-- "zip":"94105",
-- "country":"US"
-- },
-- "parcels":[{
-- "length":"5",
-- "width":"5",
-- "height":"5",
-- "distance_unit":"in",
-- "weight":"2",
-- "mass_unit":"lb"
-- }],
-- "async": false
-- }'
-- Use this online tool to generate code from sample JSON:
-- Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "address_from": {
-- "name": "Mr. Hippo",
-- "street1": "215 Clayton St.",
-- "city": "San Francisco",
-- "state": "CA",
-- "zip": "94117",
-- "country": "US"
-- },
-- "address_to": {
-- "name": "Mrs. Hippo",
-- "street1": "965 Mission St.",
-- "city": "San Francisco",
-- "state": "CA",
-- "zip": "94105",
-- "country": "US"
-- },
-- "parcels": [
-- {
-- "length": "5",
-- "width": "5",
-- "height": "5",
-- "distance_unit": "in",
-- "weight": "2",
-- "mass_unit": "lb"
-- }
-- ],
-- "async": false
-- }
local json = chilkat.JsonObject.new()
json:update_string("address_from.name", "Mr. Hippo")
json:update_string("address_from.street1", "215 Clayton St.")
json:update_string("address_from.city", "San Francisco")
json:update_string("address_from.state", "CA")
json:update_string("address_from.zip", "94117")
json:update_string("address_from.country", "US")
json:update_string("address_to.name", "Mrs. Hippo")
json:update_string("address_to.street1", "965 Mission St.")
json:update_string("address_to.city", "San Francisco")
json:update_string("address_to.state", "CA")
json:update_string("address_to.zip", "94105")
json:update_string("address_to.country", "US")
json:update_string("parcels[0].length", "5")
json:update_string("parcels[0].width", "5")
json:update_string("parcels[0].height", "5")
json:update_string("parcels[0].distance_unit", "in")
json:update_string("parcels[0].weight", "2")
json:update_string("parcels[0].mass_unit", "lb")
json:update_bool("async", false)
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/shipments/", 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)
-- {
-- "carrier_accounts": [
-- ],
-- "object_created": "2019-06-28T17:45:20.441Z",
-- "object_updated": "2019-06-28T17:45:20.511Z",
-- "object_id": "17b01795a4884d8a9002ccc17c9d09d1",
-- "object_owner": "admin@chilkatsoft.com",
-- "status": "SUCCESS",
-- "address_from": {
-- "object_id": "427489906b604f498f7bd2429ab7d9a1",
-- "is_complete": true,
-- "name": "Mr. Hippo",
-- "company": "",
-- "street_no": "",
-- "street1": "215 Clayton St.",
-- "validation_results": {},
-- "street2": "",
-- "street3": "",
-- "city": "San Francisco",
-- "state": "CA",
-- "zip": "94117",
-- "country": "US",
-- "phone": "",
-- "email": "",
-- "is_residential": null,
-- "test": true
-- },
-- "address_to": {
-- "object_id": "3d7166269a2844b7842ef9ace942ec7c",
-- "is_complete": true,
-- "name": "Mrs. Hippo",
-- "company": "",
-- "street_no": "",
-- "street1": "965 Mission St.",
-- "validation_results": {},
-- "street2": "",
-- "street3": "",
-- "city": "San Francisco",
-- "state": "CA",
-- "zip": "94105",
-- "country": "US",
-- "phone": "",
-- "email": "",
-- "is_residential": null,
-- "test": true
-- },
-- "parcels": [
-- {
-- "object_state": "VALID",
-- "object_created": "2019-06-28T17:45:20.411Z",
-- "object_updated": "2019-06-28T17:45:20.455Z",
-- "object_id": "5af766ff15684a4186b0e3c833348fac",
-- "object_owner": "admin@chilkatsoft.com",
-- "template": null,
-- "extra": {},
-- "length": "5.0000",
-- "width": "5.0000",
-- "height": "5.0000",
-- "distance_unit": "in",
-- "weight": "2.0000",
-- "mass_unit": "lb",
-- "value_amount": null,
-- "value_currency": null,
-- "metadata": "",
-- "line_items": [
-- ],
-- "test": true
-- }
-- ],
-- "shipment_date": "2019-06-28T17:45:20.511Z",
-- "address_return": {
-- "object_id": "427489906b604f498f7bd2429ab7d9a1",
-- "is_complete": true,
-- "name": "Mr. Hippo",
-- "company": "",
-- "street_no": "",
-- "street1": "215 Clayton St.",
-- "validation_results": {},
-- "street2": "",
-- "street3": "",
-- "city": "San Francisco",
-- "state": "CA",
-- "zip": "94117",
-- "country": "US",
-- "phone": "",
-- "email": "",
-- "is_residential": null,
-- "test": true
-- },
-- "alternate_address_to": null,
-- "customs_declaration": null,
-- "extra": {},
-- "rates": [
-- {
-- "object_created": "2019-06-28T17:45:20.789Z",
-- "object_id": "dd7556c284e8444294d2ab7099e662d2",
-- "object_owner": "admin@chilkatsoft.com",
-- "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
-- "attributes": [
-- "FASTEST"
-- ],
-- "amount": "22.78",
-- "currency": "USD",
-- "amount_local": "22.78",
-- "currency_local": "USD",
-- "provider": "USPS",
-- "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
-- "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
-- "servicelevel": {
-- "name": "Priority Mail Express",
-- "token": "usps_priority_express",
-- "terms": ""
-- },
-- "estimated_days": 2,
-- "arrives_by": null,
-- "duration_terms": "Overnight delivery to most U.S. locations.",
-- "messages": [
-- ],
-- "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
-- "test": true,
-- "zone": "1"
-- },
-- {
-- "object_created": "2019-06-28T17:45:20.786Z",
-- "object_id": "ec11cc297e2f4583986097f6d409e5c6",
-- "object_owner": "admin@chilkatsoft.com",
-- "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
-- "attributes": [
-- "BESTVALUE",
-- "CHEAPEST",
-- "FASTEST"
-- ],
-- "amount": "6.95",
-- "currency": "USD",
-- "amount_local": "6.95",
-- "currency_local": "USD",
-- "provider": "USPS",
-- "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
-- "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
-- "servicelevel": {
-- "name": "Priority Mail",
-- "token": "usps_priority",
-- "terms": ""
-- },
-- "estimated_days": 2,
-- "arrives_by": null,
-- "duration_terms": "Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.",
-- "messages": [
-- ],
-- "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
-- "test": true,
-- "zone": "1"
-- },
-- {
-- "object_created": "2019-06-28T17:45:20.785Z",
-- "object_id": "d256586f539f4c93a2f02cc11f4942ac",
-- "object_owner": "admin@chilkatsoft.com",
-- "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
-- "attributes": [
-- ],
-- "amount": "7.32",
-- "currency": "USD",
-- "amount_local": "7.32",
-- "currency_local": "USD",
-- "provider": "USPS",
-- "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
-- "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
-- "servicelevel": {
-- "name": "Parcel Select",
-- "token": "usps_parcel_select",
-- "terms": ""
-- },
-- "estimated_days": 7,
-- "arrives_by": null,
-- "duration_terms": "Delivery in 2 to 8 days.",
-- "messages": [
-- ],
-- "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
-- "test": true,
-- "zone": "1"
-- }
-- ],
-- "messages": [
-- {
-- "source": "DHLExpress",
-- "code": "",
-- "text": "Shippo's DHL Express master account doesn't support shipments to inside of the US"
-- }
-- ],
-- "metadata": "",
-- "test": true,
-- "order": 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 object_state = ""
local template = ""
local length = ""
local width = ""
local height = ""
local distance_unit = ""
local weight = ""
local mass_unit = ""
local value_amount = ""
local value_currency = ""
local j = 0
local count_j = 0
local shipment = ""
local amount = ""
local currency = ""
local amount_local = ""
local currency_local = ""
local provider = ""
local provider_image_75 = ""
local provider_image_200 = ""
local servicelevelName = ""
local servicelevelToken = ""
local servicelevelTerms = ""
local estimated_days = 0
local arrives_by = ""
local duration_terms = ""
local carrier_account = ""
local zone = ""
local strVal = ""
local source = ""
local code = ""
local text = ""
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_fromName = jResp:string_of("address_from.name")
local address_fromCompany = jResp:string_of("address_from.company")
local address_fromStreet_no = jResp:string_of("address_from.street_no")
local address_fromStreet1 = jResp:string_of("address_from.street1")
local address_fromStreet2 = jResp:string_of("address_from.street2")
local address_fromStreet3 = jResp:string_of("address_from.street3")
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_fromTest = jResp:bool_of("address_from.test")
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_toCompany = jResp:string_of("address_to.company")
local address_toStreet_no = jResp:string_of("address_to.street_no")
local address_toStreet1 = jResp:string_of("address_to.street1")
local address_toStreet2 = jResp:string_of("address_to.street2")
local address_toStreet3 = jResp:string_of("address_to.street3")
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:string_of("address_to.is_residential")
local address_toTest = jResp:bool_of("address_to.test")
local shipment_date = jResp:string_of("shipment_date")
local address_returnObject_id = jResp:string_of("address_return.object_id")
local address_returnIs_complete = jResp:bool_of("address_return.is_complete")
local address_returnName = jResp:string_of("address_return.name")
local address_returnCompany = jResp:string_of("address_return.company")
local address_returnStreet_no = jResp:string_of("address_return.street_no")
local address_returnStreet1 = jResp:string_of("address_return.street1")
local address_returnStreet2 = jResp:string_of("address_return.street2")
local address_returnStreet3 = jResp:string_of("address_return.street3")
local address_returnCity = jResp:string_of("address_return.city")
local address_returnState = jResp:string_of("address_return.state")
local address_returnZip = jResp:string_of("address_return.zip")
local address_returnCountry = jResp:string_of("address_return.country")
local address_returnPhone = jResp:string_of("address_return.phone")
local address_returnEmail = jResp:string_of("address_return.email")
local address_returnIs_residential = jResp:string_of("address_return.is_residential")
local address_returnTest = jResp:bool_of("address_return.test")
local alternate_address_to = jResp:string_of("alternate_address_to")
local customs_declaration = jResp:string_of("customs_declaration")
local metadata = jResp:string_of("metadata")
local test = jResp:bool_of("test")
local order = jResp:string_of("order")
local i = 0
local 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("parcels")
while i < count_i do
jResp.i = i
object_state = jResp:string_of("parcels[i].object_state")
object_created = jResp:string_of("parcels[i].object_created")
object_updated = jResp:string_of("parcels[i].object_updated")
object_id = jResp:string_of("parcels[i].object_id")
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
object_created = jResp:string_of("rates[i].object_created")
object_id = jResp:string_of("rates[i].object_id")
object_owner = jResp:string_of("rates[i].object_owner")
shipment = jResp:string_of("rates[i].shipment")
amount = jResp:string_of("rates[i].amount")
currency = jResp:string_of("rates[i].currency")
amount_local = jResp:string_of("rates[i].amount_local")
currency_local = jResp:string_of("rates[i].currency_local")
provider = jResp:string_of("rates[i].provider")
provider_image_75 = jResp:string_of("rates[i].provider_image_75")
provider_image_200 = jResp:string_of("rates[i].provider_image_200")
servicelevelName = jResp:string_of("rates[i].servicelevel.name")
servicelevelToken = jResp:string_of("rates[i].servicelevel.token")
servicelevelTerms = jResp:string_of("rates[i].servicelevel.terms")
estimated_days = jResp:int_of("rates[i].estimated_days")
arrives_by = jResp:string_of("rates[i].arrives_by")
duration_terms = jResp:string_of("rates[i].duration_terms")
carrier_account = jResp:string_of("rates[i].carrier_account")
test = jResp:bool_of("rates[i].test")
zone = jResp:string_of("rates[i].zone")
j = 0
count_j = jResp:size_of_array("rates[i].attributes")
while j < count_j do
jResp.j = j
strVal = jResp:string_of("rates[i].attributes[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("messages")
while i < count_i do
jResp.i = i
source = jResp:string_of("messages[i].source")
code = jResp:string_of("messages[i].code")
text = jResp:string_of("messages[i].text")
i = i + 1
end