PrintBase
  • Product
  • Pricing
  • Docs
  • Resources
PrintBase/Guides/Print from your language or framework

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

  1. 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.

  2. 02

    Install the agent beside the printer

    Windows, macOS, or Linux. GET /v1/printers and keep the numeric code.

  3. 03

    POST /v1/print-jobs

    content_type is pdf or raw. Send exactly one of content (standard base64) or content_url.

  4. 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.

  5. 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.

Related guides

  • Print a PDF from Python

    Use the Python standard library to POST a base64 PDF. No extra print package is required. The agent prints it on the computer you chose.

  • Print ESC/POS receipts from PHP

    From PHP, curl an ESC/POS payload to the print API. Shared hosting is enough because the call is outbound HTTPS.

  • Print ESC/POS receipts on an Epson TM-T20III

    Send ESC/POS bytes to a TM-T20III as raw, or a PDF if you rely on the Epson driver. 80 mm paper, code page is your responsibility.

  • Receipt printer prints garbage characters

    ESC/POS garbage is almost always the code page. The agent does not transcode raw bytes. Encode for the printer before base64.

Free plan: 100 jobs a month on one computer, no card.

Create a free accountRead the API docs
PrintBase

PrintBase connects your applications to physical printers anywhere - securely, reliably, and in real time.

Product
Receipt PrintingLabel PrintingDashboardDevicesWebhooks
Resources
DocsBlogStatusAPI ReferenceDownloadPrintBase vs PrintNodePrintNode PricingFAQ
Guides
All guidesIntegrationsPrintersCode samplesComparisonsUse casesTroubleshooting
Company
ContactSecurityPrivacy

© 2026 Aveniq Technologies OÜ. All rights reserved. PrintBase is a service of Aveniq Technologies OÜ.

PrivacyTermsRefunds