Rust quickstart
This page shows how to convert labels from Rust with the LabelZoom Rust SDK.
Before you begin
Section titled “Before you begin”- Install Rust 1.85 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 labelzoom crate from crates.io. The
Fill variable fields example also needs serde_json:
cargo add labelzoom serde_jsonOr add them to Cargo.toml:
[dependencies]labelzoom = "2"serde_json = "1"Convert ZPL to PDF
Section titled “Convert ZPL to PDF”Replace the contents of src/main.rs with the following program:
use labelzoom::{ConversionOptions, ConvertRequest, LabelZoomClient, SourceFormat, TargetFormat};
fn main() -> Result<(), Box<dyn std::error::Error>> { let client = LabelZoomClient::new(); // reads LABELZOOM_API_KEY
let result = client.convert( &ConvertRequest::new( SourceFormat::Zpl, TargetFormat::Pdf, "^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ", ) .options(ConversionOptions::new().label_size(4.0, 6.0)), // inches )?;
result.save("label.pdf")?; Ok(())}The SDK reads your API key from the LABELZOOM_API_KEY environment variable. To pass a key
explicitly, use LabelZoomClient::builder().api_key("API_KEY").build()?.
The client is blocking. In async code, call it inside tokio::task::spawn_blocking.
Run the program with cargo run. It writes label.pdf.
Convert a PDF to ZPL
Section titled “Convert a PDF to ZPL”let pdf = std::fs::read("shipping-label.pdf")?;
let result = client.convert( &ConvertRequest::new(SourceFormat::Pdf, TargetFormat::Zpl, pdf).options( ConversionOptions::new() .pdf_page(0) // 0-based; omit to convert every page .target_dpi(203), // the printer's dpi ),)?;
println!("{}", result.text());ConversionOptions takes two kinds of option. Source options, such as pdf_page and
label_size, change how the document is read. Target options, such as target_dpi, change the
output. A PDF target takes no dpi.
Write PDF, image, and printer-language (EPL, IPL, TSPL, DPL, SBPL) output as bytes (result.bytes), not text; see
Binary output.
Fill variable fields
Section titled “Fill variable fields”use serde_json::{json, Map, Value};
let template = "^XA^FO50,50^A0N,30,30^FD{name}^FS^FO50,100^A0N,30,30^FD{sku}^FS^XZ";let records: Vec<Map<String, Value>> = vec![ serde_json::from_value(json!({ "name": "ACME Corp", "sku": "12345" }))?, serde_json::from_value(json!({ "name": "Globex", "sku": "67890" }))?,];
let result = client.convert( &ConvertRequest::new(SourceFormat::Zpl, TargetFormat::Zpl, template) .options(ConversionOptions::new().with_data(records)), // one label per record)?;
println!("{}", result.text());Each placeholder, such as {name}, takes its value from the matching key in each record; see
Merge fields for the placeholder syntax.
Handle errors
Section titled “Handle errors”let request = ConvertRequest::new( SourceFormat::Zpl, TargetFormat::Pdf, "^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ",);
match client.convert(&request) { Ok(result) => result.save("label.pdf")?, Err(labelzoom::Error::Api(e)) => eprintln!( "HTTP {} ({}), request ID {}: {}", e.status, e.code.as_deref().unwrap_or("no code"), e.request_id.as_deref().unwrap_or("none"), e.message ), Err(other) => return Err(other.into()),}The SDK retries 429 and 5xx responses automatically before it returns an error. An invalid
option value, such as a rotation that isn’t a multiple of 90, returns Error::Validation before
any request is sent. For status codes and error codes, see Errors.
Call the API without the SDK
Section titled “Call the API without the SDK”To call the API with an HTTP client instead of the SDK, send the label as the request body. This
example uses ureq 3; add it with cargo add ureq:
fn main() -> Result<(), Box<dyn std::error::Error>> { let zpl = "^XA^FO50,50^ADN,36,20^FDLabelZoom^FS^XZ"; let params = r#"{"label":{"width":4,"height":6}}"#;
let mut request = ureq::post("https://api.labelzoom.com/api/v2/convert/zpl/to/pdf") .query("params", params) .header("Content-Type", "text/plain") .header("Accept", "*/*");
// The API key is optional. Omit the Authorization header if it isn't set. if let Ok(key) = std::env::var("LABELZOOM_API_KEY") { request = request.header("Authorization", &format!("Bearer {key}")); }
// ureq returns an error for any status other than 2xx. let mut response = request.send(zpl)?; std::fs::write("label.pdf", response.body_mut().read_to_vec()?)?; Ok(())}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 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.