Skip to content

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.

Packagist Version GitHub Repository

Terminal window
composer require templatefox/sdk

The REST variant needs no dependency — cURL is built into PHP.

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);

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.

<?php

$response = $api->createPdf($request);
file_put_contents('invoice.pdf', file_get_contents($response->getUrl()));

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();

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);

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.
<?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.

ClassMethodEndpoint
PDFApicreatePdfPOST /v1/pdf/create
PDFApicreatePdfAsyncPOST /v1/pdf/create-async
PDFApigetPdfJobGET /v1/pdf/jobs/{id}
PDFApilistPdfJobsGET /v1/pdf/jobs
TemplatesApilistTemplatesGET /v1/templates
TemplatesApigetTemplateFieldsGET /v1/templates/{id}/fields
AccountApigetAccountGET /v1/account
AccountApilistTransactionsGET /v1/account/transactions
IntegrationsApisaveS3ConfigPOST /v1/integrations/s3
IntegrationsApigetS3ConfigGET /v1/integrations/s3
IntegrationsApideleteS3ConfigDELETE /v1/integrations/s3
IntegrationsApitestS3ConnectionPOST /v1/integrations/s3/test

All REST endpoints are documented in the API Reference.