POST/api/v1/pdf/from-template

Template to PDF

Generate PDFs from stored templates with dynamic variable substitution. Templates use Mustache/Handlebars syntax for placeholders, loops, and conditionals. Create invoices, receipts, contracts, and reports without writing HTML each time.

Parameters

NameTypeRequiredDefaultDescription
template_slugstringYes-Slug identifier for the stored template (e.g., invoice-standard, receipt-minimal).
variablesobjectYes-Key-value pairs to substitute into template placeholders. Supports nested objects and arrays.

Template Syntax

Templates are stored on your account and use Mustache/Handlebars syntax. Use {{variable}} for simple substitution and {{#array}}...{{/array}} for loops over arrays.

Template Example (HTML)
<!-- Template placeholders use Mustache/Handlebars syntax -->
<h1>Invoice {{invoice_number}}</h1>
<p>Bill to: {{client_name}}</p>
<p>Address: {{client_address}}</p>

<table>
  <tr><th>Item</th><th>Qty</th><th>Price</th></tr>
  {{#items}}
  <tr>
    <td>{{description}}</td>
    <td>{{quantity}}</td>
    <td>${{price}}</td>
  </tr>
  {{/items}}
</table>

<p><strong>Total: ${{total}}</strong></p>
<p>Due: {{due_date}}</p>

Code Examples

cURL
curl -X POST "https://app.alternapdf.com/api/v1/pdf/from-template" \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "template_slug": "invoice-standard",
    "variables": {
      "company_name": "Acme Corp",
      "invoice_number": "INV-2026-001",
      "items": [
        {"description": "Consulting", "quantity": 10, "price": 150},
        {"description": "Development", "quantity": 40, "price": 120}
      ],
      "total": 6300,
      "due_date": "2026-04-15",
      "client_name": "Widget Inc",
      "client_address": "123 Main St, Springfield"
    }
  }' \
  --output invoice.pdf
Python
import requests

response = requests.post(
    "https://app.alternapdf.com/api/v1/pdf/from-template",
    headers={"X-API-Key": "YOUR_API_KEY"},
    json={
        "template_slug": "invoice-standard",
        "variables": {
            "company_name": "Acme Corp",
            "invoice_number": "INV-2026-001",
            "items": [
                {"description": "Consulting", "quantity": 10, "price": 150},
                {"description": "Development", "quantity": 40, "price": 120}
            ],
            "total": 6300,
            "due_date": "2026-04-15",
            "client_name": "Widget Inc",
            "client_address": "123 Main St, Springfield"
        }
    }
)

with open("invoice.pdf", "wb") as f:
    f.write(response.content)
JavaScript
const response = await fetch("https://app.alternapdf.com/api/v1/pdf/from-template", {
  method: "POST",
  headers: {
    "X-API-Key": "YOUR_API_KEY",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    template_slug: "invoice-standard",
    variables: {
      company_name: "Acme Corp",
      invoice_number: "INV-2026-001",
      items: [
        { description: "Consulting", quantity: 10, price: 150 },
        { description: "Development", quantity: 40, price: 120 },
      ],
      total: 6300,
      due_date: "2026-04-15",
      client_name: "Widget Inc",
      client_address: "123 Main St, Springfield",
    },
  }),
});

fs.writeFileSync("invoice.pdf", Buffer.from(await response.arrayBuffer()));

Response

Returns the generated PDF as a binary download with Content-Type: application/pdf.

HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Disposition: attachment; filename="generated.pdf"
X-Credits-Used: 1
X-Credits-Remaining: 4999

<binary PDF data>