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

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const http = try chilkat.Http.init();
    defer http.deinit();

    http.setLogin("SHOPIFY_PRIVATE_API_KEY");
    http.setPassword("SHOPIFY_PRIVATE_API_KEY");

    http.setAccept("application/json");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpNoBody("GET", "https://{shop}.myshopify.com/admin/api/2020-04/products/{product_id}/variants/{variant_id}.json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response Status Code: {d}\n", .{resp.getStatusCode()});

    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.load(try resp.getBodyStr(alloc)) catch {};
    json_response.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    if (resp.getStatusCode() != 200) {
        std.debug.print("Failed.\n", .{});
        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"
    // }
    //

    _ = json_response.intOf("id");
    _ = json_response.intOf("product_id");
    _ = try json_response.stringOf(alloc, "title");
    _ = json_response.intOf("inventory_item_id");
    _ = try json_response.stringOf(alloc, "admin_graphql_api_id");
}