Sample code for 30+ languages & platforms
Zig

Create Restricted Data Token (RDT)

See more Amazon SP-API Examples

Returns a Restricted Data Token (RDT) for one or more restricted resources that you specify.

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();
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    // The sandbox endpoint (sandbox.sellingpartnerapi-eu.amazon.com) fails.
    // Use the production endpoint and see the note below.
    rest.connect("sellingpartnerapi-eu.amazon.com", port, b_tls, b_auto_reconnect) 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");
    rest.clearAllHeaders() catch {};
    rest.addHeader("x-amz-access-token", lwa_token) catch {};

    // We're going to send a POST with the following JSON body:

    // {
    //   "restrictedResources": [
    //     {
    //       "method": "GET",
    //       "path": "/orders/v0/orders",
    //       "dataElements": ["buyerInfo", "shippingAddress"]
    //     }
    //   ]
    // }

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

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("restrictedResources[0].method", "GET") catch {};
    json.updateString("restrictedResources[0].path", "/orders/v0/orders") catch {};
    json.updateString("restrictedResources[0].dataElements[0]", "buyerInfo") catch {};
    json.updateString("restrictedResources[0].dataElements[1]", "shippingAddress") catch {};

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

    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    const uri = "/tokens/2021-03-01/restrictedDataToken";
    rest.fullRequestSb("POST", uri, sb_request, sb_response) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // ------------------------------------------------------------------------------------
    // Note: Using the sandbox endpoint, such as sandbox.sellingpartnerapi-eu.amazon.com
    // results in a response status code of 400 with the following error:
    // [{"code":"InvalidInput","message":"Could not match input arguments"}]
    // Getting a restricted data token seems to only work with the production endpoint.
    // ------------------------------------------------------------------------------------

    // Examine the response status.
    const status_code = rest.getResponseStatusCode();
    if (status_code != 200) {
        std.debug.print("Response status code: {d}\n", .{status_code});
        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:

    // {
    //   "expiresIn": 3600,
    //   "restrictedDataToken": "Atz.sprdt|AYAB.....TQ="
    // }

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

    const json_resp = try chilkat.JsonObject.init();
    defer json_resp.deinit();
    json_resp.loadSb(sb_response) catch {};

    _ = json_resp.intOf("expiresIn");
    _ = try json_resp.stringOf(alloc, "restrictedDataToken");

    // Save the RDT for subsequent use..
    try json_resp.writeFile("qa_data/tokens/sp_api_rdt_token.json");

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