Sample code for 30+ languages & platforms
Zig

Amazon SP-API Create Feed Doc

See more Amazon SP-API Examples

Creates a feed document. Amazon returns a feedDocumentId value, encryption details, and a URL for uploading the feed contents.

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 auth_aws = try chilkat.AuthAws.init();
    defer auth_aws.deinit();
    auth_aws.setAccessKey("AWS_ACCESS_KEY");
    auth_aws.setSecretKey("AWS_SECRET_KEY");
    auth_aws.setServiceName("execute-api");
    // Use the region that is correct for your needs.
    auth_aws.setRegion("eu-west-1");

    const rest = try chilkat.Rest.init();
    defer rest.deinit();
    rest.connect("sellingpartnerapi-eu.amazon.com", 443, true, true) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    try rest.setAuthAws(auth_aws);

    // Load the previously obtained LWA access token.
    // See Fetch SP-API LWA Access Token
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/sp_api_lwa_token.json") catch {
        std.debug.print("Failed to load LWA access token.\n", .{});
        return;
    };

    // Add the x-amz-access-token request header.
    const lwa_token = try json_token.stringOf(alloc, "access_token");

    const json_req = try chilkat.JsonObject.init();
    defer json_req.deinit();
    json_req.updateString("contentType", "text/tab-separated-values; charset=UTF-8") catch {};

    const sb_request = try chilkat.StringBuilder.init();
    defer sb_request.deinit();
    json_req.emitSb(sb_request) catch {};

    rest.clearAllQueryParams() catch {};
    rest.clearAllHeaders() catch {};
    rest.addHeader("x-amz-access-token", lwa_token) catch {};

    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    const path = "/feeds/2021-06-30/documents";
    rest.fullRequestSb("POST", path, sb_request, sb_response) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Examine the response status.
    const status_code = rest.getResponseStatusCode();
    std.debug.print("statusCode: {d}\n", .{status_code});

    if (status_code != 201) {
        std.debug.print("Response status text: {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("Response body: \n", .{});
        std.debug.print("{s}\n", .{try sb_response.getAsString(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

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

    // If successful, gets a JSON response such as the following:

    // {
    //   "feedDocumentId": "3d4e42b5-1d6e-44e8-a89c-2abfca0625bb",
    //   "url": "https://d34o8swod1owfl.cloudfront.net/Feed_101__POST_PRODUCT_DATA_.xml"
    // }

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    const json = try chilkat.JsonObject.init();
    defer json.deinit();

    json.loadSb(sb_response) catch {};

    _ = try json.stringOf(alloc, "feedDocumentId");
    _ = try json.stringOf(alloc, "url");

    // Save the JSON to a file for the example that uploads the feed..
    try json.writeFile("qa_data/json/sp_api_feed_upload_info.json");

    std.debug.print("Success!\n", .{});
}