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
| Header | Value | Required |
|---|---|---|
Authorization | Bearer YOUR_API_KEY | Yes |
Content-Type | application/json | Yes |
Path parameters
| Parameter | Type | Description |
|---|---|---|
tenantId | string | Your tenant identifier |
Request body
| Field | Type | Required | Description |
|---|---|---|---|
templateId | string (GUID) | Yes | ID of the stored template |
data | object | Yes | Data 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:
- The template is retrieved from the database by
templateId. - The template's
content(a JSON string) is parsed into a document definition. - All
$data.path.to.valuescalar references in paragraph text are replaced with values from thedataobject. - All
$datareferences in form field default values (textfield, checkbox, textarea) are resolved. - All
sourcebindings on tables and lists are resolved:- Table
sourcearrays are mapped to rows usingmap. - List
sourcearrays are converted to list items.
- Table
- The resolved document definition is rendered to PDF.
What is resolved
| Feature | Example | Description |
|---|---|---|
| Scalar text | $data.client.name | Replaced with the value at the given path |
| Nested paths | $data.client.address.city | Dot-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.key | Preserved 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