-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.js
executable file
·139 lines (126 loc) · 4.95 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// to run this sample
// 1. copy the file in your own directory - say, example.js
// 2. change "***" to appropriate values
// 3. install async and request packages
// npm install async
// npm install request
// 4. execute
// node example.js
//
var async = require("async"), // async module
request = require("request"), // request module
email = "[email protected]", // your account email
password = "esitproject2016", // your account password
integratorKey = "0edc98fa-39d4-46f2-baeb-09e7a0711e8c", // your account Integrator Key (found on Preferences -> API page)
recipientName = "Klever", // recipient (signer) name
templateId = "2065d181-3836-4b1e-829a-89133e49a444", // provide valid templateId from a template in your account
templateRoleName = "Customer", // template role that exists on template referenced above
baseUrl = "", // we will retrieve this
envelopeId = ""; // created from step 2
async.waterfall(
[
//////////////////////////////////////////////////////////////////////
// Step 1 - Login (used to retrieve accountId and baseUrl)
//////////////////////////////////////////////////////////////////////
function(next) {
var url = "https://demo.docusign.net/restapi/v2/login_information";
var body = ""; // no request body for login api call
// set request url, method, body, and headers
var options = initializeRequest(url, "GET", body, email, password);
// send the request...
request(options, function(err, res, body) {
if(!parseResponseBody(err, res, body)) {
return;
}
baseUrl = JSON.parse(body).loginAccounts[0].baseUrl;
next(null); // call next function
});
},
//////////////////////////////////////////////////////////////////////
// Step 2 - Send envelope with one Embedded recipient (using clientUserId property)
//////////////////////////////////////////////////////////////////////
function(next) {
var url = baseUrl + "/envelopes";
var body = JSON.stringify({
"emailSubject": "DocuSign API call - Embedded Sending Example",
"templateId": templateId,
"templateRoles": [{
"email": email,
"name": recipientName,
"roleName": templateRoleName,
"clientUserId": "1001" // user-configurable
}],
"status": "sent"
});
// set request url, method, body, and headers
var options = initializeRequest(url, "POST", body, email, password);
// send the request...
request(options, function(err, res, body) {
if(!parseResponseBody(err, res, body)) {
return;
}
// parse the envelopeId value from the response
envelopeId = JSON.parse(body).envelopeId;
next(null); // call next function
});
},
//////////////////////////////////////////////////////////////////////
// Step 3 - Get the Embedded Signing View (aka the recipient view)
//////////////////////////////////////////////////////////////////////
function(next) {
var url = baseUrl + "/envelopes/" + envelopeId + "/views/recipient";
var method = "POST";
var body = JSON.stringify({
"returnUrl": "http://www.docusign.com/devcenter",
"authenticationMethod": "email",
"email": email,
"userName": recipientName,
"clientUserId": "1001", // must match clientUserId in step 2!
});
// set request url, method, body, and headers
var options = initializeRequest(url, "POST", body, email, password);
// send the request...
request(options, function(err, res, body) {
if(!parseResponseBody(err, res, body))
return;
else
console.log("\nNavigate to the above URL to start the Embedded Signing workflow...");
});
}
]);
//***********************************************************************************************
// --- HELPER FUNCTIONS ---
//***********************************************************************************************
function initializeRequest(url, method, body, email, password) {
var options = {
"method": method,
"uri": url,
"body": body,
"headers": {}
};
addRequestHeaders(options, email, password);
console.log(options);
return options;
}
///////////////////////////////////////////////////////////////////////////////////////////////
function addRequestHeaders(options, email, password) {
// JSON formatted authentication header (XML format allowed as well)
dsAuthHeader = JSON.stringify({
"Username": email,
"Password": password,
"IntegratorKey": integratorKey // global
});
// DocuSign authorization header
options.headers["X-DocuSign-Authentication"] = dsAuthHeader;
}
///////////////////////////////////////////////////////////////////////////////////////////////
function parseResponseBody(err, res, body) {
console.log("\r\nAPI Call Result: \r\n", JSON.parse(body));
if( res.statusCode != 200 && res.statusCode != 201) { // success statuses
console.log("Error calling webservice, status is: ", res.statusCode);
console.log("\r\n", err);
return false;
}
return true;
}