Print receipts from Django
Print from a Django view or signal after the order is saved. Use the server environment for the API key, not settings committed to git.
For Django projects that take orders
Overview
Put the print call in the view, a service function, or a transaction.on_commit hook so you do not print an order that rolled back. Django's development server is not the printer. The agent is.
Read the key from the environment. A fixture or a template must not render it. printer_code can live on a Store model if each shop has its own queue.
The HTTP call is short. Do not hold the database lock while you poll for completed. Store the job id and poll from a worker or the next request.
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
# Django service: receipt
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": str(order.pk),
},
)
with urllib.request.urlopen(req) as res:
job = json.load(res) # {"id": "job_abc", "status": "queued"}
Details for this setup
on_commit
Printing inside an atomic block can print even when the order insert rolls back, if you printed before the rollback. Wait for commit.
Store model
A foreign key to a shop should carry printer_code. Do not trust a printer_code posted by the browser unless you check it.
Questions
Management command instead of a view?
Yes, for a batch. Same POST. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.
Celery? in this setup
A task can POST. Pass the order id, not the API key, as the task argument. Read the key in the worker environment.
CSRF? in this setup
The print call is outbound. CSRF does not apply to it. Your own endpoint that triggers printing still needs the usual Django protection.
Multiple databases?
Irrelevant to the print API. Just do not print until the order row is durable.
Free plan: 100 jobs a month on one computer, no card.