Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
http.login = "SHOPIFY_PRIVATE_API_KEY"
http.password = "SHOPIFY_PRIVATE_API_KEY"
http.accept = "application/json"
let resp = CkoHttpResponse()!
success = http.httpNoBody(verb: "GET", url: "https://{shop}.myshopify.com/admin/api/2020-04/products/{product_id}/variants/{variant_id}.json", response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
print("Response Status Code: \(resp.statusCode.intValue)")
let jsonResponse = CkoJsonObject()!
jsonResponse.load(json: resp.bodyStr)
jsonResponse.emitCompact = false
print("\(jsonResponse.emit()!)")
if resp.statusCode.intValue != 200 {
print("Failed.")
return
}
// 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"
// }
//
var id: Int = jsonResponse.int(of: "id").intValue
var product_id: Int = jsonResponse.int(of: "product_id").intValue
var title: String? = jsonResponse.string(of: "title")
var inventory_item_id: Int = jsonResponse.int(of: "inventory_item_id").intValue
var admin_graphql_api_id: String? = jsonResponse.string(of: "admin_graphql_api_id")
}