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",
"email": boolean | {
"subject": string,
"body": string
}
}
}
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.email | boolean | object | Optional. Whether to send the document as an attachment via email. If true, the document will be sent to your email address. If an object is provided, the email will be sent with the subject and body specified in the object. |
Each document generation consumes one quota unit.
Response
A successful request returns the generated document with the appropriate Content-Type
header:
application/vnd.openxmlformats-officedocument.wordprocessingml.document
for DOCXapplication/pdf
for PDF
The response is a binary file stream, not a JSON response. Make sure to handle the response accordingly in your code.
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: {
firstName: "John",
lastName: "Doe",
company: "ACME Inc."
},
output: {
format: "pdf"
}
})
});
const blob = await response.blob();