Sample code for 30+ languages & platforms
Zig

DocuSign Download Envelope Document (PDF)

See more DocuSign Examples

Retrieves the specified document from the envelope. The response body of this method is the PDF file as a byte stream. You can get the file name and document ID from the response's Content-Disposition 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();

    var success: bool = false;

    // 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 HTTP request:
    // GET /restapi/v2.1/accounts/{accountId}/envelopes/{envelopeId}/documents/1

    // 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.
    success = if (json_token.loadFile("qa_data/tokens/docusign.json")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try json_token.getLastErrorText(alloc)});
        return;
    }

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

    // Use your account ID and a valid envelopeId here:
    http.setUrlVar("accountId", "7f3f65ed-5e87-418d-94c1-92499ddc8252") catch {};
    http.setUrlVar("envelopeId", "90d7e40a-b4bd-4ccd-bf38-c80e37954a13") catch {};

    const url = "https://demo.docusign.net/restapi/v2.1/accounts/{$accountId}/envelopes/{$envelopeId}/documents/1";
    const bd = try chilkat.BinData.init();
    defer bd.deinit();

    success = if (http.downloadBd(url, bd)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    }

    const resp_status_code = http.getLastStatus();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code != 200) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try http.getLastResponseHeader(alloc)});
        // The response body contains an error message.
        std.debug.print("{s}\n", .{try bd.getString(alloc, "utf-8")});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // The response indicated success.
    // Get the filename from the Content-Disposition header and save to a file.
    const mime = try chilkat.Mime.init();
    defer mime.deinit();
    mime.loadMime(try http.getLastResponseHeader(alloc)) catch {};

    const filename = try mime.getHeaderFieldAttribute(alloc, "Content-Disposition", "filename");
    std.debug.print("filename = {s}\n", .{filename});

    const sb_path = try chilkat.StringBuilder.init();
    defer sb_path.deinit();
    sb_path.append("C:/aaworkarea/") catch {};
    sb_path.append(filename) catch {};
    success = if (bd.writeFile(try sb_path.getAsString(alloc))) true else |_| false;
    if (!success) {
        std.debug.print("Failed to save to output file.\n", .{});
    } else {
        std.debug.print("Wrote {s}\n", .{try sb_path.getAsString(alloc)});
    }
}