Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkMime.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    ; Adds the "Authorization: Bearer eyJ0eXAi.....UE8Kl_V8KroQ" header.
    jsonToken.i = CkJsonObject::ckCreate()
    If jsonToken.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Load a previously obtained OAuth2 access token.
    success = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/docusign.json")
    If success = 0
        Debug CkJsonObject::ckLastErrorText(jsonToken)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        ProcedureReturn
    EndIf

    CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(jsonToken,"access_token"))

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

    url.s = "https://demo.docusign.net/restapi/v2.1/accounts/{$accountId}/envelopes/{$envelopeId}/documents/1"
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckDownloadBd(http,url,bd)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkHttp::ckLastStatus(http)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode <> 200
        Debug "Response Header:"
        Debug CkHttp::ckLastResponseHeader(http)
        ; The response body contains an error message.
        Debug CkBinData::ckGetString(bd,"utf-8")
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(jsonToken)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    ; The response indicated success.
    ; Get the filename from the Content-Disposition header and save to a file.
    mime.i = CkMime::ckCreate()
    If mime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkMime::ckLoadMime(mime,CkHttp::ckLastResponseHeader(http))

    filename.s = CkMime::ckGetHeaderFieldAttribute(mime,"Content-Disposition","filename")
    Debug "filename = " + filename

    sbPath.i = CkStringBuilder::ckCreate()
    If sbPath.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbPath,"C:/aaworkarea/")
    CkStringBuilder::ckAppend(sbPath,filename)
    success = CkBinData::ckWriteFile(bd,CkStringBuilder::ckGetAsString(sbPath))
    If success = 0
        Debug "Failed to save to output file."
    Else
        Debug "Wrote " + CkStringBuilder::ckGetAsString(sbPath)
    EndIf



    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(jsonToken)
    CkBinData::ckDispose(bd)
    CkMime::ckDispose(mime)
    CkStringBuilder::ckDispose(sbPath)


    ProcedureReturn
EndProcedure