[Go to site: main page, start]

Skip to content

Python SDK & REST Examples — TemplateFox

Two ways to use the TemplateFox API from Python:

  • SDK (templatefox) — typed client auto-generated from our OpenAPI spec. Recommended.
  • RESTrequests or httpx. Useful for minimal dependencies or endpoints not yet in the SDK.

Every example on this page shows both side-by-side.

PyPI version GitHub Repository

Terminal window
pip install templatefox
# or
poetry add templatefox
# or
uv add templatefox

For the REST variant, just pip install requests.

Set your API key once and reuse the client.

import os
from templatefox import ApiClient, Configuration
from templatefox.api import PDFApi

config = Configuration()
config.api_key["ApiKeyAuth"] = os.environ["TEMPLATEFOX_API_KEY"]

client = ApiClient(config)
api = PDFApi(client)

The minimum required fields are template_id and data. Everything else has a sensible default.

from templatefox.models import CreatePdfRequest

response = api.create_pdf(
  CreatePdfRequest(
      template_id="YOUR_TEMPLATE_ID",
      data={
          "customer_name": "John Doe",
          "invoice_number": "INV-001",
          "amount": "$1,234.56",
      },
  )
)

print(response.url)                # signed URL valid 24h
print(response.credits_remaining)  # credits left on the team

See Create PDF for every supported parameter.

import urllib.request
from templatefox.models import CreatePdfRequest

response = api.create_pdf(
  CreatePdfRequest(
      template_id="YOUR_TEMPLATE_ID",
      data={"customer_name": "Jane Doe"},
  )
)

urllib.request.urlretrieve(response.url, "invoice.pdf")

With the SDK you fetch the signed URL then save. With REST you can also ask for export_type="binary" to stream the bytes directly.

Configure your S3 integration once in the dashboard, then pass store_s3=True on any call.

from templatefox.models import CreatePdfRequest

response = api.create_pdf(
  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
  )
)

print(response.s3_bucket, response.s3_key)

For archival/legal use-cases, request a PDF/A variant.

from templatefox.models import CreatePdfRequest

response = api.create_pdf(
  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"
  )
)

For large documents or batch jobs, use the async endpoint and receive a webhook when the PDF is ready. See Create PDF (Async) for the full flow.

response = requests.post(
  f"{BASE_URL}/v1/pdf/create-async",
  headers=HEADERS,
  json={
      "template_id": "YOUR_TEMPLATE_ID",
      "data": {"invoice_number": "INV-001"},
      "webhook_url": "https://example.com/webhooks/pdf",
      "webhook_secret": "whk_a1b2c3d4e5f6g7h8i9j0k1l2",  # HMAC-SHA256 signing
  },
).json()

job_id = response["job_id"]
# Poll GET /v1/pdf/jobs/{job_id} or wait for the webhook.

Async endpoints (create-pdf-async, jobs) are not yet exposed in the SDK — use requests directly until then.

from templatefox.exceptions import ApiException
from templatefox.models import CreatePdfRequest

try:
  response = api.create_pdf(
      CreatePdfRequest(template_id="INVALID_ID", data={})
  )
except ApiException as e:
  print(e.status, e.body)

See Error codes for the full list.

ClassMethodEndpoint
PDFApicreate_pdfPOST /v1/pdf/create
TemplatesApilist_templatesGET /v1/templates
TemplatesApiget_template_fieldsGET /v1/templates/{id}/fields
AccountApiget_accountGET /v1/account
AccountApilist_transactionsGET /v1/account/transactions
IntegrationsApisave_s3_configPOST /v1/integrations/s3
IntegrationsApiget_s3_configGET /v1/integrations/s3
IntegrationsApidelete_s3_configDELETE /v1/integrations/s3
IntegrationsApitest_s3_connectionPOST /v1/integrations/s3/test

All REST endpoints are documented in the API Reference.