ecommercedeveloper

Developer Guide: Integrating Python Integration into Your E-commerce Stack

Build a more robust shipping workflow. A technical deep dive into implementing Python Integration for high-performance logistics.

July 23, 20233 min read
Developer Guide: Integrating Python Integration into Your E-commerce Stack

Python Shipping Integration: From requests to Production

Python is the second most popular language for shipping integrations (after Node.js). Whether you're building a Django e-commerce backend or a Flask microservice for label generation, here's the practical guide.

Library Choices

TaskLibraryWhy
HTTP clienthttpxAsync support, connection pooling, HTTP/2
Data validationPydanticType-safe models, JSON schema generation
Background jobsCelery + RedisProven at scale for label generation queues
PDF handlingreportlab or pikepdfLabel PDF manipulation and merging
Testingpytest + responsesMock HTTP responses for carrier APIs

Project Structure

Organize your shipping code as a Python package with clear boundaries:

  • shipping/carriers/ — one module per carrier (usps.py, fedex.py, ups.py)
  • shipping/models/ — Pydantic models for shipments, rates, labels
  • shipping/services/ — business logic (rate_shopping.py, label_service.py)
  • shipping/adapters/ — e-commerce platform adapters (shopify.py, amazon.py)

Pydantic Models for Shipping

Define your data structures with Pydantic for automatic validation:

Create an Address model with fields like name (str), street1 (str), street2 (optional str), city (str), state (str, 2-char max), zip_code (str matching 5 or 9 digit pattern), and country (str defaulting to "US").

Create a Shipment model referencing the Address model with ship_from, ship_to, and a list of Package objects (each with weight_oz, length, width, height).

Pydantic catches data issues at the boundary — before they cause cryptic carrier API errors.

Ship smarter, not harder

Compare USPS, UPS & FedEx rates in seconds — no signup required.

Get Free Rates →

Async Carrier Calls with httpx

For rate shopping across multiple carriers, async is essential. Use httpx.AsyncClient to make parallel requests to USPS, FedEx, and UPS simultaneously with asyncio.gather. Flatten the results and sort by price.

This runs all three carrier API calls concurrently instead of sequentially — cutting response time from 3-5 seconds to 1-2 seconds.

Celery for Background Label Generation

Label generation can take 2-10 seconds per carrier API call. Don't make users wait. Push to a Celery queue and notify them when the label is ready.

Define a Celery task that calls the carrier API, stores the label PDF in S3/storage, and sends a WebSocket notification to the user. Use autoretry_for with ConnectionError and set max_retries to 3 with exponential backoff.

Error Handling

Carrier APIs return errors in wildly different formats. Create a unified exception hierarchy:

  • ShippingError (base)
  • AddressError — validation failed
  • RateError — no rates available
  • LabelError — label generation failed
  • AuthError — credentials expired
  • CarrierTimeoutError — carrier API didn't respond
Map each carrier's specific error codes to these categories. Your application code catches ShippingError subtypes and doesn't need to know which carrier was involved.

Testing with Recorded Responses

Use the responses library (or respx for async) to record and replay carrier API responses. Record once with real API calls, save the JSON fixtures, and replay in CI. This prevents flaky tests that depend on carrier API availability.

Start building with Atoship's Python SDK →

Share this article:

Compare USPS, UPS & FedEx rates instantly with atoship — no monthly fees.

Try Free

Save up to 89% on shipping labels

Compare USPS, UPS, and FedEx rates side by side. Get commercial pricing with no monthly fees, no contracts, and no markup.

Free forever No credit card 2-minute setup

Recommended for you

View all articles
FedEx Shipping API and Developer Tools
13 min read

FedEx Shipping API and Developer Tools

A developer-focused guide to the FedEx RESTful API. Covers authentication, rate quotes, label generation, tracking, address validation, and best practices for integrating FedEx into your application.

Facebook Marketplace Shipping: The Hidden Costs
10 min read

Facebook Marketplace Shipping: The Hidden Costs

Facebook Marketplace shipping looks cheap on the surface. But between prepaid label limitations, buyer disputes, and unclear fee structures, the real cost is higher than you think.

UPS Shipping API Integration: Developer Guide
12 min read

UPS Shipping API Integration: Developer Guide

A comprehensive developer guide to integrating with the UPS Shipping API — authentication, rate shopping, label generation, tracking, address validation, and production deployment best practices.

Print-on-Demand Shipping: Printful vs Printify vs Gooten
10 min read

Print-on-Demand Shipping: Printful vs Printify vs Gooten

A detailed shipping comparison of the three biggest print-on-demand platforms, covering costs, speed, quality, global fulfillment, and which works best for different sellers.

UPS QR Code Shipping: Print Labels at UPS Store Without a Printer
6 min read

UPS QR Code Shipping: Print Labels at UPS Store Without a Printer

Ship UPS packages without a printer using QR codes. Print labels at any UPS Store or Access Point location for free.

Best Thermal Label Printers for Shopify Stores (2026 Comparison)
6 min read

Best Thermal Label Printers for Shopify Stores (2026 Comparison)

Stop wasting money on ink. Compare the best thermal label printers for Shopify sellers - from budget options to professional-grade machines.

Shipping Software Comparison: Atoship vs ShipStation vs Pirate Ship
8 min read

Shipping Software Comparison: Atoship vs ShipStation vs Pirate Ship

An honest comparison of three popular shipping platforms — what each does well, where they fall short, and which one fits your business.

DHL eCommerce vs DHL Express: International Shipping Service Comparison
6 min read

DHL eCommerce vs DHL Express: International Shipping Service Comparison

Compare DHL eCommerce and DHL Express for international shipments. Cost differences, transit times, and best use cases for cross-border e-commerce.