Sample code for 30+ languages & platforms
JavaScript

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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

var http = new CkHttp();

// Implements the following HTTP request:
// GET /restapi/v2.1/accounts/{accountId}/envelopes/{envelopeId}/documents/1

// Adds the "Authorization: Bearer eyJ0eXAi.....UE8Kl_V8KroQ" header.
var jsonToken = new CkJsonObject();
// Load a previously obtained OAuth2 access token.
success = jsonToken.LoadFile("qa_data/tokens/docusign.json");
if (success == false) {
    console.log(jsonToken.LastErrorText);
    return;
}

http.AuthToken = jsonToken.StringOf("access_token");

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

var url = "https://demo.docusign.net/restapi/v2.1/accounts/{$accountId}/envelopes/{$envelopeId}/documents/1";
var bd = new CkBinData();

success = http.DownloadBd(url,bd);
if (success == false) {
    console.log(http.LastErrorText);
    return;
}

var respStatusCode = http.LastStatus;
console.log("Response Status Code = " + respStatusCode);
if (respStatusCode !== 200) {
    console.log("Response Header:");
    console.log(http.LastResponseHeader);
    // The response body contains an error message.
    console.log(bd.GetString("utf-8"));
    console.log("Failed.");
    return;
}

// The response indicated success.
// Get the filename from the Content-Disposition header and save to a file.
var mime = new CkMime();
mime.LoadMime(http.LastResponseHeader);

var filename = mime.GetHeaderFieldAttribute("Content-Disposition","filename");
console.log("filename = " + filename);

var sbPath = new CkStringBuilder();
sbPath.Append("C:/aaworkarea/");
sbPath.Append(filename);
success = bd.WriteFile(sbPath.GetAsString());
if (success == false) {
    console.log("Failed to save to output file.");
}
else {
    console.log("Wrote " + sbPath.GetAsString());
}