Click here to Skip to main content
15,867,835 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

 
GeneralRe: To get a Zip Code response you need to make this change Pin
Winterwheat23-Mar-10 6:04
Winterwheat23-Mar-10 6:04 
Generaldetails of USPS xml Pin
kcirnitram16-Oct-07 7:26
kcirnitram16-Oct-07 7:26 
GeneralAPI Authorization failure Pin
shruti basnet18-Aug-07 21:44
shruti basnet18-Aug-07 21:44 
GeneralRe: API Authorization failure Pin
zhedex17-Jan-08 7:14
zhedex17-Jan-08 7:14 
GeneralRe: API Authorization failure Pin
rtsang10-Feb-08 10:57
rtsang10-Feb-08 10:57 
AnswerRe: API Authorization failure Pin
zhedex12-Feb-08 12:30
zhedex12-Feb-08 12:30 
GeneralRe: API Authorization failure Pin
Dr_Chanesaw4-Apr-10 11:55
Dr_Chanesaw4-Apr-10 11:55 
GeneralIs this possible Pin
binaryDigit@@27-Jul-07 14:12
binaryDigit@@27-Jul-07 14:12 
Hi,
Is it possible to get the Delivery Point of an address? I 've been banging my head on this. I can't seem to find the formuls in the CASS doc.

Thanks
GeneralCode Errors Pin
Bob Melcos20-Jul-07 6:42
Bob Melcos20-Jul-07 6:42 
GeneralThank you.. Pin
stephenpatten16-Jun-07 8:05
stephenpatten16-Jun-07 8:05 
GeneralZip Code in Address Object Pin
MindCore13-Jun-07 4:26
MindCore13-Jun-07 4:26 
GeneralGetCityState & GetZipcode Pin
viperguynaz18-Apr-07 10:43
viperguynaz18-Apr-07 10:43 
QuestionUSPS Approval For Windows Application Pin
MidnightRider31116-Apr-07 12:12
MidnightRider31116-Apr-07 12:12 
AnswerRe: USPS Approval For Windows Application Pin
johnnycantcode16-Apr-07 12:31
johnnycantcode16-Apr-07 12:31 
QuestionRe: USPS Approval For Windows Application Pin
MidnightRider31116-Apr-07 16:53
MidnightRider31116-Apr-07 16:53 
AnswerRe: USPS Approval For Windows Application Pin
johnnycantcode17-Apr-07 1:41
johnnycantcode17-Apr-07 1:41 
AnswerRe: USPS Approval For Windows Application Pin
MidnightRider31117-Apr-07 2:55
MidnightRider31117-Apr-07 2:55 
GeneralRe: USPS Approval For Windows Application Pin
johnnycantcode17-Apr-07 4:05
johnnycantcode17-Apr-07 4:05 
GeneralRe: USPS Approval For Windows Application Pin
MidnightRider31117-Apr-07 4:42
MidnightRider31117-Apr-07 4:42 
GeneralRe: USPS Approval For Windows Application Pin
Bob Melcos20-Jul-07 6:34
Bob Melcos20-Jul-07 6:34 
QuestionRe: USPS Approval For Windows Application Pin
sklett16-Apr-07 18:33
sklett16-Apr-07 18:33 
AnswerRe: USPS Approval For Windows Application Pin
MidnightRider31116-Apr-07 18:56
MidnightRider31116-Apr-07 18:56 
GeneralRe: USPS Approval For Windows Application Pin
steveklett17-Apr-07 9:29
steveklett17-Apr-07 9:29 
GeneralRe: USPS Approval For Windows Application Pin
MidnightRider31117-Apr-07 9:44
MidnightRider31117-Apr-07 9:44 
GeneralRe: USPS Approval For Windows Application Pin
sklett18-Apr-07 14:50
sklett18-Apr-07 14:50 

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.