Rust Requires Chilkat v11.0.0+
Rust
Shopify Set inventory levels to a certain amount
See more Shopify Examples
Use the set endpoint with the location ID and inventory item ID to set an inventory level to a specific value.Chilkat Rust Downloads
// 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");
// Also see: How to retrieve inventory levels
http.set_accept("application/json");
// The following JSON is sent in the request body:
// {
// "location_id": 6884556842,
// "inventory_item_id": 12250274365496,
// "available": 1
// }
// Use this online tool to generate the code from sample JSON:
// Generate Code to Create JSON
let json_request_body = chilkat::JsonObject::new();
let _ = json_request_body.update_int("location_id", 6884556842);
let _ = json_request_body.update_int("inventory_item_id", 12250274365496);
let _ = json_request_body.update_int("available", 1);
let url = "https://{shop}.myshopify.com/admin/api/2020-04/inventory_levels/set.json".to_string();
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", &url, &json_request_body, "application/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() >= 300 {
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
// {
// "inventory_level": {
// "inventory_item_id": 12250274365496,
// "location_id": 6884556842,
// "available": 1,
// "updated_at": "2018-06-26T15:44:33-04:00",
// ...
// "admin_graphql_api_id": "gid://shopify/InventoryLevel/6485147690?inventory_item_id=12250274365496"
// ...
// }
// }
//
let inventory_level_inventory_item_id = json_response.int_of("inventory_level.inventory_item_id");
let inventory_level_location_id = json_response.int_of("inventory_level.location_id");
let inventory_level_available = json_response.int_of("inventory_level.available");
let inventory_level_updated_at = json_response.string_of("inventory_level.updated_at").unwrap_or_default();
let inventory_level_admin_graphql_api_id = json_response.string_of("inventory_level.admin_graphql_api_id").unwrap_or_default();