If you are a company or an individual who frequently shipping stuffs with United States Postal Service (USPS) then knowing how to create a program to automatically tracking packages for you might help ease some of the pain when you have to manually typing the tracking number on USPS website to get the shipping status.
Using USPS Web Tools API, yon can access their Price Calculator, Address Verification, Package Tracking, Shipping Label Printing, and Delivery Dates Lookup services. However, in this tutorial we will focus on how to use the Package Tracking service and how to build a simple Python program to track USPS package shipping status.
Step 1. Sign Up for An USPS Account
The first thing you will need is to sign up for an USPS account at their Web Tools API Portal website https://www.usps.com/business/web-tools-apis/welcome.htm.
Once you finished register, you should get an e-mail with username and password. Make sure you keep this information somewhere secure. If you need to access additional services like print a shipping label, schedule package a pick up, then you will need to e-mail firstname.lastname@example.org for additional API access.
Step 2. Create a Virtual Environment (Optional)
For me, whenever I am creating a new program, I like to create a separate environment just for that project. If you are using Python 3.5 or later version, now you can use the venv standard module (also recommended method) to create a virtual environment.
To create a virtual environment with Python version 3.5 or later, open your command prompt, change your directory to the folder where you want to install the virtual environment and type the following command:
Python -m venv USPSApi
Step 3. Install the USPS API library
If you have installed a virtual environment for this tutorial, please activate your environment first, otherwise, we can install the USPS API library by typing
pip install usps-api
Step 4. Create Package Tracking Program
Open your Python editor, create a blank script, and enter the following code below (for more detail, please watch my YouTube tutorial video).