Print labels from a React admin panel
A React button calls your API. Your API prints the label. The React bundle does not contain the print key or a direct call to the printer.
For Internal React tools used at a bench
Overview
React state can hold the selected order and the selected station. It should not hold PRINTBASE_API_KEY. The button's onClick fetches /api/labels/:id/print on your origin. That route does the PrintBase POST.
Load stations from your API, which proxies GET /v1/printers, so the browser never needs the printers:read scope either if you do not want it to.
Disable the button while the request is in flight, and key the print to the label id so a double click is one job.
Create the job with POST /v1/print-jobs, an Authorization: Bearer header, printer_code, content_type pdf or raw, and exactly one of content or content_url. The free plan is 100 jobs a month on one computer.
Integration steps
- 01
Create an API key on the server
Use an API key (pb_live_…) in the process environment. Authorization is Bearer. Do not ship the key to browsers or mobile apps.
- 02
Install the agent beside the printer
Windows, macOS, or Linux. GET /v1/printers and keep the numeric code.
- 03
POST /v1/print-jobs
content_type is pdf or raw. Send exactly one of content (standard base64) or content_url.
- 04
Reuse the idempotency key on retry
A new key means a second physical print. HTTP 409 means the same key was reused with a different body.
- 05
Read the job before you tell the user
GET /v1/print-jobs/{id}. Paid plans can also receive print_job.completed and print_job.failed webhooks.
Node.js
// Your /api/labels/:id/print handler
const res = await fetch('https://api.printbase.cloud/v1/print-jobs', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.PRINTBASE_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': labelId,
},
body: JSON.stringify({
printer_code: Number(process.env.PRINTER_CODE),
content_type: 'raw',
content: Buffer.from(zpl,'utf8').toString('base64'),
copies: 1,
}),
});
if (!res.ok) throw new Error(`PrintBase ${res.status}: ${await res.text()}`);
const job = await res.json();
// { id: "job_abc", status: "queued" }
Details for this setup
Proxy the printer list
Return id, name, and code to the select. Do not return secrets. The agent prints only what you send; it does not fetch the document from the platform for you.
Optimistic UI
Show queued when your route returns the job id. Show the failure reason when the poll says so.
Questions
Can React Query call PrintBase directly?
Technically, and then the key is in the browser. Do not. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.
Next.js and Vite are the same rule?
Yes. The bundler does not matter. The key must not be in the client environment.
How do I reprint?
A second request with a reprint flag that uses a new idempotency key on the server.
Which content type?
The server decides from the station's printer kind. The button does not send ZPL it copied from a tutorial unless you intend that.
Free plan: 100 jobs a month on one computer, no card.