fastify-x-signwell
documents.createFromTemplate
Create a SignWell document from a saved template with pre-placed signature fields.
documents.createFromTemplate
Create a new document by instantiating a saved SignWell template. The template's field placements are preserved; you supply the recipients and any field pre-fill values.
Signature
fastify.xsignwell.documents.createFromTemplate(
templateId: string,
params: {
recipients: Array<{ id: string; email: string; name?: string; sendEmail?: boolean }>;
fields?: Array<Record<string, unknown>>;
draft?: boolean;
subject?: string;
message?: string;
metadata?: Record<string, unknown>;
testMode?: boolean;
}
): Promise<Object>
Params
| Name | Type | Required | Description |
|---|---|---|---|
templateId | string | Yes | ID of the SignWell template to use. |
params.recipients | array | Yes | Non-empty array. Each recipient's id must match a placeholder ID in the template. |
params.recipients[].id | string | Yes | Matches a recipient placeholder in the template. |
params.recipients[].email | string | Yes | Recipient email address. |
params.recipients[].name | string | No | Recipient display name. |
params.recipients[].sendEmail | boolean | No | Send signing email. Default true. |
params.fields | array | No | Pre-fill field values for the template. |
params.draft | boolean | No | Create without sending. Default false. |
params.subject | string | No | Email subject override. |
params.message | string | No | Email message override. |
params.metadata | object | No | Custom metadata stored with the document. |
Returns
The created document object from the SignWell API.
Throws
[xSignwell] documents.createFromTemplate: templateId (string) is required— iftemplateIdis missing.[xSignwell] documents.createFromTemplate: params object is required— ifparamsis not an object.[xSignwell] documents.createFromTemplate: recipients (non-empty array) is required— if recipients is empty.- Re-throws SignWell API errors with
statusCodeandsignwellErrorproperties.
Examples
Basic — create from template
// Assumes template has one recipient placeholder with id "signer_1"
const doc = await fastify.xsignwell.documents.createFromTemplate(
"tmpl_abc123",
{
recipients: [
{ id: "signer_1", email: "alice@example.com", name: "Alice" },
],
}
);
console.log(doc.id); // "doc_xyz789"
Pre-fill template fields
const doc = await fastify.xsignwell.documents.createFromTemplate(
"tmpl_nda",
{
recipients: [
{ id: "party_a", email: "vendor@example.com", name: "Vendor Corp" },
],
fields: [
{ id: "company_name", value: "Vendor Corp" },
{ id: "effective_date", value: "2026-01-01" },
],
subject: "Your NDA is ready to sign",
}
);
Realistic — automate contract generation
fastify.post("/onboarding/contract", async (request, reply) => {
const { userId, email, fullName } = request.user;
const { planId } = request.body;
const template = await db.contractTemplates.findByPlan(planId);
const doc = await fastify.xsignwell.documents.createFromTemplate(
template.signwellTemplateId,
{
recipients: [{ id: "customer", email, name: fullName }],
fields: [
{ id: "customer_name", value: fullName },
{ id: "plan_name", value: template.planName },
{ id: "start_date", value: new Date().toISOString().slice(0, 10) },
],
metadata: { userId, planId },
}
);
await db.contracts.insert({ userId, documentId: doc.id, status: "sent" });
return reply.code(201).send({ documentId: doc.id });
});
See also
- templates.get / templates.list — find template IDs and inspect placeholder structure
- documents.create — create a document from scratch without a template
- documents.get — fetch a document by ID after creation
AI Context
package: "@xenterprises/fastify-xsignwell"
method: fastify.xsignwell.documents.createFromTemplate(params)
use-when: Instantiate a SignWell document from a saved template with pre-placed signature fields
params: templateId (required), recipients (array with role matching), subject, message, testMode
returns: document object with id, status, recipients
