Ed H
Ed H

Reputation: 19

Attempting to provide a document to override a server template causes Unspecified error An item with the same key has already been added

I'm attempting to port code that uses the Docusign SOAP API method CreateEnvelopeFromTemplatesAndForms to the REST API. I used this page of the Rest API Reference - Send an Envelope from a Template

I've hit a roadblock in that when I try to specify a document to post to override the server template's document, I get an Unspecified error, 'An item with the same key has already been added'.

The code I'm porting over uses two inline templates and a server template.

The server template can contain one or more documents that I want to override with local documents. In the SOAP method, the documents are specified in the first inline template. The server template is specified next. And the second inline template is third in the sequence and has the recipients and custom fields. These are all added to the envelope property of the inline template.

  1. Inline Template - Docs
  2. Server Template
  3. Inline Template - Recipients and Custom Fields

In the REST api, there doesn't seem to be an envelope property for the inline template and no clear migration guide between SOAP and REST that I could find. If I exclude my 'documents' property from the inline template, the envelope sends. I get the same error if I try using the document property of compositeTemplate. Here is my web request:

POST https://demo.docusign.net/restapi/v2/accounts/ACCOUNTID/envelopes HTTP/1.1
X-DocuSign-Authentication: <DocuSignCredentials><Username>USERNAME</Username><Password>PASSWORD</Password><IntegratorKey>INTEGRATOR_KEY</IntegratorKey></DocuSignCredentials>
Accept: application/json
Content-Type: multipart/form-data; boundary=Ne737Hao~j
Host: demo.docusign.net
Content-Length: 29110
Expect: 100-continue

--Ne737Hao~j
Content-Type: application/json
Content-Disposition: form-data

{
  "status": "sent",
  "emailSubject": "Test Doc",
  "emailBlurb": "This is a test doc",
  "compositeTemplates": [
    {
      "serverTemplates": [
        {
          "sequence": "2",
          "templateId": "0b9f2226-6c2b-4c75-84c4-c1708a5c7b97"
        }
      ],
      "inlineTemplates": [
        {
          "sequence": "1",
          "documents": [
            {
              "documentId": "1",
              "name": "Donuts.pdf",
              "transformPdfFields": "false"
            }
          ]
        },
        {
          "sequence": "3",
          "recipients": {
            "signers": [
              {
                "name": "ME",
                "recipientId": "99f8f51b-ef21-46b6-a551-69c8b27159d4",
                "roleName": "Signer 1",
                "email": "MY_EMAIL_ADDRESS"
              },
              {
                "name": "ME",
                "recipientId": "bb4388f3-098e-4d3e-bcf2-20c1ae0a7c48",
                "roleName": "Signer 2",
                "email": "MY_EMAIL_ADDRESS"
              }
            ],
            "carbonCopies": [
              {
                "name": "ME",
                "recipientId": "bc65de08-984e-4f95-925d-fcf00c05966d",
                "roleName": "Auditor",
                "email": "MY_EMAIL_ADDRESS"
              }
            ]
          }
        }
      ]
    }
  ],
  "customFields": {
    "textCustomFields": [
      {
        "name": "TemplateName",
        "show": "false",
        "value": "Test Template"
      }
    ]
  }
}
--Ne737Hao~j
Content-Type: application/pdf
Content-Disposition:file; file="Donuts.pdf"; documentId=1
Content-Transfer-Encoding: binary

%PDF-1.6
REST OF PDF BYTES
%%EOF

--Ne737Hao~j--

Thanks in advance.

I've also already looked at Incorrectly applied server templates with multiple composite templates and Adding documents to envelopes using composite templates in DocuSign with little luck.

Update

After trying the answer by WTP API below, I experimented some more. I found that the envelope would send with the correct document if I either include the base64 of the document or if I simplified the headers sent with the document bytes to just be Content-Disposition:documentId=1 instead of all the headers shown here: Docusign Rest API - Send an Envelope From a Template

