Sample code for 30+ languages & platforms
Lua

Shippo Validate Address

See more Shippo Examples

Demonstrates how to validate your addresses through the Shippo API before you create labels.

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/addresses/ \
--     -H "Authorization: ShippoToken <API_TOKEN>" \
--     -d name="Shawn Ippotle" \
--     -d company="Shippo" \
--     -d street1="215 Clayton St." \
--     -d city="San Francisco" \
--     -d state="CA" \
--     -d zip=94117 \
--     -d country="US" \
--     -d email="shippotle@goshippo.com"\
--     -d validate=true

local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = "/addresses/"
req.content_type = "application/x-www-form-urlencoded"
req:add_param("name", "Shawn Ippotle")
req:add_param("company", "Shippo")
req:add_param("street1", "215 Clayton St.")
req:add_param("city", "San Francisco")
req:add_param("state", "CA")
req:add_param("zip", "94117")
req:add_param("country", "US")
req:add_param("email", "shippotle@goshippo.com")
req:add_param("validate", "true")

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

local resp = chilkat.HttpResponse.new()
local success = http:http_req("https://api.goshippo.com/addresses/", 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_created": "2015-03-30T23:47:11.574Z",
--   "object_updated": "2015-03-30T23:47:11.596Z",
--   "object_id": "67183b2e81e9421f894bfbcdc4236b16",
--   "is_complete": false,
--   "validation_results": {
--     "is_valid": false,
--     "messages": [
--       {
--         "source": "USPS",
--         "code": "Address Not Found",
--         "type": "address_error",
--         "text": "The address as submitted could not be found. Please check for excessive abbreviations in the street address line or in the City name."
--       }
--     ]
--   },
--   "object_owner": "shippotle@goshippo.com",
--   "name": "Shawn Ippotle",
--   "company": "Shippo",
--   "street_no": "",
--   "street1": "215 HIPPO ST.",
--   "street2": "",
--   "city": "SAN FRANCISCO",
--   "state": "CA",
--   "zip": "94107",
--   "country": "US",
--   "phone": ""
-- }

-- 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 source = ""
local code = ""
local v_type = ""
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 is_complete = jResp:bool_of("is_complete")
local validation_resultsIs_valid = jResp:bool_of("validation_results.is_valid")
local object_owner = jResp:string_of("object_owner")
local name = jResp:string_of("name")
local company = jResp:string_of("company")
local street_no = jResp:string_of("street_no")
local street1 = jResp:string_of("street1")
local street2 = jResp:string_of("street2")
local city = jResp:string_of("city")
local state = jResp:string_of("state")
local zip = jResp:string_of("zip")
local country = jResp:string_of("country")
local phone = jResp:string_of("phone")
local i = 0
local count_i = jResp:size_of_array("validation_results.messages")
while i < count_i do
    jResp.i = i
    source = jResp:string_of("validation_results.messages[i].source")
    code = jResp:string_of("validation_results.messages[i].code")
    v_type = jResp:string_of("validation_results.messages[i].type")
    text = jResp:string_of("validation_results.messages[i].text")
    i = i + 1
end