Python SDK
Installation
pip install flexops
Quick Start
from flexops import FlexOps
client = FlexOps(api_key="your-api-key")
rates = client.rates.get(
from_address={"zip": "80202", "country": "US"},
to_address={"zip": "10001", "country": "US"},
parcel={"weight": 2.5, "length": 10, "width": 8, "height": 4},
)
for rate in rates.rates:
print(f"{rate.carrier} {rate.service}: ${rate.total_price}")
Configuration
client = FlexOps(
api_key="your-api-key",
base_url="https://api.flexops.io/v1", # optional
timeout=30, # seconds, default 30
max_retries=3, # default 3
)
Shipping Operations
Create a Label
label = client.labels.create(
carrier="usps",
service="priority",
from_address={
"name": "Warehouse",
"street1": "123 Main St",
"city": "Denver",
"state": "CO",
"zip": "80202",
"country": "US",
},
to_address={
"name": "Customer",
"street1": "456 Broadway",
"city": "New York",
"state": "NY",
"zip": "10001",
"country": "US",
},
parcel={"weight": 2.5, "length": 10, "width": 8, "height": 4},
idempotency_key="order-12345",
)
print(label.tracking_number)
print(label.label_url)
Track a Shipment
tracking = client.tracking.get("9400111899223456789012")
print(tracking.status)
print(tracking.estimated_delivery)
for event in tracking.events:
print(f"{event.timestamp}: {event.description}")
Validate an Address
result = client.addresses.validate(
street1="123 Main St",
city="Denver",
state="CO",
zip="80202",
country="US",
)
if result.is_valid:
print("Validated:", result.normalized)
Batch Label Creation
batch = client.labels.create_batch([
{"carrier": "usps", "service": "priority", "from_address": {...}, "to_address": {...}, "parcel": {...}},
{"carrier": "usps", "service": "priority", "from_address": {...}, "to_address": {...}, "parcel": {...}},
])
for result in batch.results:
if result.success:
print(result.label.tracking_number)
else:
print(f"Error: {result.error}")
Async Support
from flexops import AsyncFlexOps
client = AsyncFlexOps(api_key="your-api-key")
rates = await client.rates.get(
from_address={"zip": "80202", "country": "US"},
to_address={"zip": "10001", "country": "US"},
parcel={"weight": 2.5},
)
Webhook Verification
from flexops import verify_webhook_signature
@app.route("/webhooks/flexops", methods=["POST"])
def handle_webhook():
signature = request.headers.get("X-FlexOps-Signature")
is_valid = verify_webhook_signature(
payload=request.data,
signature=signature,
secret="whsec_your_secret",
)
if not is_valid:
return "Invalid signature", 401
event = request.json
if event["type"] == "tracking.delivered":
handle_delivery(event["data"])
return "OK", 200
Error Handling
from flexops.errors import FlexOpsError, RateLimitError
try:
label = client.labels.create(...)
except RateLimitError as e:
print(f"Retry after {e.retry_after} seconds")
except FlexOpsError as e:
print(f"{e.code}: {e.message}")