Skip to main content

Generate from Template

Generate a PDF by combining a stored template with a data object. All $data references and source bindings in the template are resolved with the provided data before the PDF is rendered.

POST /documents/generate-from-template

Endpoint

POST /api/v1/tenants/{tenantId}/documents/generate-from-template

Headers

HeaderValueRequired
AuthorizationBearer YOUR_API_KEYYes
Content-Typeapplication/jsonYes

Path parameters

ParameterTypeDescription
tenantIdstringYour tenant identifier

Request body

FieldTypeRequiredDescription
templateIdstring (GUID)YesID of the stored template
dataobjectYesData object with values to inject into the template
{
"templateId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"data": {
"client": {
"companyName": "Meridian Logistics",
"contactName": "Sarah Mitchell",
"address": "12 Harbor Road",
"city": "Austin, TX"
},
"document": {
"title": "Professional Services Invoice",
"date": "March 30, 2026"
},
"lineItems": [
{ "service": "Platform setup", "hours": "80", "rate": "$150", "total": "$12,000" },
{ "service": "API integration", "hours": "40", "rate": "$175", "total": "$7,000" }
],
"invoice": {
"grandTotal": "$19,000"
},
"paymentTerms": [
"Wire transfer to ShipForge Ltd account",
"Net 30 days from invoice date"
]
}
}

Response

Success (200 OK)

Returns the generated document metadata with a download URL.

{
"fileName": "doc-20260330-153045-xyz789.pdf",
"documentId": "d4e5f6a7-b8c9-0123-4567-89abcdef0123",
"status": "Completed",
"fileSizeBytes": 31744,
"contentType": "application/pdf",
"downloadUrl": "https://api.docpayload.com/v1/tenants/{tenantId}/assets/docs/doc-20260330-153045-xyz789.pdf"
}

Error (404 Not Found)

Returned when the template ID does not exist or does not belong to your tenant.

{
"message": "Template 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' not found."
}

Error (400 Bad Request)

Returned when the template does not contain a valid document definition.

{
"message": "The template does not contain a valid document definition."
}

Example

curl -X POST https://api.docpayload.com/v1/tenants/{tenantId}/documents/generate-from-template \
-H "Authorization: Bearer dp_live_abc123def456" \
-H "Content-Type: application/json" \
-d '{
"templateId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"data": {
"client": {
"companyName": "Meridian Logistics"
},
"document": {
"title": "Delivery Receipt"
}
}
}'

Data resolution

When this endpoint is called, the following resolution steps occur:

  1. The template is retrieved from the database by templateId.
  2. The template's content (a JSON string) is parsed into a document definition.
  3. All $data.path.to.value scalar references in paragraph text are replaced with values from the data object.
  4. All $data references in form field default values (textfield, checkbox, textarea) are resolved.
  5. All source bindings on tables and lists are resolved:
    • Table source arrays are mapped to rows using map.
    • List source arrays are converted to list items.
  6. The resolved document definition is rendered to PDF.

What is resolved

FeatureExampleDescription
Scalar text$data.client.nameReplaced with the value at the given path
Nested paths$data.client.address.cityDot-separated traversal of nested objects
Inside formatting[b]$data.total[/b]Ref resolved, then formatting applied
Form defaults'$data.applicant.email'Pre-fills editable form fields
Table rows"source": "$data.items"Array items become table rows
List items"source": "$data.notes"Array items become list items
Unresolved refs$data.missing.keyPreserved as literal text

Downloading the PDF

Use the downloadUrl from the response to fetch the generated PDF binary:

curl -X GET "https://api.docpayload.com/v1/tenants/{tenantId}/assets/docs/doc-20260330-153045-xyz789.pdf" \
-H "Authorization: Bearer dp_live_abc123def456" \
-o invoice.pdf