Sample code for 30+ languages & platforms
Zig

AWS Secrets Manager - Get Secret Value

See more AWS Secrets Manager Examples

Retrieves the contents of the encrypted fields SecretString or SecretBinary from the specified version of a secret, whichever contains content.

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.

    // Sends the following sample request.
    // Note: Chilkat will automatically add Content-Length, X-Amz-Date, Accept-Encoding, and Authorization

    // POST / HTTP/1.1
    // Host: secretsmanager.region.domain
    // Accept-Encoding: identity
    // X-Amz-Target: secretsmanager.GetSecretValue
    // Content-Type: application/x-amz-json-1.1
    // X-Amz-Date: <date>
    // Authorization: AWS4-HMAC-SHA256 Credential=<credentials>,SignedHeaders=<headers>, Signature=<signature>
    // Content-Length: <payload-size-bytes>
    //
    // {
    //   "SecretId": "MyTestDatabaseSecret",
    // }

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

    // Connect to the Amazon AWS REST server.
    // such as https://secretsmanager.us-west-2.amazonaws.com/
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("secretsmanager.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect);

    // Provide AWS credentials for the REST call.
    const auth_aws = try chilkat.AuthAws.init();
    defer auth_aws.deinit();
    auth_aws.setAccessKey("AWS_ACCESS_KEY");
    auth_aws.setSecretKey("AWS_SECRET_KEY");
    // the region should match our URL above..
    auth_aws.setRegion("us-west-2");
    auth_aws.setServiceName("secretsmanager");

    rest.setAuthAws(auth_aws) catch {};

    // 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("SecretId", "MyTestDatabaseSecret2") catch {};

    rest.addHeader("Content-Type", "application/x-amz-json-1.1") catch {};
    rest.addHeader("X-Amz-Target", "secretsmanager.GetSecretValue") catch {};

    const sb_request_body = try chilkat.StringBuilder.init();
    defer sb_request_body.deinit();
    json.emitSb(sb_request_body) catch {};
    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    rest.fullRequestSb("POST", "/", sb_request_body, sb_response_body) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const resp_status_code = rest.getResponseStatusCode();
    std.debug.print("response status code = {d}\n", .{resp_status_code});

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.setEmitCompact(false);
    j_resp.loadSb(sb_response_body) catch {};

    if (resp_status_code >= 400) {
        std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("Response Body:\n", .{});
        std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
        return;
    }

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    // Sample response body:

    // {
    //   "ARN":"arn:aws:secretsmanager:us-west-2:123456789012:secret:MyTestDatabaseSecret-a1b2c3",
    //   "CreatedDate":1.523477145713E9,
    //   "Name":"MyTestDatabaseSecret",
    //   "SecretString":"{\n  \"username\":\"david\",\n  \"password\":\"BnQw&XDWgaEeT9XGTT29\"\n}\n",
    //   "VersionId":"EXAMPLE1-90ab-cdef-fedc-ba987SECRET1"
    // }

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

    _ = try j_resp.stringOf(alloc, "ARN");
    _ = try j_resp.stringOf(alloc, "CreatedDate");
    _ = try j_resp.stringOf(alloc, "Name");
    _ = try j_resp.stringOf(alloc, "SecretString");
    _ = try j_resp.stringOf(alloc, "VersionId");
}