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 via the NPM Package Manager
npm install docusign-esign --save
Dependencies
This client has the following external dependencies:
- jsonwebtoken@8.2.0
- passport-oauth2@1.4.0
- superagent@3.8.2
Usage
OAuth Authorization Code Grant
See the Node.JS OAuth Authorization Code Grant flow
example for Node.JS 8.10 or later. It also shows how to use the SDK with promises instead of callback functions.
The following example uses callback functions.
uncomment auth code grant section in test/OAuthClientTests.js, run it and then open http://localhost:3000.
const express = require('express');
const docusign = require('docusign-esign');
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.addDefaultHeader('Authorization', 'Bearer ' + oAuthToken.accessToken);
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 JSON Web Token (JWT) Grant
See the Node.JS Service Integration example for Node 8.10 or later.
It uses the OAuth JWT Grant flow. It also demonstrates how to use the SDK with promises.
The following example can be used with an older version of Node.JS.
var docusign = require('docusign-esign');
var oAuth = docusign.ApiClient.OAuth;
var restApi = docusign.ApiClient.RestApi;
var async = require('async');
var path = require('path');
var integratorKey = '***';
var userId = 'YOUR_USER_ID';
var fullName = 'Joan Jett';
var recipientEmail = 'joan.jett@example.com';
var templateId = '***';
var templateRoleName = '***';
var expiresIn = 3600;
var basePath = restApi.BasePath.DEMO;
var oAuthBasePath = oAuth.BasePath.DEMO;
var redirectURI = 'https://www.docusign.com/api';
var privateKeyFilename = 'keys/docusign_private_key.txt';
var apiClient = new docusign.ApiClient({
basePath: basePath,
oAuthBasePath: oAuthBasePath
});
var scopes = [
oAuth.Scope.IMPERSONATION,
oAuth.Scope.SIGNATURE
];
async.waterfall([
function initApiClient (next) {
docusign.Configuration.default.setDefaultApiClient(apiClient);
var oauthLoginUrl = apiClient.getJWTUri(integratorKey, redirectURI, oAuthBasePath);
console.log(oauthLoginUrl);
var fs = require('fs');
var privateKeyFile = fs.readFileSync(path.resolve(__dirname, privateKeyFilename));
apiClient.requestJWTUserToken(integratorKey, userId, scopes, privateKeyFile, expiresIn, function (err, res) {
var baseUri,
accountDomain;
if (err) {
return next(err);
}
apiClient.addDefaultHeader('Authorization', 'Bearer ' + res.body.access_token);
apiClient.getUserInfo(res.body.access_token, function (err, userInfo) {
if (err) {
return next(err);
}
accountId = userInfo.accounts[0].accountId;
baseUri = userInfo.accounts[0].baseUri;
accountDomain = baseUri.split('/v2');
apiClient.setBasePath(accountDomain[0] + '/restapi');
console.log('LoginInformation: ' + JSON.stringify(userInfo));
return 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();
});
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('docusign-esign');
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.addDefaultHeader('Authorization', 'Bearer ' + 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 + '.');
});
The basePath
This section applies to applications which use OAuth for authentication with DocuSign.
The SDK must be configured to use the correct basePath
for the accredited user's DocuSign account.
To determine the user's basePath:
-
After obtaining a Bearer token, call the
OAuth::userInfo endpoint.
The getUserInfo
method can be used to call the OAuth::userInfo endpoint. See the file
ApiClient.js, line 713.
Use the results to choose the account. One of the user's accounts is their default account.
The method's results include the selected account's base_uri
field.
Note: The host for the OAuth::userInfo method is account-d.docusign.com
for the demo/developer environment,
and account.docusign.com
for the production environments.
-
Combine the base_uri with "/restapi" to create the basePath.
Use the basePath for your subsequent API calls for the account id.
You can and should cache the basePath for at least the user's session with your application. It changes very infrequently.
-
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 a DocuSign OAuth flow.
See the authentication guide.
-
Set the SDK's 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 MIT License.