Click here to Skip to main content
15,868,099 members
Articles / Web Development / ASP.NET
Article

United States Postal Service (USPS) Web Tools Wrapper

Rate me:
Please Sign up or sign in to vote.
4.81/5 (31 votes)
9 May 2008CPOL2 min read 334.9K   6.4K   91   90
United States Postal Service (USPS) Web Tools Wrapper is a .NET library you can use for address validation, retrieving rates and YES even printing labels

Introduction

UPDATE: I am working on a FedEx, UPS and DHL version of this tool. The DHL version should be the next one I release. I will post them here on The Code Project when I am finished, but you can check this Web site until then for updates and to contact me directly.

I was in need of a library that would integrate the USPS Web Tools into my Windows form application. (This library will work in ASP.NET Web applications also.) The USPS Web tools are fairly easy to integrate, but I wanted to create a reusable component that I could use in future projects. Please keep in mind that this is my first CodeProject article, so go easy! :)

USPS Web Tools

Before you can use this library, you will need to get a USPS Web Tools ID. You can do this by filling out this form. Note: If you are using the USPS Test servers, then the data in the examples below are the only data that will work. If you change the address or anything else, you will get an error. Once you have tested your application, you can request your profile to be updated by USPS to access the production environment, where you can use any appropriate data.

Validate an Address

C#
///Create a new instance of the USPS Manager class
///The constructor takes 2 arguments, the first is
///your USPS Web Tools User ID and the second is 
///true if you want to use the USPS Test Servers.
USPSManager m = new USPSManager("YOUR_USER_ID", true);
Address a = new Address();
a.Address2 = "6406 Ivy Lane";
a.City = "Greenbelt";
a.State = "MD";

///By calling ValidateAddress on the USPSManager object,
///you get an Address object that has been validated by the
///USPS servers
Address validatedAddress = m.ValidateAddress(a);

Get Zipcode

C#
///Create a new instance of the USPS Manager class
///The constructor takes 2 arguments, the first is
///your USPS Web Tools User ID and the second is 
///true if you want to use the USPS Test Servers.
USPSManager m = new USPSManager("YOUR_USER_ID", true);
Address a = new Address();
a.Address2 = "6406 Ivy Lane";
a.City = "Greenbelt";
a.State = "MD";
Address addressWithZip = m.GetZipcode(a);
string zip = addressWithZip.Zip;

Get City State from Zip

C#
///Create a new instance of the USPS Manager class
///The constructor takes 2 arguments, the first is
///your USPS Web Tools User ID and the second is 
///true if you want to use the USPS Test Servers.
USPSManager m = new USPSManager("YOUR_USER_ID", true);
Address a = m.GetCityState("90210");
string city = a.City;
string state = a.State;

Track a Package

C#
///Create a new instance of the USPS Manager class
///The constructor takes 2 arguments, the first is
///your USPS Web Tools User ID and the second is 
///true if you want to use the USPS Test Servers.
USPSManager m = new USPSManager("YOUR_USER_ID", true);
TrackingInfo t = m.GetTrackingInfo("EJ958083578US");

Get Shipping Labels

C#
///Create a new instance of the USPS Manager class
///The constructor takes 2 arguments, the first is
///your USPS Web Tools User ID and the second is 
///true if you want to use the USPS Test Servers.
USPSManager m = new USPSManager("YOUR_USER_ID", true);
Package p = new Package();
p.FromAddress.Contact = "John Smith";
p.FromAddress.Address2 = "475 L'Enfant Plaza, SW";
p.FromAddress.City = "Washington";
p.FromAddress.State = "DC";
p.FromAddress.Zip = "20260";
p.ToAddress.Contact = "Tom Customer";
p.ToAddress.Address1 = "STE 201";
p.ToAddress.Address2 = "6060 PRIMACY PKWY";
p.ToAddress.City = "Memphis";
p.ToAddress.State = "TN";
p.WeightInOunces = 2;
p.ServiceType = ServiceType.Priority;
p.SeparateReceiptPage = false;
p.LabelImageType = LabelImageType.TIF;
p.PackageSize = PackageSize.Regular;
p.PackageType = PackageType.Flat_Rate_Box;
p = m.GetDeliveryConfirmationLabel(p);

