Sample code for 30+ languages & platforms
Zig

Update an Inventory Listing using OAuth1 Authentication

See more Etsy Examples

Updates an inventory listing. This example uses OAuth1 authentication instead of providing an api_key=MY_ETSY_KEYSTRING query parameter.

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // See this example for getting an OAuth1 token for Etsy

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.loadFile("qa_data/tokens/etsy.json") catch {
        std.debug.print("Failed to load previously fetched Etsy OAuth1 access token.\n", .{});
        return;
    };

    const oauth1 = try chilkat.OAuth1.init();
    defer oauth1.deinit();

    oauth1.setConsumerKey("app_keystring");
    oauth1.setConsumerSecret("app_shared_secret");
    oauth1.setToken(try json.stringOf(alloc, "oauth_token"));
    oauth1.setTokenSecret(try json.stringOf(alloc, "oauth_token_secret"));
    oauth1.setSignatureMethod("HMAC-SHA1");
    oauth1.genNonce(16) catch {};

    const auto_reconnect = true;
    const tls = true;
    rest.connect("openapi.etsy.com", 443, tls, auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Tell the REST object to use the OAuth1 object.
    try rest.setAuthOAuth1(oauth1, true);

    const json_text = "[{\"product_id\":1999949999,\"property_values\":[],\"offerings\":[{\"offering_id\":9999905883,\"price\":\"36.23\",\"quantity\":1}]}]";

    rest.addQueryParam("products", json_text) catch {};
    rest.addHeader("Content-Type", "application/x-www-form-urlencoded") catch {};

    const json_response_text = rest.fullRequestFormUrlEncoded(alloc, "PUT", "/v2/listings/228827035/inventory") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.load(json_response_text) catch {};
    json_response.setEmitCompact(false);

    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    std.debug.print("Response status code: {d}\n", .{rest.getResponseStatusCode()});
}