caretly package has a sync client, caretly.Client, and an asyncio client, caretly.AsyncClient, with the same API. It’s typed and built on httpx.
- Package:
caretlyon PyPI - Source:
clients/python
Install
Quickstart
Create a test key in the console and set it in your environment:zpl can be a str, sent as UTF-8, or bytes.
Sync and async
Usecaretly.AsyncClient in asyncio code. Its methods are coroutines, and it closes with await client.aclose() or async with:
client.close() or a with block, to release its connections. Create one client and reuse it for many renders.
Render a page as PNG
A PNG holds one page. Choose it withpage, starting at 1:
client.render_pdf(zpl) is the matching shorthand for a PDF.
Render several files
render_files sends the files as multipart/form-data. Each file starts from a fresh printer state, and pages follow the order of the files:
(name, content) pairs. Content is str or bytes.
Render options
render and render_files take these keyword arguments:
An out-of-range
page or dpmm raises ValueError before anything is sent.
Read the result
RenderResult is a frozen dataclass:
Handle errors
All errors derive fromcaretly.CaretlyError:
APIError has status_code, message (the server’s plain-text explanation), retry_after (seconds or None) and response (the httpx.Response).
Retries and idempotency
The client retries429 and 503 responses and connection errors up to max_retries times, 2 by default. It waits Retry-After seconds when the server sends it. Otherwise it backs off 0.5 s, 1 s, 2 s, up to 8 s. Other statuses aren’t retried.
Timeouts aren’t retried either: the client raises APITimeoutError on the first one, because the server may still be rendering.
When retries are on and you don’t set idempotency_key, the client generates a UUID per call and sends it on every attempt, so a retried request is never billed twice. Set your own key, such as an order number, to make retries across calls or processes safe too:
Configuration
Bring your own httpx client
Passhttp_client to use your own proxies, transports or connection limits. Use an httpx.Client with Client and an httpx.AsyncClient with AsyncClient. You keep ownership: closing the Caretly client doesn’t close yours.
Health check
health returns False for any other answer, and raises APIConnectionError when the server can’t be reached.