API Documentation

Complete guide to use the Templatr API

Introduction

Templatr is a powerful API for generating PDFs from HTML templates. Our RESTful API allows you to upload templates, manage them, and generate personalized PDFs.

Base URL: https://api.templatr.com

Authentication

The Templatr API supports two authentication methods:

API Key (Recommended for applications)

Shell
GET /api/pdf/template-id?api_key=YOUR_API_KEY

Note: API keys can be created from your dashboard.

Available Endpoints

Upload a template

POST/upload
Shell
curl -X POST https://api.templatr.com/upload \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -F 'file=@template.html' \
  -F 'template_name=invoice_template'

Response:

{ "template_id": "abc123" }

Generate a PDF

POST/pdf/:template_id
Shell
curl -X POST https://api.templatr.com/pdf/abc123?api_key=YOUR_API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "client_name": "John Doe",
    "invoice_number": "INV-001",
    "amount": 1500.00,
    "items": [
      { "description": "Service A", "price": 1000 },
      { "description": "Service B", "price": 500 }
    ]
  }' \
  --output invoice.pdf

Response: Binary PDF file or PDF URL

Get a template

GET/upload/:template_id
Shell
curl https://api.templatr.com/upload/abc123?api_key=YOUR_API_KEY

Update a template

PUT/upload/:template_id
Shell
curl -X PUT https://api.templatr.com/upload/abc123?api_key=YOUR_API_KEY \
  -F 'file=@updated_template.html' \
  -F 'name=Updated Invoice Template'

Delete a template

DELETE/upload/:template_id
Shell
curl -X DELETE https://api.templatr.com/upload/abc123?api_key=YOUR_API_KEY

Usage Examples

JavaScript / Node.js
// Generate a PDF
const response = await fetch('https://api.templatr.com/pdf/abc123?api_key=YOUR_API_KEY', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    client_name: 'John Doe',
    invoice_number: 'INV-001',
    amount: 1500.00
  })
});

const pdfBlob = await response.blob();
// Save or display the PDF
Python
import requests

# Generate a PDF
data = {
    "client_name": "John Doe",
    "invoice_number": "INV-001",
    "amount": 1500.00
}

response = requests.post(
    "https://api.templatr.com/pdf/abc123",
    params={"api_key": "YOUR_API_KEY"},
    json=data
)

# Save the PDF
with open("invoice.pdf", "wb") as f:
    f.write(response.content)
Go
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
    "os"
)

func main() {
    // Data to send
    data := map[string]interface{}{
        "client_name":    "John Doe",
        "invoice_number": "INV-001",
        "amount":         1500.00,
    }

    jsonData, err := json.Marshal(data)
    if err != nil {
        panic(err)
    }

    // Create request
    req, err := http.NewRequest("POST", 
        "https://api.templatr.com/pdf/abc123?api_key=YOUR_API_KEY", 
        bytes.NewBuffer(jsonData))
    if err != nil {
        panic(err)
    }

    req.Header.Set("Content-Type", "application/json")

    // Send request
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    // Read response
    body, err := io.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }

    // Save PDF
    err = os.WriteFile("invoice.pdf", body, 0644)
    if err != nil {
        panic(err)
    }

    fmt.Println("PDF saved successfully")
}
cURL
curl -X POST https://api.templatr.com/pdf/abc123?api_key=YOUR_API_KEY \
  -H 'Content-Type: application/json' \
  -d '{
    "client_name": "John Doe",
    "invoice_number": "INV-001",
    "amount": 1500.00
  }' \
  --output invoice.pdf
PHP
<?php
$data = [
    'client_name' => 'John Doe',
    'invoice_number' => 'INV-001',
    'amount' => 1500.00
];

$ch = curl_init('https://api.templatr.com/pdf/abc123?api_key=YOUR_API_KEY');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json'
]);

$response = curl_exec($ch);
curl_close($ch);

// Save the PDF
file_put_contents('invoice.pdf', $response);
?>

Example HTML Template

<!DOCTYPE html>
<html>
<head>
    <style>
        body { font-family: Arial, sans-serif; }
        .invoice-header { margin-bottom: 20px; }
        .amount { font-size: 24px; font-weight: bold; }
    </style>
</head>
<body>
    <div class="invoice-header">
        <h1>Facture #{{invoice_number}}</h1>
        <p>Client: {{client_name}}</p>
    </div>
    <div class="amount">
        Total: {{amount}} €
    </div>
    {{#if items}}
    <ul>
        {{#each items}}
        <li>{{description}} - {{price}} €</li>
        {{/each}}
    </ul>
    {{/if}}
</body>
</html>

Templates use Handlebars syntax for variables and logic.

Error Handling

The API returns standard HTTP error codes:

200Success
400Bad request (missing or invalid parameters)
401Unauthorized (invalid or missing API key)
403Forbidden (quota exceeded)
404Resource not found
500Server error
{
  "error": "Invalid API key",
  "code": "INVALID_API_KEY"
}

Limits and Quotas

Default Limits

  • Maximum template size: 10 MB
  • Maximum JSON data size: 1 MB
  • PDF generation timeout: 30 seconds
  • Monthly quota: according to your plan

Quota alert: You will receive an email alert when you reach 80% of your monthly quota.

Available Plans

Consultez notre pricing page pour voir les différents plans et leurs quotas.