Lob Python Client Library (v4.x)
The `lob` Python client library provides bindings for the Lob.com API, enabling programmatic access to services like mail, print, and address verification. This registry entry specifically covers the 4.x series of the library. While still maintained for critical fixes, new major versions (5.x and above) are released under a separate PyPI package, `lob-python`.
Warnings
- breaking Version 5.x of the Lob Python client is distributed under a *separate PyPI package* named `lob-python`. Users upgrading from `lob` (v4.x) must install `lob-python` and update import paths and API usage patterns.
- gotcha Lob API uses HTTP Basic authentication where the API key is provided as the username and the password field is left blank. Ensure you use the correct API key type (e.g., `test_...` for sandbox, `live_...` for production) as they have different capabilities and billing implications. Test API keys will not send real mail.
- gotcha The Lob API has rate limits, typically 150 requests per 5 seconds per endpoint. Exceeding this limit will result in an `HTTP 429 Too Many Requests` response.
Install
-
pip install lob
Imports
- lob
import lob
Quickstart
import os
import lob
# Get your API key from environment variables
lob.api_key = os.environ.get('LOB_API_KEY', '')
# For demonstration, ensure a valid API key is present
if not lob.api_key:
print("Error: LOB_API_KEY environment variable not set.")
print("Please set it with your Lob Test API key (e.g., test_****************).")
exit(1)
try:
# Create a simple address
address = lob.Address.create(
name='Joe Smith',
address_line1='123 Main St',
address_city='San Francisco',
address_state='CA',
address_zip='94107',
address_country='US'
)
print(f"Successfully created Address ID: {address.id}")
print(f"Address Line 1: {address.address_line1}")
print(f"Address City: {address.address_city}")
print(f"Address State: {address.address_state}")
except lob.LobError as e:
print(f"Lob API Error: {e.message}")
except Exception as e:
print(f"An unexpected error occurred: {e}")