Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Handle hoJsonToken
    String sUrl
    Variant vBd
    Handle hoBd
    Integer iRespStatusCode
    Handle hoMime
    String sFilename
    Handle hoSbPath
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

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

    // Adds the "Authorization: Bearer eyJ0eXAi.....UE8Kl_V8KroQ" header.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonToken
    If (Not(IsComObjectCreated(hoJsonToken))) Begin
        Send CreateComObject of hoJsonToken
    End
    // Load a previously obtained OAuth2 access token.
    Get ComLoadFile Of hoJsonToken "qa_data/tokens/docusign.json" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJsonToken To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonToken "access_token" To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1

    // Use your account ID and a valid envelopeId here:
    Get ComSetUrlVar Of hoHttp "accountId" "7f3f65ed-5e87-418d-94c1-92499ddc8252" To iSuccess
    Get ComSetUrlVar Of hoHttp "envelopeId" "90d7e40a-b4bd-4ccd-bf38-c80e37954a13" To iSuccess

    Move "https://demo.docusign.net/restapi/v2.1/accounts/{$accountId}/envelopes/{$envelopeId}/documents/1" To sUrl
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End

    Get pvComObject of hoBd to vBd
    Get ComDownloadBd Of hoHttp sUrl vBd To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLastStatus Of hoHttp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode <> 200) Begin
        Showln "Response Header:"
        Get ComLastResponseHeader Of hoHttp To sTemp1
        Showln sTemp1
        // The response body contains an error message.
        Get ComGetString Of hoBd "utf-8" To sTemp1
        Showln sTemp1
        Showln "Failed."
        Procedure_Return
    End

    // The response indicated success.
    // Get the filename from the Content-Disposition header and save to a file.
    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End
    Get ComLastResponseHeader Of hoHttp To sTemp1
    Get ComLoadMime Of hoMime sTemp1 To iSuccess

    Get ComGetHeaderFieldAttribute Of hoMime "Content-Disposition" "filename" To sFilename
    Showln "filename = " sFilename

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPath
    If (Not(IsComObjectCreated(hoSbPath))) Begin
        Send CreateComObject of hoSbPath
    End
    Get ComAppend Of hoSbPath "C:/aaworkarea/" To iSuccess
    Get ComAppend Of hoSbPath sFilename To iSuccess
    Get ComGetAsString Of hoSbPath To sTemp1
    Get ComWriteFile Of hoBd sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Showln "Failed to save to output file."
    End
    Else Begin
        Get ComGetAsString Of hoSbPath To sTemp1
        Showln "Wrote " sTemp1
    End



End_Procedure