Skip to main content
Solved

Best way to extract specific document from envelope


Forum|alt.badge.img+1

Hello all,

 

I am a developer using the eSignature API (specifically the Envelopes API) to construct Envelopes from multiple Composite templates.

My goal: I have a use case where I need to download a specific document from a signed Envelope. I want to understand the best / most efficient way to accomplish this.

 

After taking a look at the API documentation, I see that the only way to download a specific document from an Envelope is to use the EnvelopeDocuments:get API, which requires passing in the documentId.

 

I also know that despite specifying unique documentIds in each composite template, the documentId field is not guaranteed to stay the same as the envelope assigns new values to avoid any potential collisions. (See Referencing ID values here).

 

This makes getting the documentId challenging, and I am wondering what is the best way to accomplish this. After reading How to track recipients and documents when IDs change, I can think of three ways to identify a specific document:

  1. The article recommends to use EnvelopeDocuments:list API to retrieve all documents and use the filename field to identify the documentId. In my opinion, this method is brittle, as the filename is controlled via the template in the Docusign template UI. If the filename were to change tomorrow, any application logic we write around this field would break.
  2. The alternative approach mentioned in the article is to provide EnvelopeDocumentFields and use those to identify the document in question. In theory, I could pass a custom document field for the specific document I need to download and then search for this custom field in order to identify the documentId for download. However, I discovered that custom document fields are not included in the EnvelopeDocuments:list API response. As a result, this approach requires many API calls:
    • 1 call to write the custom field
    • 1 call to list the documents in the envelope
    • n calls to iterate through each document and check it for the custom field
    • 1 call to download the document with found documentId.
  3. A third approach would be to use the templateId of the document I am searching for. I have this information available to me, but the solution would require me to list all documents and iterate through each one, calling EnvelopeTemplates:listByDocument to check if the document’s templateId matches the one I am searching for. Also very inefficient.

 

Is there a better way to identify a specific document within an Envelope constructed from multiple Composite templates?

 

Thank you for taking the time to read. I appreciate any support!

Best answer by Byungjae.Chung

Among the options provided, the best approach to get the document is option 2. You can skip the API call for the "1 call to write the custom field" by defining the "documentField" property when creating the envelope via CreateEnvelope API. Also, you can retrieve the document field in each of the documents at once using the GetEnvelope API call with the query parameters below:

GET {baseUrl}/v2.1/accounts/{accountId}/envelopes/{envelopeId}?include=documents,document_fields

Instead of calling the API, you can utilize the Docusign Connect (webhook service). By using Connect, you can also get the custom fields and the document data at once. When adding the Connect configuration on Docusign Portal > Admin > Connect, please ensure to check the "Documents" and "Document Fields" under the "Envelope and Recipients" > "Include Data", as shown in the attached screenshot to include those data in the event.
 

 

View Original
Is this content helpful?

7 replies

Forum|alt.badge.img+7
  • Docusign Employee
  • 74 replies
  • Answer
  • April 14, 2025

Among the options provided, the best approach to get the document is option 2. You can skip the API call for the "1 call to write the custom field" by defining the "documentField" property when creating the envelope via CreateEnvelope API. Also, you can retrieve the document field in each of the documents at once using the GetEnvelope API call with the query parameters below:

GET {baseUrl}/v2.1/accounts/{accountId}/envelopes/{envelopeId}?include=documents,document_fields

Instead of calling the API, you can utilize the Docusign Connect (webhook service). By using Connect, you can also get the custom fields and the document data at once. When adding the Connect configuration on Docusign Portal > Admin > Connect, please ensure to check the "Documents" and "Document Fields" under the "Envelope and Recipients" > "Include Data", as shown in the attached screenshot to include those data in the event.
 

 


Forum|alt.badge.img+1
  • Author
  • Newcomer
  • 4 replies
  • April 14, 2025

Thank you Byungjae for the detailed response! It’s great to hear I can save the calls by providing documentFields directly in the createEnevelope call itself. Do you know if this is supported if I am creating the compositeTemplate from a serverTemplate instead of providing the document itself? I am not currently utilizing the document field of the compositeTemplate.

 

For example, my composite template for the document that needs to be identified looks as follows:

{
   serverTemplates: [
   {
      sequence: <>,
      templateId: <>,
      templateRoles: <>
   }],
   inlineTemplates: [
      {
         sequence: <>,
         recipients: {
            signers: [<>]
         }
      }
   ]
}

```


Forum|alt.badge.img+1
  • Author
  • Newcomer
  • 4 replies
  • April 14, 2025

Update: I did some testing and specifying just the document_fields without a document doesn’t seem to work in the envelope:Create API. Below is the payload I sent:

{
  "compositeTemplates": [
      {
          "documents": [
              {
                  "documentFields": [
                      {
                          "name": "document_name",
                          "value": "<my_document_name>"
                      }
                  ]
              }
          ],
          "serverTemplates": [
              {
                  "sequence": "1",
                  "templateId": "<my_template_id>"
              }
          ],
          "inlineTemplates": [
              {
                  "sequence": 1,
                  "recipients": {
                      "signers": [
                          {
                              "email": "<my_email_adress>",
                              "name": "<my_name>",
                              "recipientId": "1",
                              "roleName": "<my_role>"
                          }
                      ]
                  }
              }
          ]
      }
  ],
  "emailSubject": "<my_email_subject>",
  "emailBlurb": "<my_email_blurb>",
  "status": "sent"
}

I have a requirement to use the server template for the document rather than using the document field in the compositeTemplate. if it is not possible to specify `documentFields` in a composite template creation, I could leverage the server templateId which is readily available to me.

I see that after creating the envelope, I can use the EnvelopeTemplates:list API, but would it be possible to include the server templateId (if one exists) in the GetEnvelope API call? Maybe something like include=documents,document_templates?

The GetEnvelope API documentation doesn’t include this, but it also didn’t include documentFields so I am wondering if there is an unofficial query param I can pass. The reason why I ask is because we already issue a GetEnvelope API call, so including the server templateIds there would avoid having to issue an additional call to EnvelopTemplates:list.


Forum|alt.badge.img+7

As you noticed, you need to define the document if you want to specify the document_fields.

 

but would it be possible to include the server templateId (if one exists) in the GetEnvelope API call? Maybe something like include=documents,document_templates?

Unfortunately, GetEnvelope API doesn't return the template information applied to the envelope. You would need to call the EnvelopeTemplates:list API additionally to get the list of the templates.


Forum|alt.badge.img+1
  • Author
  • Newcomer
  • 4 replies
  • April 15, 2025

Thanks for all the help. I think we will go with the EnvelopeTemplates:list approach and use the known templateId to extract the documentId.


A final question on this topic: are documentIds assigned sequentially with respect to the order in which the compositeTemplates are provided in the request in createEnvelope? The document I am looking to download is always the last one in the request array so I am wondering if Docusign guarantees the order and I can just use the largest documentId for the download.


Forum|alt.badge.img+7

are documentIds assigned sequentially with respect to the order in which the compositeTemplates are provided in the request in createEnvelope?

Yes, your understanding is correct. However, it is not guaranteed. To avoid an unexpected update or change, you should refer to the document using documentFields.


Forum|alt.badge.img+1
  • Author
  • Newcomer
  • 4 replies
  • April 16, 2025

Thanks Byungjae. Based on this discussion, we will go with the EnvelopeTemplates:list to identify the correct documentId for download.