Sample code for 30+ languages & platforms
Lua

Etsy: Get the Inventory for a Listing

See more Etsy Examples

Gets the inventory for a listing.

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 -X GET \
--   https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING

local sbResponseBody = chilkat.StringBuilder.new()
local success = http:quick_get_sb("https://openapi.etsy.com/v2/listings/listing_id/inventory?api_key=MY_ETSY_KEYSTRING", sbResponseBody)
if not success then
    print(http.last_error_text)
    return
end

local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false

print("Response Body:")
print(jResp:emit())

local respStatusCode = http.last_status
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
    print("Response Header:")
    print(http.last_header)
    print("Failed.")
    return
end

-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)

-- {
--   "count": 1,
--   "results": {
--     "products": [
--       {
--         "product_id": 3361120103,
--         "property_values": [
--         ],
--         "offerings": [
--           {
--             "offering_id": 3579642570,
--             "price": {
--               "amount": 16000,
--               "divisor": 100,
--               "currency_code": "USD",
--               "currency_formatted_short": "$160.00",
--               "currency_formatted_long": "$160.00 USD",
--               "currency_formatted_raw": "160.00"
--             },
--             "quantity": 1
--           }
--         ]
--       }
--     ]
--   },
--   "params": {
--     "listing_id": "720138253",
--     "write_missing_inventory": false
--   },
--   "type": "ListingInventory",
--   "pagination": {}
-- }

-- 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 product_id = 0
local j = 0
local count_j = 0
local offering_id = 0
local priceAmount = 0
local priceDivisor = 0
local priceCurrency_code = ""
local priceCurrency_formatted_short = ""
local priceCurrency_formatted_long = ""
local priceCurrency_formatted_raw = ""
local quantity = 0

local count = jResp:int_of("count")
local paramsListing_id = jResp:string_of("params.listing_id")
local paramsWrite_missing_inventory = jResp:bool_of("params.write_missing_inventory")
local v_type = jResp:string_of("type")
local i = 0
local count_i = jResp:size_of_array("results.products")
while i < count_i do
    jResp.i = i
    product_id = jResp:int_of("results.products[i].product_id")
    j = 0
    count_j = jResp:size_of_array("results.products[i].property_values")
    while j < count_j do
        jResp.j = j
        j = j + 1
    end

    j = 0
    count_j = jResp:size_of_array("results.products[i].offerings")
    while j < count_j do
        jResp.j = j
        offering_id = jResp:int_of("results.products[i].offerings[j].offering_id")
        priceAmount = jResp:int_of("results.products[i].offerings[j].price.amount")
        priceDivisor = jResp:int_of("results.products[i].offerings[j].price.divisor")
        priceCurrency_code = jResp:string_of("results.products[i].offerings[j].price.currency_code")
        priceCurrency_formatted_short = jResp:string_of("results.products[i].offerings[j].price.currency_formatted_short")
        priceCurrency_formatted_long = jResp:string_of("results.products[i].offerings[j].price.currency_formatted_long")
        priceCurrency_formatted_raw = jResp:string_of("results.products[i].offerings[j].price.currency_formatted_raw")
        quantity = jResp:int_of("results.products[i].offerings[j].quantity")
        j = j + 1
    end

    i = i + 1
end