Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

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

local http = chilkat.Http.new()

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

-- Adds the "Authorization: Bearer eyJ0eXAi.....UE8Kl_V8KroQ" header.
local jsonToken = chilkat.JsonObject.new()
-- Load a previously obtained OAuth2 access token.
local success = jsonToken:load_file("qa_data/tokens/docusign.json")
if not success then
    print(jsonToken.last_error_text)
    return
end

http.auth_token = jsonToken:string_of("access_token")

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

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

success = http:download_bd(url, bd)
if not success then
    print(http.last_error_text)
    return
end

local respStatusCode = http.last_status
print("Response Status Code = " .. respStatusCode)
if respStatusCode ~= 200 then
    print("Response Header:")
    print(http.last_response_header)
    -- The response body contains an error message.
    print(bd:get_string("utf-8"))
    print("Failed.")
    return
end

-- The response indicated success.
-- Get the filename from the Content-Disposition header and save to a file.
local mime = chilkat.Mime.new()
mime:load_mime(http.last_response_header)

local filename = mime:get_header_field_attribute("Content-Disposition", "filename")
print("filename = " .. filename)

local sbPath = chilkat.StringBuilder.new()
sbPath:append("C:/aaworkarea/")
sbPath:append(filename)
success = bd:write_file(sbPath:get_as_string())
if not success then
    print("Failed to save to output file.")
else
    print("Wrote " .. sbPath:get_as_string())
end