Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Shopify Query a variant for its inventory item ID

See more Shopify Examples

Query a product variant to find the ID of its inventory item.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local http = chilkat.Http.new()

http.login = "SHOPIFY_PRIVATE_API_KEY"
http.password = "SHOPIFY_PRIVATE_API_KEY"

http.accept = "application/json"

local resp = chilkat.HttpResponse.new()
local success = http:http_no_body("GET", "https://{shop}.myshopify.com/admin/api/2020-04/products/{product_id}/variants/{variant_id}.json", resp)
if not success then
    print(http.last_error_text)
    return
end

print("Response Status Code: " .. resp.status_code)

local jsonResponse = chilkat.JsonObject.new()
jsonResponse:load(resp.body_str)
jsonResponse.emit_compact = false
print(jsonResponse:emit())

if resp.status_code ~= 200 then
    print("Failed.")
    return
end

-- Sample output...
-- (See the parsing code below..)
--
-- Use the this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON

-- {
--   "id": 12195009364024,
--   "product_id": 1321541042232,
--   "title": "xs",
-- ...
--   "inventory_item_id": 12250274365496,
-- ...
--   "admin_graphql_api_id": "gid://shopify/ProductVariant/12195009364024"
-- }
--

local id = jsonResponse:int_of("id")
local product_id = jsonResponse:int_of("product_id")
local title = jsonResponse:string_of("title")
local inventory_item_id = jsonResponse:int_of("inventory_item_id")
local admin_graphql_api_id = jsonResponse:string_of("admin_graphql_api_id")