@caretly/caretly package has no runtime dependencies and is built on the standard fetch. It ships ESM, CommonJS and TypeScript types.
- Package:
@caretly/caretlyon npm - Source:
clients/node
Install
@caretly scope. Import from @caretly/caretly, not caretly.
Quickstart
Create a test key in the console and set it in your environment:ESM, CommonJS and runtimes
The package works withimport and require:
instanceof checks on errors work even when an application loads both builds.
It runs on Node.js 18 or newer. The core also runs in Deno, Bun and edge runtimes that have fetch. Only saveTo needs node:fs. Elsewhere, read result.bytes instead.
Render a page as PNG
A PNG holds one page. Choose it withpage, starting at 1:
caretly.renderPdf(zpl) is the matching shorthand for a PDF.
Render several files
renderFiles sends the files as multipart/form-data. Each file starts from a fresh printer state, and pages follow the order of the files:
Render options
render(zpl, options) and renderFiles(files, options) take:
Options you leave out aren’t sent, so the server defaults apply. An invalid
format, page, dpmm or idempotencyKey throws a RangeError before any request.
Read the result
Handle errors
Every error from the API or the network extendsCaretlyError:
APIError has status, message (the server’s text), retryAfter (seconds or null) and headers.
signal rejects with the signal’s reason, an AbortError by default, not a CaretlyError.
Retries and idempotency
The client retries429, 503 and connection errors up to maxRetries 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. Timeouts and other statuses aren’t retried.
When retries are on and you pass no idempotencyKey, 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
Custom fetch
Pass your ownfetch to add logging, route requests through a proxy, or mock them in tests: