Sample code for 30+ languages & platforms
Rust 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 Rust Downloads

Rust

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

let http = chilkat::Http::new();

http.set_login("SHOPIFY_PRIVATE_API_KEY");
http.set_password("SHOPIFY_PRIVATE_API_KEY");

http.set_accept("application/json");

let resp = chilkat::HttpResponse::new();
if http.http_no_body("GET", "https://{shop}.myshopify.com/admin/api/2020-04/products/{product_id}/variants/{variant_id}.json", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

println!("Response Status Code: {}", resp.status_code());

let json_response = chilkat::JsonObject::new();
let _ = json_response.load(&resp.body_str());
json_response.set_emit_compact(false);
println!("{}", json_response.emit().unwrap_or_default());

if resp.status_code() != 200 {
    println!("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"
// }
// 

let id = json_response.int_of("id");
let product_id = json_response.int_of("product_id");
let title = json_response.string_of("title").unwrap_or_default();
let inventory_item_id = json_response.int_of("inventory_item_id");
let admin_graphql_api_id = json_response.string_of("admin_graphql_api_id").unwrap_or_default();