PHP SDK & REST Examples — TemplateFox
Two ways to use the TemplateFox API from PHP:
- SDK (
templatefox/sdk) — typed client auto-generated from our OpenAPI spec. Recommended. - REST — raw cURL or Guzzle. Useful for minimal dependencies.
Every example on this page shows both side-by-side.
Installation
Section titled “Installation”composer require templatefox/sdkThe REST variant needs no dependency — cURL is built into PHP.
Configuration
Section titled “Configuration”Set your API key once and reuse the client.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use TemplateFox\Api\PDFApi;
use TemplateFox\Configuration;
$config = Configuration::getDefaultConfiguration()
->setApiKey('x-api-key', getenv('TEMPLATEFOX_API_KEY'));
$api = new PDFApi(null, $config); Generate a PDF
Section titled “Generate a PDF”The minimum required fields are template_id and data. Everything else has a sensible default.
<?php
use TemplateFox\Model\CreatePdfRequest;
$request = new CreatePdfRequest([
'template_id' => 'YOUR_TEMPLATE_ID',
'data' => [
'customer_name' => 'John Doe',
'invoice_number' => 'INV-001',
'amount' => '$1,234.56',
],
]);
$response = $api->createPdf($request);
echo $response->getUrl() . "\n"; // signed URL valid 24h
echo $response->getCreditsRemaining() . "\n"; // credits left on the team See Create PDF for every supported parameter.
Save the PDF to a file
Section titled “Save the PDF to a file”<?php
$response = $api->createPdf($request);
file_put_contents('invoice.pdf', file_get_contents($response->getUrl())); Upload straight to your S3 bucket
Section titled “Upload straight to your S3 bucket”Configure your S3 integration once in the dashboard, then pass store_s3 = true on any call.
<?php
use TemplateFox\Model\CreatePdfRequest;
$request = new CreatePdfRequest([
'template_id' => 'YOUR_TEMPLATE_ID',
'data' => ['invoice_number' => 'INV-001'],
'filename' => 'invoice-001', // no extension, .pdf appended
'store_s3' => true, // upload to your configured bucket
's3_filepath' => 'invoices/2026/04/', // key prefix (optional)
// 's3_bucket' => 'custom-bucket', // override bucket from integration
]);
$response = $api->createPdf($request);
echo $response->getS3Bucket() . '/' . $response->getS3Key(); PDF/A compliance
Section titled “PDF/A compliance”For archival/legal use-cases, request a PDF/A variant.
<?php
use TemplateFox\Model\CreatePdfRequest;
$request = new CreatePdfRequest([
'template_id' => 'YOUR_TEMPLATE_ID',
'data' => ['customer_name' => 'John Doe'],
'pdf_variant' => 'pdf/a-2b', // "pdf/a-1b" | "pdf/a-2b" | "pdf/a-3b"
]);
$response = $api->createPdf($request); Async generation with webhooks
Section titled “Async generation with webhooks”For large documents or batch jobs, use the async endpoint. The PHP SDK exposes createPdfAsync, getPdfJob, and listPdfJobs — see Create PDF (Async) for the full flow.
<?php
use TemplateFox\Model\CreatePdfAsyncRequest;
$request = new CreatePdfAsyncRequest([
'template_id' => 'YOUR_TEMPLATE_ID',
'data' => ['invoice_number' => 'INV-001'],
'webhook_url' => 'https://example.com/webhooks/pdf',
'webhook_secret' => 'whk_a1b2c3d4e5f6g7h8i9j0k1l2', // HMAC-SHA256 signing
]);
$response = $api->createPdfAsync($request);
echo $response->getJobId();
// Poll $api->getPdfJob($jobId) or wait for the webhook. Error handling
Section titled “Error handling”<?php
use TemplateFox\ApiException;
try {
$response = $api->createPdf($request);
} catch (ApiException $e) {
echo 'status=' . $e->getCode() . "\n";
echo 'body=' . $e->getResponseBody() . "\n";
} See Error codes for the full list.
Available SDK methods
Section titled “Available SDK methods”| Class | Method | Endpoint |
|---|---|---|
PDFApi | createPdf | POST /v1/pdf/create |
PDFApi | createPdfAsync | POST /v1/pdf/create-async |
PDFApi | getPdfJob | GET /v1/pdf/jobs/{id} |
PDFApi | listPdfJobs | GET /v1/pdf/jobs |
TemplatesApi | listTemplates | GET /v1/templates |
TemplatesApi | getTemplateFields | GET /v1/templates/{id}/fields |
AccountApi | getAccount | GET /v1/account |
AccountApi | listTransactions | GET /v1/account/transactions |
IntegrationsApi | saveS3Config | POST /v1/integrations/s3 |
IntegrationsApi | getS3Config | GET /v1/integrations/s3 |
IntegrationsApi | deleteS3Config | DELETE /v1/integrations/s3 |
IntegrationsApi | testS3Connection | POST /v1/integrations/s3/test |
All REST endpoints are documented in the API Reference.
Next steps
Section titled “Next steps”- Create PDF — every parameter in detail
- Create PDF Async — webhooks + polling
- GitHub repository — source, changelog, issues
- Postman collection — test endpoints interactively