How it fits together
PrintEzy Agent runs on a computer in the customer’s office. Every 2 seconds it asks your web app for print jobs and sends them straight to the chosen printer. It only makes outbound calls, so your web app can be anywhere on the internet and nothing needs to be opened in the customer’s network.
You add two endpoints. The customer enters your web app’s base address (and an access key, if you use one) in PrintEzy Agent. You keep full control of the label design.
| Step | Who | What |
|---|---|---|
| 1 | Your web app | User presses Print. You insert a job with status queued. |
| 2 | Agent → you | POST /api/print-agent/claim. You return waiting jobs and mark them sending. |
| 3 | Agent → printer | Sends data to the printer unchanged. |
| 4 | Agent → you | POST /api/print-agent/jobs/{id}/result with ok or error. |
1. Claim jobs
POST {web app address}/api/print-agent/claim
Content-Type: application/json
Authorization: Bearer {access key} (only if the customer entered a key)
{}Respond 200 with a JSON array of up to about 10 jobs, or [] when nothing is waiting.
[
{ "id": "1234", "data": "SIZE 4,6\r\nCLS\r\nTEXT 40,40,\"4\",0,1,1,\"Hello\"\r\nPRINT 1,1\r\n" }
]| Field | Meaning |
|---|---|
id | Your job id (string or number). Used in the result URL. |
data | Raw printer commands as text, in the printer’s language (TSPL, ZPL, ESC/POS…). Any size. Sent to the printer as-is. |
dataBase64 | Use instead of data for binary content (images, fonts). |
host, port | Optional. Only used when the customer chose “Printer chosen by the web app” for this connection. port defaults to 9100. |
FOR UPDATE SKIP LOCKED as in the examples below. Return 401 for a missing or wrong access key: the customer sees “Web app answered HTTP 401”.2. Receive the result
POST {web app address}/api/print-agent/jobs/{id}/result
Content-Type: application/json
Authorization: Bearer {access key}
{ "ok": true }
{ "ok": false, "error": "Printer problem: out of labels" }Reply 200. The body is ignored. Show error to the user word for word. It is already written for them.
Error messages
| Message | Meaning / what the user should do |
|---|---|
Printer problem: out of labels | Load a new label roll. |
Printer problem: out of ribbon | Replace the ribbon. |
Printer problem: print head open | Close the print head. |
Printer problem: paper jam | Clear the jammed labels. |
Printer problem: printer paused | Press the pause/feed button on the printer. |
Printer 192.168.1.50 is not responding | The printer is off, unplugged or its IP address changed. |
No printer is selected for this web app connection… | Choose a printer for this connection in PrintEzy Agent, or send host with the job. |
| Subscription messages | For example, the trial has ended, the subscription is in its grace period, or printing has stopped. The user should contact whoever pays for PrintEzy. |
Status checks (out of labels, head open…) work on TSC printers. Other printers are checked for being reachable. If there are several problems, they are listed together, for example Printer problem: print head open, out of labels.
Example: Node.js (Express + PostgreSQL)
// Express + PostgreSQL (node-postgres). Table: print_jobs(id, customer_id, status, data, error, created_at, updated_at)
import express from 'express';
import pg from 'pg';
const app = express();
const db = new pg.Pool();
app.use(express.json());
// Find the customer from the access key the agent sends.
async function customerFromKey(req) {
const key = (req.get('authorization') || '').replace(/^Bearer\s+/i, '');
if (!key) return null;
const { rows } = await db.query('SELECT id FROM customers WHERE print_agent_key = $1', [key]);
return rows[0] || null;
}
// 1. Claim jobs: atomically, so a job is never returned twice.
app.post('/api/print-agent/claim', async (req, res) => {
const customer = await customerFromKey(req);
if (!customer) return res.status(401).json({ error: 'Invalid access key' });
const { rows } = await db.query(`
UPDATE print_jobs SET status = 'sending', updated_at = now()
WHERE id IN (
SELECT id FROM print_jobs
WHERE customer_id = $1 AND status = 'queued'
ORDER BY created_at
FOR UPDATE SKIP LOCKED
LIMIT 10
)
RETURNING id, data`, [customer.id]);
res.json(rows.map(r => ({ id: String(r.id), data: r.data })));
});
// 2. Receive the result.
app.post('/api/print-agent/jobs/:id/result', async (req, res) => {
const customer = await customerFromKey(req);
if (!customer) return res.status(401).json({ error: 'Invalid access key' });
const { ok, error } = req.body || {};
await db.query(
"UPDATE print_jobs SET status = $1, error = $2, updated_at = now() WHERE id = $3 AND customer_id = $4",
[ok ? 'printed' : 'failed', ok ? null : String(error || 'Print failed'), req.params.id, customer.id]);
res.json({ ok: true });
});
app.listen(3000);Example: PHP (PDO)
<?php
// claim.php -> route POST /api/print-agent/claim here
// Uses PDO with PostgreSQL. For MySQL 8+, the same query works with FOR UPDATE SKIP LOCKED
// inside a transaction (SELECT ... FOR UPDATE SKIP LOCKED, then UPDATE those ids).
$pdo = new PDO(getenv('DB_DSN'), getenv('DB_USER'), getenv('DB_PASS'), [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$key = preg_replace('/^Bearer\s+/i', '', $_SERVER['HTTP_AUTHORIZATION'] ?? '');
$stmt = $pdo->prepare('SELECT id FROM customers WHERE print_agent_key = ?');
$stmt->execute([$key]);
$customerId = $stmt->fetchColumn();
if (!$key || !$customerId) { http_response_code(401); echo json_encode(['error' => 'Invalid access key']); exit; }
$stmt = $pdo->prepare("
UPDATE print_jobs SET status = 'sending', updated_at = now()
WHERE id IN (
SELECT id FROM print_jobs
WHERE customer_id = ? AND status = 'queued'
ORDER BY created_at
FOR UPDATE SKIP LOCKED
LIMIT 10
)
RETURNING id, data");
$stmt->execute([$customerId]);
header('Content-Type: application/json');
echo json_encode(array_map(fn($r) => ['id' => (string)$r['id'], 'data' => $r['data']], $stmt->fetchAll(PDO::FETCH_ASSOC)));<?php
// result.php -> route POST /api/print-agent/jobs/{id}/result here, with $jobId taken from the URL
$body = json_decode(file_get_contents('php://input'), true) ?: [];
$ok = !empty($body['ok']);
$error = $ok ? null : (string)($body['error'] ?? 'Print failed');
// ...check the access key exactly as in claim.php, giving $customerId...
$stmt = $pdo->prepare('UPDATE print_jobs SET status = ?, error = ?, updated_at = now() WHERE id = ? AND customer_id = ?');
$stmt->execute([$ok ? 'printed' : 'failed', $error, $jobId, $customerId]);
header('Content-Type: application/json');
echo json_encode(['ok' => true]);Print data: any size, any language
There is no fixed size or format. The printer receives exactly what you send, so you choose the size and layout in your commands: a 25 mm price sticker, a shipping label, an 80 mm receipt or a full A4 page. Use the printer’s own language: TSPL for TSC, ZPL for Zebra, ESC/POS for receipt printers, PCL or PostScript for office printers. Send binary content (images, fonts, PCL) with dataBase64. Keep templates in your web app and fill them per job. End TSPL lines with \r\n.
SIZE 4,6
GAP 0.12,0
DIRECTION 1
CLS
TEXT 40,40,"4",0,1,1,"DISPATCH DSP-24-01877"
TEXT 40,110,"3",0,1,1,"Ship to: Sample Industries Pvt Ltd"
BARCODE 40,180,"128",120,1,0,3,3,"8901234567894"
QRCODE 560,40,L,6,A,0,"https://erp.example.com/d/1877"
PRINT 1,1^XA
^PW812
^LL1218
^FO40,40^A0N,50,50^FDDISPATCH DSP-24-01877^FS
^FO40,120^A0N,35,35^FDShip to: Sample Industries Pvt Ltd^FS
^FO40,200^BY3^BCN,120,Y,N,N^FD8901234567894^FS
^XA// When a user presses "Print label": build the label and queue it. Don't wait for the printer.
const data = [
'SIZE 4,6', 'CLS',
`TEXT 40,40,"4",0,1,1,"${order.dispatchNo}"`,
`BARCODE 40,180,"128",120,1,0,3,3,"${order.barcode}"`,
'PRINT 1,1', ''
].join('\r\n');
await db.query("INSERT INTO print_jobs (customer_id, status, data) VALUES ($1, 'queued', $2)", [customerId, data]);
// The page then polls the job's status and shows "Printed" or the error text word for word.Test your endpoints
curl -X POST https://erp.example.com/api/print-agent/claim \
-H "Authorization: Bearer YOUR-ACCESS-KEY" -H "Content-Type: application/json" -d "{}"
curl -X POST https://erp.example.com/api/print-agent/jobs/1234/result \
-H "Authorization: Bearer YOUR-ACCESS-KEY" -H "Content-Type: application/json" \
-d "{\"ok\": false, \"error\": \"Printer problem: out of labels\"}"Then add a connection in PrintEzy Agent with your address and key. It shows Connected once your claim endpoint answers 200.
Recommendations
- Use an access key per customer, and reject claim calls without it.
- Time out stuck jobs. Mark a job
failedif it’s stillqueuedafter about 5 minutes (the agent is offline) or stillsendingafter about 2 minutes. - Never re-send automatically. It risks duplicate labels. Let the user press Print again.
- Keep templates in your web app (TSPL/ZPL) so labels stay pixel-exact and you control changes.
- Answer quickly. The agent waits up to 15 seconds for your endpoints.
- Use HTTPS. The access key is sent in the
Authorizationheader.
Questions about an integration? Contact us. We’re happy to look at your endpoint responses.