EasyPost Shipping API Client Library for Python
Actively maintained. Last release 3 weeks ago, commits in 6 of the last 26 weeks. A safe choice.
pip install easypostEasyPost Python Client Library
EasyPost, the simple shipping solution. You can sign up for an account at https://easypost.com.
Install
The library is tested against Python3 and should be compatible with PyPy3.
pip install easypost
# Import the EasyPost library:
import easypost
Usage
A simple create & buy shipment example:
import os
import easypost
client = easypost.EasyPostClient(os.getenv('EASYPOST_API_KEY'))
shipment = client.shipment.create(
from_address = {
"name": "EasyPost",
"street1": "118 2nd Street",
"street2": "4th Floor",
"city": "San Francisco",
"state": "CA",
"zip": "94105",
"country": "US",
"phone": "415-456-7890",
},
to_address = {
"name": "Dr. Steve Brule",
"street1": "179 N Harbor Dr",
"city": "Redondo Beach",
"state": "CA",
"zip": "90277",
"country": "US",
"phone": "310-808-5243",
},
parcel = {
"length": 10.2,
"width": 7.8,
"height": 4.3,
"weight": 21.2,
},
)
bought_shipment = client.shipment.buy(shipment.id, rate=shipment.lowest_rate())
print(bought_shipment)
HTTP Hooks
Users can subscribe to HTTP requests and responses via the RequestHook and ResponseHook objects. To do so, pass a function to the subscribe_to_request_hook or subscribe_to_response_hook methods of an EasyPostClient object:
def custom_function(**kwargs):
"""Pass your code here, data about the request/response is contained within `kwargs`."""
print(f"Received a request with the status code of: {kwargs.get('http_status')}")
client = easypost.EasyPostClient(os.getenv('EASYPOST_API_KEY'))
client.subscribe_to_response_hook(custom_function)
# Make your API calls here, your custom_function will trigger once a response is received