DocuSign Integration on Ruby on Rails

DocuSign is a platform for the online signing of documents, legal agreements where you as an application owner or developer can send documents to the end-users for signing process after approval/consent from DocuSign & users can sign with their own electronic signature.

Remote Signing

Pricing And Plans

Docusign comes with a three plan Personal, Standard, and Business Pro plans.

Apart from document signing, DocuSign offers features like reminder notifications, payment gateway, bulk-sending of templates to multiple users in premium plans.

Go through below link for details :

Getting Started

Gem Used : docusign_esign

gem 'docusign_esign'
bundle install

Account and keys setup :

  1. Signup: create your DocuSign account
  2. Login to your demo account
  3. Account ID (Click on upper right avatar and get your account id)
  4. Get Integration key (Go to settings -> Integration -> API and Keys -> My Apps / Integration Keys -> Add key). Also, copy the user-id and base-URI listed on the same page.
  5. In the My Apps / Integration Keys section on API and Keys page click on Actions -> Edit. Now, in the service integration section add RSA key and copy the public key and private key in config/public_key.txt & config/private_key.txt respectively.
  6. In additional settings, add a redirect URL to get redirected to the application after getting DocuSign consent. For local development http://localhost:3000/***.
  7. Now, add all the keys in application.yml or .env of your application.
BASE_URL: 'http://localhost:3000'
DOCUSIGN_USERNAME: '****@gmail.com'
DOCUSIGN_PASSWORD: '*******'
DOCUSIGN_INTEGRATION_KEY: 'e8**-2***-7***-6***-a********'
DOCUSIGN_ACCOUNT_ID: '11*****'
DOCUSIGN_USER_ID: '8******-1***-4***-9***-e********'
DOCUSIGN_ENDPOINT: 'http://demo.docusign.net/restapi'
DOCUSIGN_API_VERSION: 'v2'
DOCUSIGN_AUTH_SERVER: 'account-d.docusign.com'

DocuSign Integration in Application :

There are two ways in which the signing process can be executed:

  1. Remote Signing (Signing via mail)
  2. Embedded signing (Signing within the application)

I will go deep down into integrating the remote signing process while for embedded signing you can check the launcher code example here.

Firstly, create some templates on your remote account to be sent to the end-users within an envelope for signing.

Step 1. Get Docusign Consent for application :

(I have used gem Figaro for keys configuration)

integration_key = Figaro.env.DOCUSIGN_INTEGRATION_KEY
redirect_uri = "#{Figaro.env.BASE_URL}"
consent_scopes = "signature%20impersonation"
path = "https://#{Figaro.env.DOCUSIGN_AUTH_SERVER}/oauth/auth?
response_type=code&scope=#{consent_scopes}&client_id=#{integration_key}&redirect_uri=#{redirect_uri}"
redirect_to path

After getting authorized, DocuSign redirects back to the redirect URI.

Step 2. Initiate api_client object

template_name = 'template name' #search name from remote
configuration = DocuSign_eSign::Configuration.new
configuration.host = base_path
api_client = DocuSign_eSign::ApiClient.new(configuration)

Step 3. Get JWT bearer token

base_path = Figaro.env.DOCUSIGN_ENDPOINT
integrator_key = Figaro.env.DOCUSIGN_INTEGRATION_KEY
user_id = Figaro.env.DOCUSIGN_USER_ID
expires_in_seconds = 5000
api_client.set_base_path(base_path)
private_key_filename = File.join(Rails.root, 'config', 'private_key.txt')
bearer_token = api_client.request_jwt_user_token(integrator_key, user_id, private_key_filename, expires_in_seconds)

Step 4. Access template from remote

@template_name = template_name
templates_api = DocuSign_eSign::TemplatesApi.new api_client
options = DocuSign_eSign::ListTemplatesOptions.new
options.search_text = @template_name
results = templates_api.list_templates(Figaro.env.DOCUSIGN_ACCOUNT_ID, options)template_id = results.envelope_templates[0].template_id

Step 5. Create envelope definition and send to end user via mail

user = current_user
envelope_definition= DocuSign_eSign::EnvelopeDefinition.new({status: 'sent', templateId: template_id, emailSubject: 'Document Signing'})
signer = DocuSign_eSign::TemplateRole.new({email: user.email, name: user.full_name, roleName: 'signer'})signer.name = 'Default User' unless signer.name.present?envelope_definition.template_roles = [signer]envelopes_api = DocuSign_eSign::EnvelopesApi.new(api_client)result = envelopes_api.create_envelope(account_id, envelope_definition)

That’s it! an email with an envelope containing metadata and template to get signed is sent successfully to the user.

DocuSign Connect

Get notified when users review or complete the signing process and hence update your database in your application.

For this purpose, I added a webhook in my application which gets called every time the envelope status changes like sent, delivered, completed, declined, voided.

Configuring Connect

  1. Go to Settings -> Integration -> Connect
  2. Add webhook URL -> https://3b******.ngrok.io/webhooks

(I added ngrok URL as connect accepts https secured URL only)

3. Now, add events to get triggered and save.

Coming to the code ..

resp = Hash.from_xml(request.raw_post)
envelope_id = resp["DocuSignEnvelopeInformation"]["EnvelopeStatus"]["EnvelopeID"]
envelope = Envelope.find_by(envelope_id: envelope_id)
envelope.update(status: resp["DocuSignEnvelopeInformation"]["EnvelopeStatus"]["Status"].downcase)

Software Engineer at Systango

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store