Guide complet pour utiliser l'API Templatr
Templatr est une API puissante pour générer des PDFs à partir de templates HTML. Notre API RESTful vous permet de télécharger des templates, de les gérer et de générer des PDFs personnalisés.
URL de base: https://api.templatr.com
L'API Templatr supporte deux méthodes d'authentification :
GET /api/pdf/template-id?api_key=YOUR_API_KEYNote: Les clés API peuvent être créées depuis votre tableau de bord.
/uploadcurl -X POST https://api.templatr.com/upload \
-H 'Authorization: Bearer YOUR_TOKEN' \
-F 'file=@template.html' \
-F 'template_name=invoice_template'Réponse:
{ "template_id": "abc123" }/pdf/:template_idcurl -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.pdfRéponse: Fichier PDF binaire ou URL du PDF
/upload/:template_idcurl https://api.templatr.com/upload/abc123?api_key=YOUR_API_KEY/upload/:template_idcurl -X PUT https://api.templatr.com/upload/abc123?api_key=YOUR_API_KEY \
-F 'file=@updated_template.html' \
-F 'name=Updated Invoice Template'/upload/:template_idcurl -X DELETE https://api.templatr.com/upload/abc123?api_key=YOUR_API_KEY// 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<!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>Les templates utilisent la syntaxe Handlebars pour les variables et la logique.
L'API retourne des codes d'erreur HTTP standards :
{
"error": "Invalid API key",
"code": "INVALID_API_KEY"
}Alerte quota : Vous recevrez un email d'alerte lorsque vous atteindrez 80% de votre quota mensuel.
Consultez notre page de tarification pour voir les différents plans et leurs quotas.