Document Stack
Document Stack
Docs

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/generate

Request Format

HeaderValue
AuthorizationBearer YOUR_API_KEY
Content-Typeapplication/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"
  }
}
FieldTypeRequiredDescription
templateIdstringYesThe ID of the template to generate from (find it in the dashboard)
dataobjectNoKey-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"
}
StatusMeaningWhat To Do
400Invalid requestCheck your request body format and required fields
401UnauthorizedCheck your API key is correct and active
404Template not foundVerify the template ID and that the key has access to its project
429Rate limitedSlow down your requests or upgrade your plan
500Server errorContact 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.pdf

JavaScript (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)
For production use with retry handling and type safety, use our official Node.js, Python, or Go SDK instead of raw HTTP calls.