Go quickstart
This page shows how to convert labels from Go with the LabelZoom Go SDK.
Before you begin
Section titled “Before you begin”- Install Go 1.23 or later.
- Optional: Get an API key. Without one, output has a watermark. See Authentication.
Install the SDK
Section titled “Install the SDK”Add the module to your project:
go get github.com/labelzoom/labelzoom-sdk/go/v2Convert ZPL to PDF
Section titled “Convert ZPL to PDF”Save the following program as main.go:
package main
import ( "context" "log"
labelzoom "github.com/labelzoom/labelzoom-sdk/go/v2")
func main() { client, err := labelzoom.New() // reads LABELZOOM_API_KEY if err != nil { log.Fatal(err) }
result, err := client.Convert(context.Background(), labelzoom.ConvertRequest{ From: labelzoom.SourceZPL, Body: []byte("^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ"), To: labelzoom.TargetPDF, Options: &labelzoom.Options{ Label: &labelzoom.LabelSize{Width: labelzoom.Ptr(4.0), Height: labelzoom.Ptr(6.0)}, // inches }, }) if err != nil { log.Fatal(err) }
if err := result.Save("label.pdf"); err != nil { log.Fatal(err) }}The SDK reads your API key from the LABELZOOM_API_KEY environment variable. To pass a key
explicitly, use labelzoom.New(labelzoom.WithAPIKey("API_KEY")).
Options are pointers, so build them with labelzoom.Ptr. A float field needs a float literal:
write labelzoom.Ptr(4.0), not labelzoom.Ptr(4).
Run the program with go run .. It writes label.pdf.
Convert a PDF to ZPL
Section titled “Convert a PDF to ZPL”pdf, err := os.ReadFile("shipping-label.pdf")if err != nil { log.Fatal(err)}
converted, err := client.Convert(context.Background(), labelzoom.ConvertRequest{ From: labelzoom.SourcePDF, Body: pdf, To: labelzoom.TargetZPL, Options: &labelzoom.Options{ PDF: &labelzoom.PDFOptions{PageNumber: labelzoom.Ptr(0)}, // 0-based; omit to convert every page TargetDPI: labelzoom.Ptr(203), // the printer's density },})if err != nil { log.Fatal(err)}
fmt.Println(converted.Text())PDF sets how the PDF is read, and TargetDPI sets the density of the ZPL. Don’t set SourceDPI
for a PDF source: it returns 400.
Write PDF, image, and printer-language (EPL, IPL, TSPL, DPL, SBPL) output as bytes
(converted.Bytes), not text. See Binary output.
Fill variable fields
Section titled “Fill variable fields”template := "^XA^FO50,50^A0N,30,30^FD{name}^FS^FO50,100^A0N,30,30^FD{sku}^FS^XZ"
labels, err := client.Convert(context.Background(), labelzoom.ConvertRequest{ From: labelzoom.SourceZPL, Body: []byte(template), To: labelzoom.TargetZPL, Options: &labelzoom.Options{ Data: []labelzoom.DataRecord{ // one label per record {"name": "ACME Corp", "sku": "12345"}, {"name": "Globex", "sku": "67890"}, }, },})if err != nil { log.Fatal(err)}
fmt.Println(labels.Text())Each placeholder, such as {name}, takes its value from the matching key in each record. For the
placeholder syntax, see Merge fields.
Handle errors
Section titled “Handle errors”if _, err := client.Convert(context.Background(), labelzoom.ConvertRequest{ From: labelzoom.SourceZPL, Body: []byte("^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ"), To: labelzoom.TargetPDF,}); err != nil { var apiErr *labelzoom.APIError if errors.As(err, &apiErr) { log.Fatalf("HTTP %d (%s), request ID %s: %s", apiErr.Status, apiErr.Code, apiErr.RequestID, apiErr.Message) } log.Fatal(err)}apiErr.Code is the error’s code value, or "" when the response has none. If the SDK rejects an
option before sending, such as a rotation that isn’t a multiple of 90, the call returns a
*labelzoom.ValidationError instead.
The SDK retries 429 and 5xx responses and network failures, up to 3 attempts, before it returns
an error. For status codes and code values, see Errors.
Call the API without the SDK
Section titled “Call the API without the SDK”To call the API with net/http instead of the SDK, send the label as the request body:
import ( "context" "fmt" "io" "net/http" "net/url" "os" "strings")
func convert(ctx context.Context) error { zpl := "^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ"
params := url.Values{} params.Set("params", `{"label":{"width":4,"height":6}}`) endpoint := "https://api.labelzoom.com/api/v2/convert/zpl/to/pdf?" + params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(zpl)) if err != nil { return err } req.Header.Set("Content-Type", "text/plain") req.Header.Set("Accept", "*/*") // The API key is optional. Omit the Authorization header if it isn't set. if key := os.Getenv("LABELZOOM_API_KEY"); key != "" { req.Header.Set("Authorization", "Bearer "+key) }
resp, err := http.DefaultClient.Do(req) if err != nil { return err } defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) if err != nil { return err } if resp.StatusCode != http.StatusOK { return fmt.Errorf("LabelZoom returned %d: %s", resp.StatusCode, body) }
return os.WriteFile("label.pdf", body, 0o600)}To convert a PDF to ZPL, send the file’s bytes to /convert/pdf/to/zpl with
Content-Type: application/pdf, and read the body as text. To set the page and the printer’s
density, send {"pdf":{"pageNumber":0},"targetDpi":203} as params.
To set options, send one params query parameter that holds JSON, or use dot notation such as
label.width=4. Accept can be */*, the target’s media type, or a wildcard such as image/*.
See Conversion parameters.