Generate PDF
Call the API to generate PDFs from your templates with dynamic data.
Overview
The Generate API is the core of Document Stack. Send a template ID and your data, get back a PDF. It's a single endpoint you call from your server whenever you need to create a document.
Endpoint
text
POST https://api.documentstack.dev/api/v1/generateRequest Format
| Header | Value |
|---|---|
Authorization | Bearer YOUR_API_KEY |
Content-Type | application/json |
JSON
{
"templateId": "tmpl_abc123",
"data": {
"customer_name": "Acme Corporation",
"invoice_number": "INV-2026-042",
"due_date": "March 1, 2026",
"line_items": [
{ "description": "Design Services", "amount": "$2,000" },
{ "description": "Development", "amount": "$6,000" }
],
"total": "$8,000.00"
}
}| Field | Type | Required | Description |
|---|---|---|---|
templateId | string | Yes | The ID of the template to generate from (find it in the dashboard) |
data | object | No | Key-value pairs that fill in your template's data-bound fields |
You can find your template ID in the template settings or in the dashboard URL when viewing a template.
Response
On success, the API returns the generated PDF as a binary file:
text
HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Disposition: attachment; filename="document.pdf"
<binary PDF content>Error Handling
Errors return a JSON body with a descriptive message:
JSON
{
"success": false,
"error": "Template not found"
}| Status | Meaning | What To Do |
|---|---|---|
400 | Invalid request | Check your request body format and required fields |
401 | Unauthorized | Check your API key is correct and active |
404 | Template not found | Verify the template ID and that the key has access to its project |
429 | Rate limited | Slow down your requests or upgrade your plan |
500 | Server error | Contact support if this persists |
Full Examples
cURL
Bash
curl -X POST https://api.documentstack.dev/api/v1/generate \
-H "Authorization: Bearer ds_live_abc123" \
-H "Content-Type: application/json" \
-d '{
"templateId": "tmpl_xyz",
"data": {
"customer_name": "John Doe",
"amount": "$1,250.00"
}
}' \
--output invoice.pdfJavaScript (fetch)
JavaScript
const response = await fetch("https://api.documentstack.dev/api/v1/generate", {
method: "POST",
headers: {
"Authorization": "Bearer " + process.env.DOCUMENTSTACK_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
templateId: "tmpl_xyz",
data: { customer_name: "John Doe", amount: "$1,250.00" },
}),
});
// Save the PDF
const pdfBuffer = await response.arrayBuffer();
fs.writeFileSync("invoice.pdf", Buffer.from(pdfBuffer));Python (requests)
Python
import requests
response = requests.post(
"https://api.documentstack.dev/api/v1/generate",
headers={"Authorization": "Bearer " + API_KEY},
json={
"templateId": "tmpl_xyz",
"data": {"customer_name": "John Doe", "amount": "$1,250.00"},
},
)
with open("invoice.pdf", "wb") as f:
f.write(response.content)