Skip to main content

Example Project

This example project takes you step-by-step through creating a NodeJS module with the SDK that will automatically send documents from your account with an example template. You can use any IDE (e.g. VS Code) or text editor to make your changes. We assume you have a recent installation of NodeJS, if not install it from the NodeJS Official Website.

Installation

In the root of your coding directory use the following terminal commands create a folder (you can use the name we've picked or choose one of your own) and change directory to that new folder.

  mkdir lesdk-example

cd lesdk-example

Prepare Node in the directory by initialising it with the following command

npm init -y

Configure the Typescript compiler

Let's get typescript set and install the node types.

npm install --save-dev typescript
npm i --save-dev @types/node

Open and edit the tsconfig.json file.

tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist"
},
"lib": ["es2015"]
}

Create and edit the send.ts file and copy into it the following, not very exciting, code. Don't worry we'll get onto more ambitious tasks very soon.

console.log("Executing your esignature document commands");

Check Build and Run

Let's make sure that everything we've set up is working properly. Build (npx tsc) and run (node dist/send.js) your module with the two following commands. Notice that our options in tsconfig.json have sent the transpiled output to the dist directory.

npx tsc
node dist/send

After executing the following you should see:

Executing your esignature document commands

(Optionally) put these into your package.json settings by replacing the script block with the following:

package.json
  "scripts": {
"run": "node dist/send",
"build": "npx tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},

this means we're ready to add some much more exciting elements to our code.

Install the SDK and Import

Install the SDK with the command

npm install legalesign-sdk-js

Back in your send.ts file lets import some key objects from the SDK.

send.ts
import { Legalesign, Recipient, SendOptions } from "legalesign-sdk-js";

The Legalesign object is the centre of every interaction you'll make with the esignature platform and Legalesign systems. It handles your authentication, session, shaping your requests and much more. Recipient and SendOptions are useful types provided by the SDK.

Recipient is one of the core entities in the SDK, the others are Organisation, Template, Experience, Document, Batch, Group, User, Task and Draft. These allow a developer to easily generate or receive valid data and write code it. Every one of the core objects will have an id property which is a base64 unique identifier. For now we only need to use Recipient.

SendOptions allows you to define any of the multitude of ways you can send documents off to be processed by the platform. It can include some very advanced option to handle multiple document packets and bulk sending but for this example we are going to stick with the simplest way to send a single document to a single signer.

Let's add some code to grab some arguments when our program is run and instanciate our first instance of the Legalesign SDK

note

You'll need to insert your development or api credentials in this code to test run it. When the code runs, if you get a credentials exception you'll need to check these are valid.

send.ts
const args = process.argv.slice(2);

// Check that we have the correct number of arguments (you could validate these arguments much more strictly)
if(!args || args?.length !== 3) {
console.log(`Invalid options:: ${args.toString()}\nusage:send [firstname] [lastname] [email address]`);
} else {
console.log('Executing your esignature document commands...');

const lesign = new Legalesign({
organisationId: "b3XXXXX",
options: {
apiUser: "api@legalesign.com",
apiPassword: "yourapipassword",
},
});
}

Let's expand this to include construction of a send request. You'll need two critical bits of data for this:

The id of the template that you want to send to the customer. You can find this if you edit a template in the Console application, e.g. https://console.legalesign.com/template/dHBsMjE2NzRiOTktZjJjNy0xMWVmLWI3ZDItMDYXNzZlZmU0MzIx/edit where dHBsMjE2NzRiOTktZjJjNy0xMWVmLWI3ZDItMDYXNzZlZmU0MzIx is the id for that template.

The id of the group that you want to send to the customer. You can find this if look at the document listing in the Console application, e.g. https://console.legalesign.com/group/Z3JwYWZlMTYxZmYtMDlhZC0xMWVkLTkwMDctMGE1ZjJiNzljNDVl/listings where Z3JwYWZlMTYxZmYtMDlhZC0xMWVkLTkwMDctMGE1ZjJiNzljNDVl is the id for your current group.

Let's go ahead and expand our program to do something genuinely useful. Put the following code in your send.ts file and rebuild the project with npx tsc.

send.ts
import { Legalesign, Recipient, SendOptions } from 'legalesign-sdk-js';

const args = process.argv.slice(2);

// Check that we have the correct number of arguments (you could validate these arguments much more strictly)
if(!args || args?.length !== 3) {
console.log(`Invalid options:: ${args.toString()}\nusage:send [firstname] [lastname] [email address]`);
} else {
console.log('Executing your esignature document commands...');

const lesign = new Legalesign({
options: {
apiUser: "api@legalesign.com",
apiPassword: "yourapipassword",
},
});

const recipient: Recipient = {
email: "recipient@customer.com",
firstName: "Sidney",
lastName: "Lawrence",
};

const sendOptions: SendOptions = {
title: "SDK Test Simple Send",
groupId: "Z3GROUPID",
templateId: "dHBTEMPLATEID",
recipients: [recipient],
};

lesign.sender.send(sendOptions).then(()=> console.log('Document send.'));
}