Skip to content

Instantly share code, notes, and snippets.

@Ergin008
Last active March 22, 2016 17:35
Show Gist options
  • Save Ergin008/89fe9cd92065a51e78a5 to your computer and use it in GitHub Desktop.
Save Ergin008/89fe9cd92065a51e78a5 to your computer and use it in GitHub Desktop.
code snippet that shows how to create and send an envelope from a document using the docusign-node client
function createAndSendEnvelope (loginAccounts, next) {
var fileBytes = null;
try {
var fs = require('fs'),
path = require('path');
// read file from a local directory
fileBytes = fs.readFileSync(path.resolve(__filename + '/..' + SignTest1File));
} catch (ex) {
// handle error
console.log("Exception: " + ex);
}
// create a new envelope object that we will manage the signature request through
var envDef = new docusign.EnvelopeDefinition();
envDef.setEmailSubject("[DocuSign Node SDK] - Please sign this doc");
// add a document to the envelope
var doc = new docusign.Document();
var base64Doc = new Buffer(fileBytes).toString('base64');
doc.setDocumentBase64(base64Doc);
doc.setName("TestFile.pdf");
doc.setDocumentId("1");
var docs = [];
docs.push(doc);
envDef.setDocuments(docs);
// Add a recipient to sign the document
var signer = new docusign.Signer();
signer.setName("[RECIPIENT_NAME]");
signer.setEmail("[RECIPIENT_EMAIL]");
signer.setRecipientId("1");
// create a signHere tab somewhere on the document for the signer to sign
// default unit of measurement is pixels, can be mms, cms, inches also
var signHere = new docusign.SignHere();
signHere.setDocumentId("1");
signHere.setPageNumber("1");
signHere.setRecipientId("1");
signHere.setXPosition("100");
signHere.setYPosition("100");
// can have multiple tabs, so need to add to envelope as a single element list
var signHereTabs = [];
signHereTabs.push(signHere);
var tabs = new docusign.Tabs();
tabs.setSignHereTabs(signHereTabs);
signer.setTabs(tabs);
// configure the envelope's recipient(s)
envDef.setRecipients(new docusign.Recipients());
envDef.getRecipients().setSigners([]);
envDef.getRecipients().getSigners().push(signer);
// send the envelope (otherwise it will be "created" in the Draft folder)
envDef.setStatus("sent");
var envelopesApi = new docusign.EnvelopesApi();
envelopesApi.createEnvelope(loginAccounts[0].accountId, envDef, null, function(error, envelopeSummary, response) {
if (error) {
return next(error);
}
if (envelopeSummary) {
console.log("EnvelopeSummary: " + JSON.stringify(envelopeSummary));
envelopeId = envelopeSummary.envelopeId;
next();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment