Skip to main content
The caretly crate has an async client for tokio and a blocking client. Both share the same options, result and error types.

Install

The async examples on this page also need tokio with a runtime and the main macro:
The crate needs Rust 1.85 or newer.

Feature flags

To use only the blocking client, without tokio in your dependency tree:
Cargo.toml
Keep rustls-tls or native-tls on to reach https:// URLs. Without either, only http:// works, for example a self-hosted server on localhost.

Quickstart

Create a test key in the console and set it in your environment:
Render a label to PDF and save it:
render and render_pdf accept a &str, String, &[u8] or Vec<u8>.

Async and blocking

caretly::Client is async and runs on tokio. With the blocking feature, caretly::blocking::Client has the same methods without .await:
Don’t call the blocking client from inside an async runtime. Use the async client there.

Render a page as PNG

A PNG holds one page. Choose it with page, starting at 1:

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:
Pass any iterator of (name, content) pairs. File contents can be &str, String, &[u8] or Vec<u8>.

Render options

Build options with RenderOptions::new(), or pass Default::default() for the server defaults: Unset options aren’t sent, so the server defaults apply. Dpmm::try_from(n) converts a number and fails on anything other than 6, 8, 12 or 24. page(0), an empty or too long idempotency key, and an empty file list fail with Error::InvalidArgument before anything is sent.

Read the result

RenderResult has public fields:

Handle errors

Every call returns Result<_, caretly::Error>: The API variants hold an ApiError with status, message (the response body) and retry_after. Use Error::status(), Error::retry_after() and Error::api_error() to read them without matching.

Retries and idempotency

The client retries a 429, a 503 or a connection error 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, 4 s, then 8 s. Timeouts and other statuses aren’t retried. When retries are on and you don’t set an idempotency key, the client generates a UUID v4 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

The blocking ClientBuilder has the same methods. An empty API key sends no Authorization header, for self-hosted servers without keys. The client builds its own reqwest client, so you can’t inject one. Choose the TLS backend with the rustls-tls and native-tls features.

Health check