Generate Document
The generate document endpoint allows you to create a document by filling a template with your data.
Request
POST /v1/templates/{template_id}/generate
Path Parameters
| Parameter | Type | Description |
|---|---|---|
template_id | string | The unique identifier of the template to use |
Request Body
{
"data": object,
"output": {
"format": "docx" | "pdf",
"filename": string,
"email": EmailOptions
}
}
Request Fields
| Field | Type | Description |
|---|---|---|
data | object | Key-value pairs where keys match template placeholders |
output | object | Optional. Output options |
output.format | string | Optional. The desired output format: "docx" (default) or "pdf" |
output.filename | string | Optional. The desired output filename. If not provided, the filename will be generated based on the template name. |
output.email | EmailOptions | Optional. See details below. |
Each document generation consumes 10 credits.
Email Options
To send the document via email, use the following object structure:
{
"output": {
"email": {
"to": string[],
"cc": string[],
"bcc": string[],
"subject": string,
"body": string
}
}
}
Email Options Fields
| Field | Type | Description |
|---|---|---|
to | string[] | Primary recipients |
cc | string[] | Optional. CC recipients |
bcc | string[] | Optional. BCC recipients |
subject | string | Optional. Email subject line to replace the default one |
body | string | Optional. Text or HTML email body content to replace the default one |
Read more about our email delivery options in our email delivery documentation.
Response
A successful request returns the generated document with the appropriate Content-Type header:
application/vnd.openxmlformats-officedocument.wordprocessingml.documentfor DOCXapplication/pdffor PDF
The response is a binary file stream, not a JSON response. Make sure to handle the response accordingly in your code.
Document Storage
Generated documents are saved as Quick Documents in your organization's History, and automatically deleted based on your data retention settings. To disable storage, set Quick Document retention to 0 days.
Examples
const apiKey = "Your_API_Key";
const templateId = "template_123";
const response = await fetch(`https://templatedocs.io/api/v1/templates/${templateId}/generate`, {
method: 'POST',
headers: {
"Authorization": `Bearer ${apiKey}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
data: {
CustomerName: "John Doe",
Items: [
{
Name: "Gadget",
Price: 100
},
{
Name: "Gizmo",
Price: 200
}
]
},
output: {
format: "pdf",
email: {
to: [
"recipient1@example.com",
"recipient2@example.com"
],
subject: "Your document is ready"
}
}
})
});
const blob = await response.blob();Next Steps
- Set up your API key to start generating documents
- Explore different template features:
- Check out example templates for inspiration