How to attach documents via binary transfer
This topic demonstrates how to attach documents to an envelope via binary transfer.
Run this code example from our Quickstart or launcher projects: Bash, C#, Java, Node.js, PHP, PowerShell, Python, or Ruby.
Required data
Running the code in this how-to requires this data:
Step 1. Obtain your OAuth token
To make the API call shown in this how-to, you need a valid OAuth access token. Docusign supports access tokens for three different recommended OAuth grant types: Public Authorization Code Grant, Confidential Authorization Code Grant, and JSON Web Token (JWT) Grant. Choose the OAuth grant type appropriate for your scenario.
See Confidential Authorization Code Grant for guidance on your options to get an Confidential Authorization Code Grant OAuth token.
Step 2. Construct your API headers
After you have a valid OAuth access token, you are ready to construct the API headers that you will pass to the API call. Construct your headers like this, but make sure to substitute your valid OAuth access token for {ACCESS_TOKEN}.
Step 3. Construct the request body
Create a custom envelope definition to apply multipart boundary string delimiters for your JSON request. This topic shows how to create a JSON envelope definition using a method, makeEnvelope, that:
-
Creates your multipart JSON request body object.
- Begin a multipart body by first appending two hyphens, then the boundary separator. From there, append a CRLF (A carriage return/line feed, more commonly known as a line break or new line), then add the header
"Content-Type: application/json"; make another CRLF, and then add the header"Content-Disposition: form data". - Now add two more CRLFs, one after another (which occurs to denote that the proceeding data is assumed to be of
Content-Type text/plain). From there, insert the envelope JSON object.
- Begin a multipart body by first appending two hyphens, then the boundary separator. From there, append a CRLF (A carriage return/line feed, more commonly known as a line break or new line), then add the header
-
Attaches multiple documents to the JSON envelope.
- Add each file in bytes (specifically as an encoded byte array), with a correlated content disposition, into the request body. To do so, for each document, append your existing request body with the following sequence:
Content-Typewith the value being the MIME type, UTF-8 encoded, and thenContent-Dispositionas a file, also including attributes such as the filename and document ID, and finally the document in bytes.
- Add each file in bytes (specifically as an encoded byte array), with a correlated content disposition, into the request body. To do so, for each document, append your existing request body with the following sequence:
-
Selects the appropriate MIME type and file attributes for your documents.
Note:
You must specify correct matching MIME-type attributes so you do not encounter errors when attempting to load the documents.
-
Specifies your CRLF boundary delimiter and applies it to your documents.
- With all documents attached one after another, split by boundary delimiters, specify a closing boundary by appending a final set of CRLF, hyphens, boundary, hyphens, then ending CRLF.
-
Attaches multiple documents to the JSON envelope.
-
Sets signer and cc recipient data in the JSON envelope.
-
Creates a routing order for the documents in the JSON envelope. Documents are delivered to recipients in ascending order, as each previous document is signed and completed.
-
Creates and assigns signable fields to specific signers within the documents.
Step 4. Call the eSignature REST API
With a completed request body taking some reference fields from the envelope JSON, you can send the envelope with the Envelopes:create method.
- Send a POST request using the base path that injects your API account ID in the
account_idportion of the URI. - Set the
Authorizationheader to the OAuth2.0 bearer token, anAcceptheader toapplication/json, theContent-typetomultipart/form-data, and the boundary to the specified delimiter variable in the prerequisite. Complete the request by passing in the completed request body. - Keep the code contained in a wrapper named
worker, then run this worker method within atry/catchmechanism to handle any returned errors should any parameters be invalid or expired. By using a known template GUID and passing along appropriate template role parameters, you can return an envelope ID from the result.
Expected response
If you run this code example from our Quickstart or launcher projects, you should see a JSON response similar to the code shown.
In addition, the signer receives an email containing a signing link that they can use to open and electronically sign a document using the Docusign mobile app or the Docusign website. After signing is complete, the recipient receives a carbon copy (CC) of the email that includes all of the completed and signed documents.
Next steps
- Run this code example from our Quickstart or launcher projects: Bash, C#, Java, Node.js, PHP, PowerShell, Python, or Ruby.
- Learn about documents.
- See How to request a signature by email.
- See how to Go-Live with a completed app and find the base path for API calls in the production environment.