The Official DocuSign Node Client
NPM module that wraps the DocuSign API
Documentation about the DocuSign API
Documentation about this package
You can sign up for a free developer sandbox.
Requirements
Node 4 or later.
Installation
NPM Package Manager
Install the client locally: npm install docusign-esign --save --save-exact
(note you may have to use sudo
based on your permissions)
Alternatively you can just copy the source code directly into your project.
Dependencies
This client has the following external dependencies:
- jsonwebtoken@8.2.0
- passport-oauth2@1.4.0
- superagent@3.8.2
Usage
To initialize the client, make the Login API Call and send a template for signature:
SDK version 3.x.x
OAuth Authorization Code Grant
uncomment auth code grant section in test/OAuthClientTests.js, run it and then open http://localhost:3000.
const express = require('express');
const docusign = require('../src/index');
const apiClient = new docusign.ApiClient();
const app = express();
const port = process.env.PORT || 3000;
const host = process.env.HOST || 'localhost';
const integratorKey = 'ae30ea4e-3959-4d1c-b867-fcb57d2dc4df';
const clientSecret = 'b4dccdbe-232f-46cc-96c5-b2f0f7448f8f';
const redirectUri = 'http://localhost:3000/auth';
const basePath = 'https://demo.docusign.net/restapi';
const responseType = apiClient.OAuth.ResponseType.CODE;
const scopes = [apiClient.OAuth.Scope.EXTENDED];
const randomState = "*^.$DGj*)+}Jk";
apiClient.setBasePath(basePath);
app.get('/', function (req, res) {
const authUri = apiClient.getAuthorizationUri(integratorKey, scopes, redirectUri, responseType, randomState);
res.redirect(authUri);
});
app.get('/auth', function (req, res) {
apiClient.generateAccessToken(integratorKey, clientSecret, req.query.code, function (err, oAuthToken) {
console.log(oAuthToken);
apiClient.getUserInfo(oAuthToken.accessToken, function (err, userInfo) {
console.log("UserInfo: " + userInfo);
apiClient.setBasePath(userInfo.accounts[0].baseUri + "/restapi");
res.send(userInfo);
});
});
});
app.listen(port, host, function (err) {
if (err)
throw err;
console.log('Your server is running on http://' + host + ':' + port + '.');
});
OAuth Implicit Grant
uncomment implicit grant section in test/OAuthClientTests.js, run it and then open http://localhost:3000.
const express = require('express');
const docusign = require('../src/index');
const apiClient = new docusign.ApiClient();
const app = express();
const port = process.env.PORT || 3000;
const host = process.env.HOST || 'localhost';
const integratorKey = '68c1711f-8b19-47b1-888f-b49b4211d831';
const redirectUri = 'http://localhost:3000/auth';
const basePath = 'https://demo.docusign.net/restapi';
const responseType = apiClient.OAuth.ResponseType.TOKEN;
const scopes = [apiClient.OAuth.Scope.EXTENDED];
const randomState = "*^.$DGj*)+}Jk";
apiClient.setBasePath(basePath);
app.get('/', function (req, res) {
const authUri = apiClient.getAuthorizationUri(integratorKey, scopes, redirectUri, responseType, randomState);
res.redirect(authUri);
});
app.get('/auth', function (req,res) {
res.send();
});
app.get('/auth/:accessToken', function (req, res) {
const accessToken = req.params.accessToken;
apiClient.getUserInfo(accessToken, function (err, userInfo) {
if (err)
console.log(err)
console.log("UserInfo: " + userInfo);
apiClient.setBasePath(userInfo.accounts[0].baseUri + "/restapi");
res.send(userInfo);
});
});
app.listen(port, host, function(err) {
if (err)
throw err;
console.log('Your server is running on http://' + host + ':' + port + '.');
});
Using JSON Web Token Bearer Grant
var docusign = require('docusign-esign');
var async = require('async');
var path = require('path');
var integratorKey = '***';
var email = 'YOUR_EMAIL';
var password = 'YOUR_PASSWORD';
var docusignEnv = 'demo';
var fullName = 'Joan Jett';
var recipientEmail = 'joan.jett@example.com';
var templateId = '***';
var templateRoleName = '***';
var baseUrl = 'https://' + docusignEnv + '.docusign.net/restapi';
var userId = 'YOUR_USER_ID';
var oAuthBaseUrl = 'account-d.docusign.com';
var redirectURI = 'https://www.docusign.com/api';
var privateKeyFilename = 'keys/docusign_private_key.txt';
var apiClient = new docusign.ApiClient();
async.waterfall([
function initApiClient (next) {
apiClient.setBasePath(baseUrl);
docusign.Configuration.default.setDefaultApiClient(apiClient);
var oauthLoginUrl = apiClient.getJWTUri(integratorKey, redirectURI, oAuthBaseUrl);
console.log(oauthLoginUrl);
apiClient.configureJWTAuthorizationFlow(path.resolve(__dirname, privateKeyFilename), oAuthBaseUrl, integratorKey, userId, 3600, function (err, res) {
if (!err && res.body && res.body.access_token) {
apiClient.getUserInfo(res.body.access_token, function (err, userInfo) {
accountId = userInfo.accounts[0].accountId;
var baseUri = userInfo.accounts[0].baseUri;
var accountDomain = baseUri.split('/v2');
apiClient.setBasePath(accountDomain[0] + "/restapi");
console.log('LoginInformation: ' + JSON.stringify(userInfo.accounts));
next(null, userInfo.accounts[0]);
});
}
});
},
function sendTemplate (loginAccount, next) {
var envDef = new docusign.EnvelopeDefinition();
envDef.emailSubject = 'Please sign this document sent from Node SDK';
envDef.templateId = templateId;
var tRole = new docusign.TemplateRole();
tRole.roleName = templateRoleName;
tRole.name = fullName;
tRole.email = recipientEmail;
var templateRolesList = [];
templateRolesList.push(tRole);
envDef.templateRoles = templateRolesList;
envDef.status = 'sent';
var accountId = loginAccount.accountId;
var envelopesApi = new docusign.EnvelopesApi();
envelopesApi.createEnvelope(accountId, {'envelopeDefinition': envDef}, function (err, envelopeSummary, response) {
if (err) {
return next(err);
}
console.log('EnvelopeSummary: ' + JSON.stringify(envelopeSummary));
next(null);
});
}
], function end (error) {
if (error) {
console.log('Error: ', error);
process.exit(1);
}
process.exit();
});
SDK version 2.x.x using 2-legged authentication only
var docusign = require('docusign-esign');
var async = require('async');
var integratorKey = '***';
var email = 'YOUR_EMAIL';
var password = 'YOUR_PASSWORD';
var docusignEnv = 'demo';
var fullName = 'Joan Jett';
var recipientEmail = 'joan.jett@example.com';
var templateId = '***';
var templateRoleName = '***';
var baseUrl = 'https://' + docusignEnv + '.docusign.net/restapi';
var apiClient = new docusign.ApiClient();
apiClient.setBasePath(baseUrl);
var creds = JSON.stringify({
Username: email,
Password: password,
IntegratorKey: integratorKey
});
apiClient.addDefaultHeader('X-DocuSign-Authentication', creds);
docusign.Configuration.default.setDefaultApiClient(apiClient);
async.waterfall([
function login (next) {
var authApi = new docusign.AuthenticationApi();
var loginOps = new authApi.LoginOptions();
loginOps.setApiPassword('true');
loginOps.setIncludeAccountIdGuid('true');
authApi.login(loginOps, function (err, loginInfo, response) {
if (err) {
return next(err);
}
if (loginInfo) {
var loginAccounts = loginInfo.getLoginAccounts();
console.log('LoginInformation: ' + JSON.stringify(loginAccounts));
var loginAccount = loginAccounts[0];
var accountId = loginAccount.accountId;
var baseUrl = loginAccount.baseUrl;
var accountDomain = baseUrl.split("/v2");
apiClient.setBasePath(accountDomain[0]);
docusign.Configuration.default.setDefaultApiClient(apiClient);
next(null, loginAccount);
}
});
},
function sendTemplate (loginAccount, next) {
var envDef = new docusign.EnvelopeDefinition();
envDef.setEmailSubject('Please sign this document sent from Node SDK');
envDef.setTemplateId(templateId);
var tRole = new docusign.TemplateRole();
tRole.setRoleName(templateRoleName);
tRole.setName(fullName);
tRole.setEmail(recipientEmail);
var templateRolesList = [];
templateRolesList.push(tRole);
envDef.setTemplateRoles(templateRolesList);
envDef.setStatus('sent');
var accountId = loginAccount.accountId;
var envelopesApi = new docusign.EnvelopesApi();
envelopesApi.createEnvelope(accountId, envDef, null, function (err, envelopeSummary, response) {
if (err) {
return next(err);
}
console.log('EnvelopeSummary: ' + JSON.stringify(envelopeSummary));
next(null);
});
}
], function end (error) {
if (error) {
console.log('Error: ', error);
process.exit(1);
}
process.exit();
});
See CoreRecipes.js for more examples.
Sample App
Check out the LoanCo sample app - an open source app that showcases the Node.js SDK and demonstrates several common DocuSign workflows and features:
Run the app: https://loancosample.docusign.com/
Get the code: https://github.com/docusign/sample-app-loanco-nodejs
Authentication
(Legacy Header Authentication uses the X-DocuSign-Authentication header.)
- Use the Authentication: login method to retrieve the account number and the baseUrl for the account.
The url for the login method is www.docusign.net for production and demo.docusign.net for the developer sandbox.
The
baseUrl
field is part of the loginAccount
object. See the docs and the loginAccount object - The baseUrl for the selected account, in production, will start with na1, na2, na3, eu1, or something else. Use the baseUrl that is returned to create the basePath (see the next step.) Use the basePath for all of your subsequent API calls.
- As returned by login method, the baseUrl includes the API version and account id. Split the string to obtain the basePath, just the server name and api name. Eg, you will receive
https://na1.docusign.net/restapi/v2/accounts/123123123
. You want just https://na1.docusign.net/restapi
- Instantiate the SDK using the basePath. Eg
ApiClient apiClient = new ApiClient(basePath);
- Set the authentication header as shown in the examples by using
Configuration.Default.AddDefaultHeader
User Applications that use OAuth Authentication
- After obtaining a Bearer token, call the OAuth: Userinfo method. Obtain the selected account's
base_uri
(server name) field.
The url for the Userinfo method is account-d.docusign.com for the demo/developer environment, and account.docusign.com for the production environment. - Combine the base_uri with "/restapi" to create the basePath. The base_uri will start with na1, na2, na3, eu1, or something else. Use the basePath for your subsequent API calls.
- Instantiate the SDK using the basePath. Eg
ApiClient apiClient = new ApiClient(basePath);
- Create the
authentication_value
by combining the token_type
and access_token
fields you receive from either an Authorization Code Grant or Implicit Grant OAuth flow. - Set the authentication header by using
Configuration.Default.AddDefaultHeader('Authorization', authentication_value)
Testing
Unit tests are available in the Test folder.
Contributing
This SDK is auto-generated from OpenAPI specification file. For that reason, we actually do NOT accept pull requests. If you find a bug or have an idea that you want to see in the SDK, please open a new issue.
Support
Feel free to log issues against this client through GitHub. We also have an active developer community on Stack Overflow, search the DocuSignAPI tag.
License
The DocuSign Node Client is licensed under the following License.