Sample code for 30+ languages & platforms
Zig

Retrieve User Account Data

See more DocuSign Examples

To make an API call to the DocuSign platform, your application needs both an access token (which you obtained in the previous step), and base URI that is unique to the user on whose behalf your application is making the API call. To get the base URI, call the/oauth/userinfoendpoint, supplying your application’s access token as a header.

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 http = try chilkat.Http.init();
    defer http.deinit();

    // Implements the following CURL command:

    // curl --header "Authorization: Bearer eyJ0eXAi.....UE8Kl_V8KroQ" https://account-d.docusign.com/oauth/userinfo

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    // Adds the "Authorization: Bearer eyJ0eXAi.....UE8Kl_V8KroQ" header.
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    // Load a previously obtained OAuth2 access token.
    json_token.loadFile("qa_data/tokens/docusign.json") catch {
        std.debug.print("{s}\n", .{try json_token.getLastErrorText(alloc)});
        return;
    };

    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://account-d.docusign.com/oauth/userinfo", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

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

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

    const resp_status_code = http.getLastStatus();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try http.getLastResponseHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "sub": "564f7988-xxxx-xxxx-xxxx-781ee556ab7a",
    //   "name": "Example J Smith",
    //   "given_name": "Example",
    //   "family_name": "Smith",
    //   "created": "2018-04-13T22:03:03.45",
    //   "email": "Example.Smith@exampledomain.com",
    //   "accounts": [
    //     {
    //       "account_id": "18b4799a-xxxx-xxxx-xxxx-b5b4b8a97604",
    //       "is_default": true,
    //       "account_name": "ExampleAccount",
    //       "base_uri": "https://demo.docusign.net"
    //     }
    //   ]
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var account_id: [:0]const u8 = "";
    var is_default: bool = false;
    var account_name: [:0]const u8 = "";
    var base_uri: [:0]const u8 = "";

    _ = try j_resp.stringOf(alloc, "sub");
    _ = try j_resp.stringOf(alloc, "name");
    _ = try j_resp.stringOf(alloc, "given_name");
    _ = try j_resp.stringOf(alloc, "family_name");
    _ = try j_resp.stringOf(alloc, "created");
    _ = try j_resp.stringOf(alloc, "email");
    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("accounts");
    while (i < count_i) {
        j_resp.setI(i);
        account_id = try j_resp.stringOf(alloc, "accounts[i].account_id");
        is_default = j_resp.boolOf("accounts[i].is_default");
        account_name = try j_resp.stringOf(alloc, "accounts[i].account_name");
        base_uri = try j_resp.stringOf(alloc, "accounts[i].base_uri");
        i = i + 1;
    }
}