Print ESC/POS receipts from Python
Build ESC/POS bytes in Python and send them as raw. The agent does not transcode. Encode text before base64.
For Python order services printing thermal receipts
Overview
ESC/POS in Python is a bytes object: ESC @, text, GS V. Libraries such as python-escpos can build that buffer. PrintBase does not require one. Whatever you build, base64 it and set content_type raw.
The agent writes those bytes unchanged. If you encoded UTF-8 and the printer expects CP437 or GBK, the paper will be wrong and the job can still be completed.
Do not send the PDF magic by accident. A payload starting with %PDF- is INVALID_CONTENT when content_type is raw.
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.
Python
# Python ESC/POS
import json, os, urllib.request
body = json.dumps({
"printer_code": int(os.environ["PRINTER_CODE"]),
"content_type": "raw",
"content": base64.b64encode(receipt).decode(),,
"copies": 1,
}).encode()
req = urllib.request.Request(
"https://api.printbase.cloud/v1/print-jobs",
data=body,
method="POST",
headers={
"Authorization": f"Bearer {os.environ['PRINTBASE_API_KEY']}",
"Content-Type": "application/json",
"Idempotency-Key": order_id,
},
)
with urllib.request.urlopen(req) as res:
job = json.load(res) # {"id": "job_abc", "status": "queued"}
Details for this setup
Bytes, not a text string that got re-encoded
If you build bytes, base64 those bytes. Encoding the repr of a string will print the word bytes.
58 mm and 80 mm
About 32 and 48 columns. Pad with spaces. The API does not wrap lines. The agent prints only what you send; it does not fetch the document from the platform for you.
Questions
Is python-escpos required?
No. It is one way to build the buffer. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.
Can I open the USB device from Python?
Only on the machine that has the USB device. A cloud function should call the API instead.
Drawer kick?
Include the bytes in the buffer. They are not a JSON option.
How do I reprint?
A new idempotency key. The printer will not dedupe a second different job.
Free plan: 100 jobs a month on one computer, no card.