Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, BinData, StringBuilder, Mime, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
http: HCkHttp;
jsonToken: HCkJsonObject;
url: PWideChar;
bd: HCkBinData;
respStatusCode: Integer;
mime: HCkMime;
filename: PWideChar;
sbPath: HCkStringBuilder;

begin
success := False;

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

http := CkHttp_Create();

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

// Adds the "Authorization: Bearer eyJ0eXAi.....UE8Kl_V8KroQ" header.
jsonToken := CkJsonObject_Create();
// Load a previously obtained OAuth2 access token.
success := CkJsonObject_LoadFile(jsonToken,'qa_data/tokens/docusign.json');
if (success = False) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(jsonToken));
    Exit;
  end;

CkHttp_putAuthToken(http,CkJsonObject__stringOf(jsonToken,'access_token'));

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

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

success := CkHttp_DownloadBd(http,url,bd);
if (success = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

respStatusCode := CkHttp_getLastStatus(http);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode <> 200) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkHttp__lastResponseHeader(http));
    // The response body contains an error message.
    Memo1.Lines.Add(CkBinData__getString(bd,'utf-8'));
    Memo1.Lines.Add('Failed.');
    Exit;
  end;

// The response indicated success.
// Get the filename from the Content-Disposition header and save to a file.
mime := CkMime_Create();
CkMime_LoadMime(mime,CkHttp__lastResponseHeader(http));

filename := CkMime__getHeaderFieldAttribute(mime,'Content-Disposition','filename');
Memo1.Lines.Add('filename = ' + filename);

sbPath := CkStringBuilder_Create();
CkStringBuilder_Append(sbPath,'C:/aaworkarea/');
CkStringBuilder_Append(sbPath,filename);
success := CkBinData_WriteFile(bd,CkStringBuilder__getAsString(sbPath));
if (success = False) then
  begin
    Memo1.Lines.Add('Failed to save to output file.');
  end
else
  begin
    Memo1.Lines.Add('Wrote ' + CkStringBuilder__getAsString(sbPath));
  end;

CkHttp_Dispose(http);
CkJsonObject_Dispose(jsonToken);
CkBinData_Dispose(bd);
CkMime_Dispose(mime);
CkStringBuilder_Dispose(sbPath);

end;