X Enterprises
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

NameTypeRequiredDescription
templateIdstringYesID of the SignWell template to use.
params.recipientsarrayYesNon-empty array. Each recipient's id must match a placeholder ID in the template.
params.recipients[].idstringYesMatches a recipient placeholder in the template.
params.recipients[].emailstringYesRecipient email address.
params.recipients[].namestringNoRecipient display name.
params.recipients[].sendEmailbooleanNoSend signing email. Default true.
params.fieldsarrayNoPre-fill field values for the template.
params.draftbooleanNoCreate without sending. Default false.
params.subjectstringNoEmail subject override.
params.messagestringNoEmail message override.
params.metadataobjectNoCustom metadata stored with the document.

Returns

The created document object from the SignWell API.

Throws

  • [xSignwell] documents.createFromTemplate: templateId (string) is required — if templateId is missing.
  • [xSignwell] documents.createFromTemplate: params object is required — if params is not an object.
  • [xSignwell] documents.createFromTemplate: recipients (non-empty array) is required — if recipients is empty.
  • Re-throws SignWell API errors with statusCode and signwellError properties.

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

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
Copyright © 2026