-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-link.js
80 lines (66 loc) · 2.35 KB
/
example-link.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
// Get your free API Key at: https://www.ontask.io/solutions/ontask-api/
// Full OnTask API Documentation: https://docs.ontask.io/#overview
const fs = require('fs');
const fetch = require('node-fetch');
const ontaskUrl = 'https://app.ontask.io/api/v2';
const apiKey = 'INSERT-API-KEY';
const emailFinalized = "[email protected]";
// Upload the file and get a documentid to pass to create a signature endpoint
// https://docs.ontask.io/#upload
const uploadSampleDocument = async () => {
const readStream = fs.createReadStream('./sample-files/sample.pdf');
const response = await fetch(`${ontaskUrl}/documents`, {
method: 'POST',
headers: {
'Authorization': apiKey,
'Content-Type': 'application/pdf'
},
body: readStream
});
const { documentId } = await response.json();
return documentId;
}
// Call /signature endpoint to start signature process
// https://docs.ontask.io/#signature-api
const startSignature = async (documentId) => {
const requestBody = JSON.stringify({
documents: [ { documentId: documentId } ],
testMode: true,
signers: [
{
label: 'John Smith',
contactMethod: [
{
type: 'link'
}
]
}
],
"onSignaturesFinalized": [
{
type: 'email',
email: emailFinalized
}
]
});
const response = await fetch(`${ontaskUrl}/signatures`, {
method: 'POST',
headers: {
'Authorization': apiKey,
'Content-Type': 'application/json'
},
body: requestBody
});
const signatureResponse = await response.json();
return signatureResponse;
}
(async () => {
console.log('\n##### Example Starting #####')
const documentId = await uploadSampleDocument();
console.log(`\nUploaded sample file and received a documentId of ${documentId}`)
const response = await startSignature(documentId);
console.log('\nURL for signer John Smith: (Follow URL to complete signature process)')
console.log(`${response['signers'][0]['contactMethod'][0]['taskUrl']}\n`);
console.log(`Once signed the final document will be sent to: ${emailFinalized}\n`)
console.log('##### Example Complete #####\n')
})();