Print receipts from Express
An Express route can post an ESC/POS receipt when an order is created. The API key stays in the Express process, not in the static frontend.
For Express APIs that take orders for a counter printer
Overview
Express is usually the order API. After you persist the order, build the receipt bytes and POST them before you return 201 to the client. Do not block the request for the physical print; the print call itself returns when the job is queued.
ESC/POS is content_type raw. A PDF receipt is pdf. Pick one per printer. The agent will not convert HTML from res.render into a receipt.
Share the Idempotency-Key with any other worker that might also print that order, or you will get two tickets.
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
// Express handler: ESC/POS receipt
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': order.id,
},
body: JSON.stringify({
printer_code: Number(process.env.PRINTER_CODE),
content_type: 'raw',
content: receipt.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
Do not print in middleware for every request
Only the order-create handler should print. A logging middleware that prints will burn paper.
Encoding
Build the Buffer in the code page the receipt printer expects. UTF-8 Chinese will look wrong on most ESC/POS devices.
Questions
Can the React frontend call Express, and Express call PrintBase?
Yes. That is the right split. The frontend never sees the print key.
Body parser and base64?
You send JSON out. You do not need the receipt printer to post back into Express.
Cluster mode?
Any worker can POST. The idempotency key has to be the order id, not a random uuid per worker.
How do I know the kitchen saw it?
Poll the job or use a paid webhook. Express's res.json of the create response is only queued.
Free plan: 100 jobs a month on one computer, no card.