Python SDK - Coming Soon
Printivo Python SDK
Generate QR codes, render ZPL labels, and create barcodes with a Pythonic API. Sync and async support, type hints, and dataclass models. Python 3.9+.
Installation
Install the Printivo Python SDK via pip.
pip
poetry
# pip
pip install printivo
# poetry
# poetry add printivo
# With async support (includes aiohttp)
# pip install printivo[async]Requirements
- Python 3.9 or later
- A Printivo API key (get one free)
Quick Start
Initialize the client and make your first API call.
Sync
Async
from printivo import PrintivoClient
# Initialize with API key
client = PrintivoClient(api_key="your_api_key")
# Or with full configuration
client = PrintivoClient(
api_key="your_api_key",
base_url="https://api.printivo.cloud", # optional, this is the default
timeout=30,
max_retries=3,
)
# Async client
# from printivo import AsyncPrintivoClient
# client = AsyncPrintivoClient(api_key="your_api_key")Generate QR Codes
Create QR codes with full control over content, styling, and output format.
Basic
Styled
Async
from printivo import PrintivoClient
from printivo.models import QrCodeRequest, ContentType, OutputFormat
client = PrintivoClient(api_key="your_api_key")
# Generate a URL QR code
qr_image = client.qr_codes.generate(QrCodeRequest(
content="https://example.com",
content_type=ContentType.URL,
size=400,
format=OutputFormat.PNG,
error_correction="M",
))
# Save to file
with open("qrcode.png", "wb") as f:
f.write(qr_image)
# Generate a styled QR code
styled_qr = client.qr_codes.generate(QrCodeRequest(
content="https://example.com",
content_type=ContentType.URL,
size=600,
format=OutputFormat.SVG,
foreground_color="#1e40af",
background_color="#ffffff",
module_shape="Rounded",
error_correction="H",
))
with open("styled-qr.svg", "wb") as f:
f.write(styled_qr)Render ZPL Labels
Convert ZPL code to high-quality PNG or PDF images.
Render Label
from printivo.models import LabelRequest
# Render a shipping label
zpl_code = """^XA
^FO50,50^A0N,40,40^FDOrder #12345^FS
^FO50,120^BY3^BCN,100,Y,N,N^FDSHIP-2024-001^FS
^FO50,260^FDJane Smith^FS
^FO50,310^FD456 Oak Avenue^FS
^FO50,360^FDSan Francisco, CA 94102^FS
^XZ"""
label_image = client.labels.render(LabelRequest(
zpl=zpl_code,
dpi=203,
format=OutputFormat.PNG,
width=4.0,
height=6.0,
))
with open("shipping-label.png", "wb") as f:
f.write(label_image)
# Render to PDF for printing
label_pdf = client.labels.render(LabelRequest(
zpl=zpl_code,
dpi=300,
format=OutputFormat.PDF,
width=4.0,
height=6.0,
))
with open("label.pdf", "wb") as f:
f.write(label_pdf)Generate Barcodes
Create barcodes of any supported symbology with input validation.
EAN-13
Code 128
from printivo.models import BarcodeRequest, BarcodeType
# Generate an EAN-13 barcode
ean13 = client.barcodes.generate(BarcodeRequest(
data="1234567890128",
type=BarcodeType.EAN13,
width=300,
height=150,
show_text=True,
format=OutputFormat.PNG,
))
with open("ean13.png", "wb") as f:
f.write(ean13)
# Generate a Code 128 barcode
code128 = client.barcodes.generate(BarcodeRequest(
data="SHIP-2024-001",
type=BarcodeType.CODE128,
width=400,
height=100,
show_text=True,
))
with open("code128.png", "wb") as f:
f.write(code128)Error Handling
The SDK provides typed exceptions for predictable error handling.
Error Handling
from printivo.exceptions import (
PrintivoAuthError,
PrintivoRateLimitError,
PrintivoValidationError,
PrintivoApiError,
)
try:
qr = client.qr_codes.generate(request)
except PrintivoAuthError:
# Invalid or missing API key (401)
print("Check your API key.")
except PrintivoRateLimitError as e:
# Rate limit exceeded (429)
print(f"Rate limited. Retry after {e.retry_after}s")
except PrintivoValidationError as e:
# Invalid request (400)
for error in e.errors:
print(f"{error.field}: {error.message}")
except PrintivoApiError as e:
# Other API errors
print(f"API error: {e.status_code} - {e.message}")Framework Integration
Use Printivo in Django, FastAPI, Flask, and other Python frameworks.
FastAPI
Django
from fastapi import FastAPI
from fastapi.responses import Response
from printivo import AsyncPrintivoClient
from printivo.models import QrCodeRequest, ContentType, OutputFormat
app = FastAPI()
client = AsyncPrintivoClient(api_key="your_api_key")
@app.get("/qr/{content}")
async def generate_qr(content: str):
"""Generate a QR code for the given content."""
qr_image = await client.qr_codes.generate(QrCodeRequest(
content=content,
content_type=ContentType.URL,
size=400,
format=OutputFormat.PNG,
))
return Response(
content=qr_image,
media_type="image/png",
headers={"Cache-Control": "public, max-age=3600"},
)
@app.get("/label")
async def render_label(zpl: str):
"""Render a ZPL label."""
from printivo.models import LabelRequest
label = await client.labels.render(LabelRequest(
zpl=zpl,
dpi=203,
format=OutputFormat.PNG,
width=4.0,
height=6.0,
))
return Response(content=label, media_type="image/png")Coming Soon
The Python SDK is under active development. Sign up to be notified when it launches, or use the REST API directly in the meantime.