maple-esign
v0.1.0
Published
TypeScript client for the Maple eSign REST API: templates, documents, bulk send, webhooks and embedded signing.
Maintainers
Readme
Maple eSign for JavaScript and TypeScript
A typed client for the Maple eSign v1 REST API: templates, documents, bulk send, field detection, embedded signing and sending, audit trails, sealed PDFs and webhooks. It has no runtime dependencies and runs anywhere with fetch: Node.js 20 or later, Deno, Bun, edge workers and browsers. It ships ES modules and CommonJS, with TypeScript types for both.
Install
npm install maple-esignFirst request
Create an API token in the Maple eSign console. A test token (ssk_test_...) works in the sandbox.
import { MapleClient } from 'maple-esign';
const maple = new MapleClient({ token: process.env.MAPLE_API_TOKEN! });
const documents = await maple.listDocuments({ limit: 10 });
for (const d of documents) console.log(d.title, d.status);CommonJS works the same way: const { MapleClient } = require('maple-esign');
The client calls https://sign.mapleesign.ca unless you pass baseUrl.
Send an agreement from a template
const doc = await maple.useTemplate(
'tpl_123',
{
recipients: { client: { name: 'Ada Lovelace', email: '[email protected]', locale: 'fr-CA' } },
title: 'Mutual NDA for Ada',
},
{ idempotencyKey: 'order-1234' }, // a retry with the same key returns the same document
);
const sent = await maple.sendDocument(doc.id);
console.log(sent.signingLinks[0]?.signingUrl);createDocument does the same from a PDF you upload. Both take an idempotencyKey, so a retry after a timeout never makes a second agreement.
When everyone has signed, downloadArtifact(id) returns the sealed PDF. It throws a 409 until then.
Test and live tokens
| Token | What it reaches |
|---|---|
| ssk_test_... | The sandbox. It sees and makes only test agreements. A test send emails only your own verified address and your organization's verified sending domains; any other address is refused with 422 test_recipient_not_allowed. Sealed PDFs are marked as tests, and webhook payloads carry test: true. |
| ssk_live_... | Production agreements. |
| oat_... | An OAuth access token acting for one member. |
All three use the same base URL. tokenEnvironment(token) returns 'test', 'live' or 'oauth', read from the prefix.
Errors
Every refusal throws MapleApiError with status, code, message and requestId. Quote the requestId when you contact support. Some refusals carry more in details. For example, test_recipient_not_allowed lists the recipients it refused and the domains you may use. A 429 sets retryAfterSeconds.
import { MapleApiError } from 'maple-esign';
try {
await maple.sendDocument(doc.id);
} catch (e) {
if (e instanceof MapleApiError && e.status === 429) {
// wait e.retryAfterSeconds, then try again
}
throw e;
}The status codes are 401 (not signed in), 403 (not allowed), 404 (not found, including another team's records), 409 (wrong state, such as downloading before sealing), 422 (validation), 429 (rate limited) and 503 (a feature this deployment has turned off).
Webhooks
createWebhook returns the endpoint's secret once, so store it. Each delivery is signed with that secret in the X-Stalwart-Signature header. Verify it against the raw body, exactly as it arrived:
import express from 'express';
import { verifyWebhookSignature, type WebhookPayload } from 'maple-esign';
const app = express();
app.post('/maple/webhooks', express.raw({ type: 'application/json' }), async (req, res) => {
const ok = await verifyWebhookSignature(
process.env.MAPLE_WEBHOOK_SECRET!,
req.get('x-stalwart-signature'),
req.body, // the raw bytes; a re-serialised object will not verify
);
if (!ok) return res.sendStatus(400);
const event = JSON.parse(req.body.toString('utf8')) as WebhookPayload;
res.sendStatus(200);
// A delivery is a nudge: re-read the document before acting on it.
if ('documentId' in event) await maple.getDocument(event.documentId);
});Signatures older than five minutes are refused, so a captured delivery cannot be replayed. Change that with { toleranceSeconds }.
Embedded signing
Create the document with delivery: 'embedded' on the recipient, send it, then mint a short lived signing link from your server for one of your registered embed origins:
const { url } = await maple.createEmbedSigningToken(doc.id, {
recipientId: doc.recipients[0]!.id,
origin: 'https://app.example.ca',
});Frame url in your page. isMapleSigningEvent and mapleOriginOf read the frame's postMessage events.
Paging
listDocuments answers one page, newest first: 50 by default and up to 100 with limit. Pass offset to read on until a page comes back short. It also filters by status, q, folder, tag and hideExpired.
Requirements
- Node.js 20 or later, or any runtime with
fetchand Web Crypto (crypto.subtle, used only to verify webhooks). - TypeScript 5.0 or later for the types.
Licence
Apache License 2.0. The full text is in the LICENSE file that comes with the package.
