Print raw ZPL from Go
Go's net/http posts ZPL with no extra module. Base64 from encoding/base64. The agent prints the Zebra queue.
For Go services printing labels
This page in another language: 中文
Overview
The Go standard library is enough: bytes.Buffer or a string of ZPL, base64.StdEncoding, and http.NewRequest. Set the Idempotency-Key header. Do not depend on a printer daemon inside the container.
The marketing site lists an official Go SDK. This sample is the REST call. Module path and client types are not repeated here because the HTTP shape is the stable contract.
This page is the English counterpart of the Chinese Go ZPL page.
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.
Go
// Go ZPL
body, _ := json.Marshal(map[string]any{
"printer_code": printerCode,
"content_type": "raw",
"content": base64.StdEncoding.EncodeToString([]byte(zpl)),,
"copies": 1,
})
req, _ := http.NewRequest(http.MethodPost, "https://api.printbase.cloud/v1/print-jobs", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("PRINTBASE_API_KEY"))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", idempotencyKey)
res, err := http.DefaultClient.Do(req)
if err != nil { log.Fatal(err) }
defer res.Body.Close()
// 201 {"id":"job_abc","status":"queued"}
Details for this setup
Context timeouts
Use http.NewRequestWithContext and a few-second timeout. A stuck dial should not park a goroutine for minutes.
JSON numbers
printer_code must be a JSON number, not a string, unless you have tested a string. Send the integer.
Questions
cgo and CUPS?
Unnecessary for a remote printer. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.
Does the container need the Zebra driver?
No. The agent host needs it. The call is still POST /v1/print-jobs with a Bearer API key, and HTTP 201 means queued rather than printed.
bytes versus string?
ZPL is ASCII-compatible. Encode as UTF-8 and base64. Do not base64 the Go quoted syntax.
Retry policy?
Retry the same key on network errors. Do not generate a new key in the retry loop.
Free plan: 100 jobs a month on one computer, no card.