POST https://demo.docusign.net/restapi/v2/accounts/ACCOUNTID/envelopes HTTP/1.1
X-DocuSign-Authentication: <DocuSignCredentials><Username>USERNAME</Username><Password>PASSWORD</Password><IntegratorKey>INTEGRATOR_KEY</IntegratorKey></DocuSignCredentials>
Accept: application/json
Content-Type: multipart/form-data; boundary=Ne737Hao~j
Host: demo.docusign.net
Content-Length: 29110
Expect: 100-continue

--Ne737Hao~j
Content-Type: application/json
Content-Disposition: form-data

{
  "status": "sent",
  "emailSubject": "Test Doc",
  "emailBlurb": "This is a test doc",
  "compositeTemplates": [
    {
      "serverTemplates": [
        {
          "sequence": "2",
          "templateId": "0b9f2226-6c2b-4c75-84c4-c1708a5c7b97"
        }
      ],
      "inlineTemplates": [
        {
          "sequence": "1",
          "documents": [
            {
              "documentId": "1",
              "name": "Donuts.pdf",
              "transformPdfFields": "false"
            }
          ]
        },
        {
          "sequence": "3",
          "recipients": {
            "signers": [
              {
                "name": "ME",
                "recipientId": "99f8f51b-ef21-46b6-a551-69c8b27159d4",
                "roleName": "Signer 1",
                "email": "MY_EMAIL_ADDRESS"
              },
              {
                "name": "ME",
                "recipientId": "bb4388f3-098e-4d3e-bcf2-20c1ae0a7c48",
                "roleName": "Signer 2",
                "email": "MY_EMAIL_ADDRESS"
              }
            ],
            "carbonCopies": [
              {
                "name": "ME",
                "recipientId": "bc65de08-984e-4f95-925d-fcf00c05966d",
                "roleName": "Auditor",
                "email": "MY_EMAIL_ADDRESS"
              }
            ]
          }
        }
      ]
    }
  ],
  "customFields": {
    "textCustomFields": [
      {
        "name": "TemplateName",
        "show": "false",
        "value": "Test Template"
      }
    ]
  }
}
--Ne737Hao~j
Content-Disposition:documentId=1

%PDF-1.6
REST OF PDF BYTES
%%EOF

--Ne737Hao~j--

Upvotes: 0

Views: 361

Answers (1)

WTP API
WTP API

Reputation: 546

The "documents" node and "recipients" should be within the same inline template.

Below is the correct JSON body that works for me:

{
    "status": "sent",
    "emailSubject": "Test Doc",
    "emailBlurb": "This is a test doc",
    "compositeTemplates": [
        {
            "serverTemplates": [
                {
                    "sequence": "2",
                    "templateId": "0b9f2226-6c2b-4c75-84c4-c1708a5c7b97"
                }
            ],
            "inlineTemplates": [
                {
                    "sequence": "1",
                    "documents": [
                        {
                            "documentId": "1",
                            "name": "Donuts.pdf",
                            "transformPdfFields": "false"
                        }
                    ],
                    "recipients": {
                        "signers": [
                            {
                                "name": "ME",
                                "recipientId": "99f8f51b-ef21-46b6-a551-69c8b27159d4",
                                "roleName": "Signer 1",
                                "email": "MY_EMAIL_ADDRESS"
                            },
                            {
                                "name": "ME",
                                "recipientId": "bb4388f3-098e-4d3e-bcf2-20c1ae0a7c48",
                                "roleName": "Signer 2",
                                "email": "MY_EMAIL_ADDRESS"
                            }
                        ],
                        "carbonCopies": [
                            {
                                "name": "ME",
                                "recipientId": "bc65de08-984e-4f95-925d-fcf00c05966d",
                                "roleName": "Auditor",
                                "email": "MY_EMAIL_ADDRESS"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "customFields": {
        "textCustomFields": [
            {
                "name": "TemplateName",
                "show": "false",
                "value": "Test Template"
            }
        ]
    }
}

Upvotes: 2

Related Questions