Note: If you want to test the other label methods, you will need to download the Guides from the USPS Web Tools site and use the sample data they provide.

It appears that USPS may have changed some of the behaviour of their Web services. I will look into this and make changes to the code accordingly.

I have updated the source code to include the XML Parser version of the FromXML method on the Address object. The code was provided by viperguynaz. Thank you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Gologic Tech LLC.
United States United States
I work as an independent software architect and senior developer. I have worked on many large enterprise projects as well as small single user applications.

Comments and Discussions

 
BugIssue with GetCityState(zip) Pin
Member 1373026816-Mar-18 5:21
Member 1373026816-Mar-18 5:21 
QuestionUSPS Integration Pin
Nilam Savani16-Mar-17 19:00
Nilam Savani16-Mar-17 19:00 
GeneralRe: USPS Integration Pin
PIEBALDconsult16-Mar-17 19:02
mvePIEBALDconsult16-Mar-17 19:02 
BugURL Encode needed on USPSManager methods Pin
Aidin Tavakkol3-Nov-15 8:02
Aidin Tavakkol3-Nov-15 8:02 
Questionhow can i print label of USPS shipping ?????give me code or needable TAG to acheive it... Pin
Jacky Ruler9-Aug-15 23:42
Jacky Ruler9-Aug-15 23:42 
AnswerRe: how can i print label of USPS shipping ?????give me code or needable TAG to acheive it... Pin
Bruj1224-Sep-15 9:00
Bruj1224-Sep-15 9:00 
Questionhow can i calculate USPS shipping rate based on zipcode???????..if anyone know it then give me code of it.....our some suggestion Pin
Jacky Ruler9-Aug-15 23:41
Jacky Ruler9-Aug-15 23:41 
QuestionThank you so much bro! Pin
Member 102334081-Apr-15 11:20
Member 102334081-Apr-15 11:20 
QuestionUSPS zip code lookup and city state fuction not working Pin
shiva mca16-Mar-15 21:50
shiva mca16-Mar-15 21:50 
QuestionI get this Error Pin
vishal2cool08925-Feb-15 20:14
vishal2cool08925-Feb-15 20:14 
QuestionFor anyone who wants to do ExpressMailLabelCertify Pin
HackensackNJ24-Dec-14 1:42
HackensackNJ24-Dec-14 1:42 
QuestionUSPS Web Tools Wrapper Pin
Dannoman12345-Nov-14 5:53
Dannoman12345-Nov-14 5:53 
AnswerRe: USPS Web Tools Wrapper Pin
Member 1186380826-Jul-15 1:42
Member 1186380826-Jul-15 1:42 
GeneralRe: USPS Web Tools Wrapper Pin
Dannoman123421-Sep-15 1:20
Dannoman123421-Sep-15 1:20 
QuestionUSPSManager Pin
Member 1109016217-Sep-14 11:39
Member 1109016217-Sep-14 11:39 
QuestionThe bin directory is missing from the download Pin
Member 1109016217-Sep-14 9:49
Member 1109016217-Sep-14 9:49 
QuestionShipping Calculation Pin
yatinlad12-Feb-14 1:12
yatinlad12-Feb-14 1:12 
QuestionTrack and Confirm by Email API??? Pin
Yonah Suds2-Feb-14 2:48
Yonah Suds2-Feb-14 2:48 
AnswerRe: Track and Confirm by Email API??? Pin
sbrazier4-Feb-14 1:34
sbrazier4-Feb-14 1:34 
GeneralMilitary address validation Pin
tbillett12-Jul-12 18:53
tbillett12-Jul-12 18:53 
QuestionShipping label creation Pin
wonder-FOOL22-Aug-11 7:00
wonder-FOOL22-Aug-11 7:00 
GeneralUnable to write the image Pin
asif m@hmood30-Apr-11 2:57
asif m@hmood30-Apr-11 2:57 
GeneralCopyright Question Pin
thumb221-Mar-11 5:47
thumb221-Mar-11 5:47 
GeneralAPI Testing Pin
jordanbaucke24-Aug-10 11:21
jordanbaucke24-Aug-10 11:21 
GeneralFirmName Setter Pin
Member 403999323-Apr-09 10:00
Member 403999323-Apr-09 10:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.