Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Walmart Partner API Authentication (Generate a Signature for a Request)

See more RSA Examples

Demonstrates how to generate a signature for a Walmart Partner REST API call.

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 consumer_id = "b68d2a72....";
    const base_url = "https://marketplace.walmartapis.com/v2/feeds";
    // This is your Base64 encoded private key
    const private_encoded_str = "MIICeAIBADANBgkqhkiG9w0BAQEFAA......";
    const http_method = "GET";

    // We need a timestamp in decimal string form representing the number of milliseconds since Jan 01 1970 UTC.
    const dt = try chilkat.DateTime.init();
    defer dt.deinit();
    // Set bLocal = true for a timestamp in the local timezone.  Set bLocal = false for a UTC timestamp.
    const b_local = false;
    // This gets the timestamp in seconds, not milliseconds.
    const time_stamp_val = @as(i32, @intCast(dt.getAsUnixTime(b_local)));

    // Build the string to sign.
    const sb_string_to_sign = try chilkat.StringBuilder.init();
    defer sb_string_to_sign.deinit();
    sb_string_to_sign.append(consumer_id) catch {};
    sb_string_to_sign.append("\n") catch {};
    sb_string_to_sign.append(base_url) catch {};
    sb_string_to_sign.append("\n") catch {};
    sb_string_to_sign.append(http_method) catch {};
    sb_string_to_sign.append("\n") catch {};
    sb_string_to_sign.appendInt(time_stamp_val) catch {};
    // We add three zero's so that the timestamp value is in milliseconds.
    // We don't care about accuracy down to less than a second.
    // All the server cares about is that the request was signed at the current date/time
    // within some reasonable margin of error (to account for systems having clocks
    // that may be slightly different).
    sb_string_to_sign.append("000\n") catch {};

    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    // Load the private key into a private key object.
    // Note: Technically the private key is not PEM because it lacks the header/footer strings
    // used for PEM.  However, the LoadPem method will still accept it and load it correctly.
    priv_key.loadPem(private_encoded_str) catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    const rsa = try chilkat.Rsa.init();
    defer rsa.deinit();
    rsa.usePrivateKey(priv_key) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    // We want a base64 signature string.
    rsa.setEncodingMode("base64");

    const signature_string = rsa.signStringENC(alloc, try sb_string_to_sign.getAsString(alloc), "SHA256") catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Signature String: {s}\n", .{signature_string});
}