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:
Usage
To initialize the client, make the Login API Call and send a template for signature:
SDK version 3.x.x
Using DocuSign passport-based Oauth Client for Express
run node test/OAuthClientTests.js and then open http://localhost:3000/auth.
const express = require('express');
const passport = require('passport');
var session = require('express-session');
var docusign = require('../src/index');
const app = express();
const port = process.env.PORT || 3000;
const host = process.env.HOST || 'localhost';
app.use(session({
secret: 'secret token',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
var hostUrl = 'http://' + host + ':' + port;
passport.use(new docusign.OAuthClient({
sandbox: true,
clientID: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
callbackURL: hostUrl + '/auth/callback'
},
function (accessToken, refreshToken, user, done) {
user.accessToken = accessToken;
user.refreshToken = refreshToken;
return done(null, user);
}
));
app.get('/auth', function (req, res) {
passport.authenticate('docusign')(req, res);
});
app.get('/auth/callback', function (req, res) {
passport.authenticate('docusign', function (err, user) {
if (err) {
return res.send(err);
}
if (!user) {
return res.redirect('/auth');
}
var apiClient = new docusign.ApiClient();
var BaseUrl = 'https://demo.docusign.net/restapi';
apiClient.setBasePath(BaseUrl);
apiClient.addDefaultHeader('Authorization', 'Bearer ' + user.accessToken);
var authApi = new docusign.AuthenticationApi(apiClient);
var loginOps = {};
loginOps.apiPassword = 'true';
loginOps.includeAccountIdGuid = 'true';
authApi.login(loginOps, function (error, loginInfo, response) {
if (error) {
return res.send(error);
}
if (loginInfo) {
var loginAccounts = loginInfo.loginAccounts;
var loginAccount = loginAccounts[0];
var baseUrl = loginAccount.baseUrl;
var accountDomain = baseUrl.split('/v2');
apiClient.setBasePath(accountDomain[0]);
docusign.Configuration.default.setDefaultApiClient(apiClient);
return res.send(loginAccounts);
}
});
})(req, res);
});
app.listen(port, host, function (err) {
if (err) {
throw err;
}
console.log('Your server is running on http://' + host + ':' + port + '.');
});
Using 2-legged authentication
Run this script using node command
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, next);
},
function login (next) {
var authApi = new docusign.AuthenticationApi();
var loginOps = {};
loginOps.apiPassword = 'true';
loginOps.includeAccountIdGuid = 'true';
authApi.login(loginOps, function (err, loginInfo, response) {
if (err) {
return next(err);
}
if (loginInfo) {
var loginAccounts = loginInfo.loginAccounts;
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.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
Run this script using node command
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.