You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

DocuSign Rest API


The DocuSign is for sending agreements and complete approvals from anywhere and any device. It uses Digital Signatures using a standard called Public Key Infrastructure (PKI).  It can be integrated with Salesforce with the pre build Integration system to send, sign, and manage important sales documents.  The standard DocuSign Salesforce Package provides the ability to send documents to any Contacts/Leads/Emails easily from Salesforce.  DocuSign Web services make it possible for more convenient and user friendly way of sending documents in some point clicks.

DocuSign Web Services: 

SOAP API and REST API Web Services provide a way to build complex enterprise applications.  Both the web services are secured, reliable and can be implemented using Apex.

SOAP API: 

SOAP API is efficient and secured way of interacting with DocuSign, but the implementation of SOAP API is much harder than REST API.  The SOAP Request is larger than the REST API Request.

REST API: 

DocuSign REST API has all features of SOAP API.  REST API is very much simpler to implement and it can also provide a secured way of sending documents.  There is a limit of 1000 Callout per Hour.

Prerequisites:

  1. DocuSign Demo Account.  Get registered here https://secure.docusign.com/demo .
  1. AccountID, User Name, Integrator Key, TemplateID and Password.

To get the items mentioned in #2, please follow the below steps.

  1. Login into DocuSign account and Navigate to preference -> API.
  1. Enter a Name in ‘New Key Description’ field and Click Get Demo Integrator Key.  The credentials will get created as show below,

Image

  1. Navigate to Manage section and Create a new template, upload the document if necessary and the templateID will be generated as shown below,

docusign rest api

 

The following coding is the simple way to send envelopes via Apex custom coding.  The simplified version of DocuSign REST API code is as follows,

public class DocuSign { 

   /* DocuSign Rest API webservice class to send an envelope. 

    * Created for Demo Purpose. 

    * */ 

  

 /******  Method to send docusign envelope the return the envelope status  ******/ 

 public String sendEnvelope(String email, String name){ 

      

     //Credentials are stored in variables. 

     String accountID = '48b4b1bf-013f-415b-****-8e417aae80ea'; 

     String userName = '36b6a456-e45f-40b3-****-b14cf095c340'; 

     String passWord = '*********'; 

     String integratorKey = 'TEST-363e8b0f-8f77-****-b8cf-f5da73a7d73e'; 

     String templateID = 'E61E5EEC-916E-4634-9F6A-75C71D36038B'; 

     //This Endpoint supports creating an envelope with the templateId. 

     String endPoint = 'https://demo.docusign.net/restapi/v2/accounts/'+accountID+'/envelopes'; 

      

     //Apex HTTP Method Initialization. 

     HttpRequest req = new HttpRequest(); 

     req.setEndpoint(endPoint); 

     req.setMethod('POST'); 

      

     //Authorization header generating using DocuSign credentials. 

     String authorizationHeader = '<DocuSignCredentials><Username>'+userName+'</Username><Password>'+Password+'</Password><IntegratorKey>'+integratorKey+'</IntegratorKey></DocuSignCredentials>'; 

     req.setHeader('X-DocuSign-Authentication', authorizationHeader); 

     req.setHeader('Accept','application/json'); 

     req.setHeader('Content-Length','162100'); 

     req.setHeader('Content-Type','application/json'); 

     req.setHeader('Content-Disposition','form-data'); 

      

     //JSON Request which contains template id and receivers email and name. 

     String json=      '{'+ 

         ' '+ 

         ' "emailSubject": "Agreement",'+ 

         ' "emailBlurb": "MSTSolutions is sending you this request for your electronic signature and enter or update confidential payment information.\n\nPlease review and electronically sign by following the link below. ",'+ 

         ' "templateId": "'+templateID+'",'+ 

         ' "envelopeIdStamping": "false",'+ 

         ' "templateRoles": ['+ 

         '  {'+ 

         '    "roleName": "Signer 1",'+ 

         '    "name": "'+name+'",'+ 

         '    "email": "'+email+'",'+ 

         '    "recipientId": "1"'+ 

         '   }'+ 

         ' ],'+ 

         ' "status": "sent"'+ 

         '}'; 

      

     req.setBody(json); 

      

     Http http = new Http(); 

     HTTPResponse res; 

     try{ 

         //Docusign Request Callout. 

         res = http.send(req); 

         system.debug('DocuSign Response'+ res.getBody());                      

     } 

     catch(Exception e){ 

         ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.FATAL, e.getMessage())); 

     } 

   /********   The response from the Docusign is sent to wrapper class to deserialize.    *********/ 

   WrapperDocuSign wrapInst = WrapperDocuSign.parse(res.getBody()); 

   Return wrapInst.status; 

 } 

}

 

The callout request to the DocuSign is sent as JSON and it can be generated by creating wrapper class with serialize method and the response can be deserialized in the wrapper class.  The wrapper class with deserialize method used for our response is as follows,

public class WrapperDocuSign { 

  /* Wrapper class to deserialize the Docusign response. 

   * Created for Demo Purpose. 

   * */ 

  public String envelopeId; 

  public String uri; 

  public String statusDateTime; 

  public String status; 

   

  public static WrapperDocuSign  parse(String json) { 

      return (WrapperDocuSign ) System.JSON.deserialize(json, WrapperDocuSign .class); 

  } 

} 

Now, the DocuSign class can be called by creating an instance from any controllers like shown below.

DocuSign Sign = New DocuSign(); 

Sign.sendEnvelope('venkatesh@mstsolutions.com','Venkatesh'); 

To test out the flow, run the above code snippet in developer console, as shown below.

And the response will be generated as below.

docusign rest api

By using DocuSign API with the apex code, complex e-Sign business requirements can be implemented in a simple and efficient way.

Reference Link: https://www.docusign.com/p/RESTAPIGuide/RESTAPIGuide.htm.