Skip to main content

Templates and Forms

Learn how to create documents from templates and dynamically fill form fields using the sajn API.

Overview

Templates in sajn allow you to create reusable document structures with pre-configured form fields. This guide covers:
  1. Creating documents from templates - Use existing templates as document foundation
  2. Filling form fields - Update form field values using field keys
  3. Sending templated documents - Complete the workflow by sending for signatures

Template-Based Document Creation

Step 1: Create Document from Template

Create a new document using an existing template as the foundation:
Response:

Key Parameters

  • name - Document title (will override template name)
  • templateId - ID of the template to use as foundation
  • type - Document type (usually SIGNABLE for templates)
  • externalId - Optional external reference ID
  • expiresAt - Optional expiration date

Filling Form Fields

Understanding Field Keys

Templates contain form fields with unique keys that identify each input field. These keys allow you to programmatically update field values. Common field key examples:
  • name - Person’s full name
  • phone - Phone number
  • email - Email address
  • address - Street address
  • startDate - Employment start date

Step 2: Update Form Fields by Key

Use field keys to update specific form field values:

Update Name Field

Update Phone Field

Update Email Field

Field Types and Values

Different field types accept different value formats:

Text Input Fields

Number Fields

Date Fields

Checkbox Fields

Select/Dropdown Fields

Complete Workflow Example

Here’s a complete example of creating a document from template, filling fields, and sending for signing:

JavaScript Implementation

Step 3: Send for Signing

After filling all form fields, send the document for signatures:
Delivery method and signature type are configured per-signer when adding them, not at send time.

Send Options

  • customMessage - Optional custom message to signers. Supports \n for line breaks and template variables (e.g. {{firstName}}, {{documentName}}, {{signUrl}}). See Sending Documents for Signing for the full list.

Advanced Field Operations

Bulk Field Updates

Update multiple FORM fields in a single atomic request with the batch endpoint PATCH /documents/{documentId}/fields:
Don’t update fields by firing parallel single-key PATCH .../fields/key:<key> requests (e.g. Promise.all). All FORM subfields share one underlying record, so concurrent single-key writes read-modify-write the same data and clobber each other — only the last one survives. The batch endpoint applies every change against a single read, so nothing is lost.

Conditional Field Updates

Update fields based on conditions:

Template Field Discovery

Get Template Fields

Discover what fields are available in a template:
The response includes field information with keys:

Extract Field Keys

Error Handling

Common Errors

Field Key Not Found

Solution: Verify the field key exists in the template

Invalid Field Value

Solution: Check value format matches field type (date, number, etc.)

Template Not Found

Solution: Verify template ID is correct and accessible

Robust Error Handling

Best Practices

1. Field Validation

2. Many Fields (more than 50)

The batch endpoint accepts up to 50 updates per request. If you have more than that, send the chunks sequentiallyawait each request before the next. Don’t run batch requests for the same document in parallel: they each read-modify-write the same FORM record and would clobber each other, just like parallel single-key calls.

3. Handling Partial Failures

The batch response reports each key individually, so you can surface the ones that failed (e.g. an unknown key) while the valid updates still apply. success is true only when every update in the request succeeded.

Integration Examples

React Form Integration

Node.js Server Integration

Next Steps