Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL oJsonResponse
LOCAL nId
LOCAL nProduct_id
LOCAL cTitle
LOCAL nInventory_item_id
LOCAL cAdmin_graphql_api_id
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oHttp := CreateObject("Chilkat.Http")
oHttp:Login := "SHOPIFY_PRIVATE_API_KEY"
oHttp:Password := "SHOPIFY_PRIVATE_API_KEY"
oHttp:Accept := "application/json"
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpNoBody("GET", "https://{shop}.myshopify.com/admin/api/2020-04/products/{product_id}/variants/{variant_id}.json", oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oHttp:destroy()
oResp:destroy()
RETURN
ENDIF
? "Response Status Code: " + Str(oResp:StatusCode)
oJsonResponse := CreateObject("Chilkat.JsonObject")
oJsonResponse:Load(oResp:BodyStr)
oJsonResponse:EmitCompact := 0
? oJsonResponse:Emit()
IF (oResp:StatusCode != 200)
? "Failed."
oHttp:destroy()
oResp:destroy()
oJsonResponse:destroy()
RETURN
ENDIF
// 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"
// }
//
nId := oJsonResponse:IntOf("id")
nProduct_id := oJsonResponse:IntOf("product_id")
cTitle := oJsonResponse:StringOf("title")
nInventory_item_id := oJsonResponse:IntOf("inventory_item_id")
cAdmin_graphql_api_id := oJsonResponse:StringOf("admin_graphql_api_id")
oHttp:destroy()
oResp:destroy()
oJsonResponse:destroy()