Silent printing from browser JavaScript, via your backend
Browsers will not silently print to a USB device from a public website. Call your backend, and let the backend call the print API.
For Frontend developers who need a receipt without window.print
Overview
window.print() always involves the browser's dialog and the user's printer choice. A page on https://shop.example cannot open a raw socket to a printer, and Chrome's private-network rules also block casual calls to localhost device services.
The pattern that works is: the button calls your backend; the backend holds the API key; the backend posts the job; the agent in the shop prints. The browser receives a job id, not a printer handle.
The agent's optional local port 127.0.0.1:17891 accepts the same JSON, but only from localhost or a printbase.cloud origin. Your shop's origin is rejected on purpose.
Integration steps
- 01
Do not put the key in the bundle
Search the built JavaScript. If the key is there, rotate it.
- 02
Call your backend
The button posts the order id, not the print payload, unless the backend trusts that user.
- 03
Backend posts the job
Authorization Bearer, printer_code from your database, content_type matching the device.
- 04
Show queued, then poll
The UI can poll your backend, which polls GET /v1/print-jobs/{id}.
- 05
Do not point the browser at 127.0.0.1:17891 in production
That origin check will fail, and it would only reach a printer on the visitor's laptop anyway.
Node.js
// Backend endpoint the browser calls: it then prints
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': orderId,
},
body: JSON.stringify({
printer_code: Number(process.env.PRINTER_CODE),
content_type: 'pdf',
content: pdf.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
Why the dialog exists
The browser treats printing as a user decision. Silent print is a server-side job plus a local agent, not a hidden iframe.
Local port allow-list
Code in the agent allows an empty Origin, localhost, 127.0.0.1, ::1, and printbase.cloud hosts. Other origins get 403.
Questions
Can I hide the dialog with CSS?
No. That is not a supported browser feature, and it would still print on the visitor's machine.
What about QZ Tray or DYMO's local service?
Those are local agents the browser talks to, with their own certificates or ports. This design moves the call to your server. See the comparison pages.
A kiosk page on localhost?
The local agent port can accept that origin. A public kiosk URL should still use the cloud API so the key is not in the page.
How does the cashier know it failed?
Your UI reads the job status. The browser will not throw a printer error by itself.
Free plan: 100 jobs a month on one computer, no card.