Skip to main content

Python SDK

Use the official Pritset Python SDK to manage DOCX templates and generate PDFs from your Python application.

View the official Python SDK repository on GitHub

Requirementsโ€‹

  • Python 3.9 or newer
  • A Pritset access token and secret from the Profile page

The SDK is currently a 0.1.5 preview, so public method names may be refined before 1.0.0.

Compatibilityโ€‹

This page documents SDK version 0.1.5, which implements Pritset SDK contract 1.0.0.

Install the packageโ€‹

Install the package with pip:

python -m pip install pritset

The PyPI project name must be verified before the first public release. For development from a source checkout, install the package in editable mode:

python -m pip install -e .

Create a clientโ€‹

Store credentials in environment variables rather than source code. Pritset sends the access token directly in the Authorization header and the secret in X-Secret; do not add a Bearer prefix or expose either value to browser code or logs.

Clients own their default HTTP connection pool, so use a context manager or call close() when finished.

import os

from pritset import PritsetClient

with PritsetClient(
access_token=os.environ["PRITSET_ACCESS_TOKEN"],
secret=os.environ["PRITSET_SECRET"],
) as client:
page = client.templates.list()

Generate a PDFโ€‹

Upload a DOCX template first and copy its ID. See Template management for upload requirements and the portal workflow.

with PritsetClient(
access_token=os.environ["PRITSET_ACCESS_TOKEN"],
secret=os.environ["PRITSET_SECRET"],
) as client:
pdf = client.documents.generate(
"YOUR_TEMPLATE_ID",
{
"invoice": {"number": "INV-1042", "customer": "Ada Lovelace"},
},
)
print(pdf.content_type) # application/pdf
pdf.save_to_file("invoice.pdf")

Binary responses are stream-first. save_to_file() and to_bytes() consume and close the response, so call only one of them. Use iter_bytes() for custom streaming, then close the response yourself or use it as a context manager.

The data object must match the placeholders and loops in your DOCX file; see Templating syntax.

Manage templatesโ€‹

Use client.templates to list, retrieve, create, update, download, validate, and delete templates.

from pritset import PritsetClient, TemplateSort, Upload

with PritsetClient(
access_token=os.environ["PRITSET_ACCESS_TOKEN"],
secret=os.environ["PRITSET_SECRET"],
) as client:
page = client.templates.list(
search="invoice",
page=1,
page_size=25,
sort=TemplateSort(sort_by="Name", sort_direction=0), # 0 ascending, 1 descending
)
for template in page.data:
print(template.id, template.name)

created = client.templates.create(
name="Monthly invoice",
tags="invoice,monthly",
template=Upload("./invoice.docx"),
)

Paths can be used directly for uploads. Bytes and file objects require an explicit filename. Keep every related operation inside the active client context:

from pritset import PritsetClient, Upload

with PritsetClient(
access_token=os.environ["PRITSET_ACCESS_TOKEN"],
secret=os.environ["PRITSET_SECRET"],
) as client:
created = client.templates.create(
name="Monthly invoice",
template=Upload(
docx_bytes,
filename="invoice.docx",
content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
),
)
details = client.templates.get(created.id)
print(details.file_info.object_name, details.file_info.size)

client.templates.update(
created.id,
name="Monthly invoice 2026",
tags="invoice,monthly,2026",
template=Upload("./invoice-2026.docx"), # optional replacement file
)

download = client.templates.download(created.id)
download.save_to_file("downloaded-invoice.docx")
valid = client.templates.validate(
file=Upload("./invoice.docx"),
data={"invoice": {"number": "INV-1042"}},
)
print("Template valid:", valid)
client.templates.delete(created.id)

For endpoint behavior and limits, see the Template management API reference.

Raw JSONโ€‹

Pass a JSON string when exact serialization matters.

with PritsetClient(
access_token=os.environ["PRITSET_ACCESS_TOKEN"],
secret=os.environ["PRITSET_SECRET"],
) as client:
pdf = client.documents.generate("YOUR_TEMPLATE_ID", '{"amount":"10.00"}')
pdf.save_to_file("invoice.pdf")

Generate through a webhookโ€‹

Use generate_webhook() when Pritset should send the generation result to your endpoint. The webhook URL must be an absolute HTTP(S) URL without embedded credentials.

with PritsetClient(
access_token=os.environ["PRITSET_ACCESS_TOKEN"],
secret=os.environ["PRITSET_SECRET"],
) as client:
job = client.documents.generate_webhook(
"YOUR_TEMPLATE_ID",
{"invoice": {"number": "INV-1042"}},
"https://example.com/webhooks/pritset",
)
print(job.id)

Pritset posts the generated PDF to the supplied URL and includes the job ID as an id query parameter. The SDK does not run or verify your webhook receiver. See Direct and webhook processing for the API workflow.

Async usage and cancellationโ€‹

AsyncPritsetClient mirrors the synchronous API. Cancel the surrounding asyncio task to cancel an in-flight request.

import asyncio
import os

from pritset import AsyncPritsetClient


async def main() -> None:
async with AsyncPritsetClient(
access_token=os.environ["PRITSET_ACCESS_TOKEN"],
secret=os.environ["PRITSET_SECRET"],
) as client:
pdf = await client.documents.generate(
"YOUR_TEMPLATE_ID", {"title": "Async report"}
)
await pdf.save_to_file("async-report.pdf")


asyncio.run(main())

Handle errorsโ€‹

The SDK raises PritsetApiError for API responses with an error status and PritsetTransportError when a request does not complete. It does not retry automatically.

from pritset import PritsetApiError, PritsetClient, PritsetTransportError

with PritsetClient(
access_token=os.environ["PRITSET_ACCESS_TOKEN"],
secret=os.environ["PRITSET_SECRET"],
) as client:
try:
client.templates.get("YOUR_TEMPLATE_ID")
except PritsetApiError as error:
print(error.status, error.field_errors, error.trace, error.retry_after)
except PritsetTransportError:
print("The request did not complete.")

See API errors for status codes and retry guidance.

Local development and custom transportsโ€‹

The SDK uses https://api.pritset.com by default and disables redirects to prevent credentials from being forwarded to another origin. An injected httpx.Client remains caller-owned and is not closed by the SDK.

import httpx

with httpx.Client(verify=True) as http_client:
with PritsetClient(
access_token=os.environ["PRITSET_ACCESS_TOKEN"],
secret=os.environ["PRITSET_SECRET"],
timeout=120.0,
http_client=http_client,
) as client:
# The injected client remains caller-owned.
page = client.templates.list()

HTTPS is required except for an explicit local test server:

with PritsetClient(
access_token="test-token",
secret="test-secret",
base_url="http://127.0.0.1:5000",
allow_insecure_http=True,
) as local:
page = local.templates.list()

Next stepsโ€‹