Click here to Skip to main content
15,887,027 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 338.5K   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

 
QuestionHow to show label Pin
sd218728-Oct-08 20:48
sd218728-Oct-08 20:48 
AnswerRe: How to show label Pin
fancycat24-Oct-10 11:53
fancycat24-Oct-10 11:53 
QuestionHOW TO SHOW PDF FILE FROM UTF8 BYTE ARRAY. Pin
sd218724-Oct-08 23:40
sd218724-Oct-08 23:40 
AnswerRe: HOW TO SHOW PDF FILE FROM UTF8 BYTE ARRAY. Pin
ScottieW8-Jan-09 16:26
ScottieW8-Jan-09 16:26 
GeneralRe: HOW TO SHOW PDF FILE FROM UTF8 BYTE ARRAY. Pin
fancycat24-Oct-10 11:51
fancycat24-Oct-10 11:51 
GeneralNice Pin
Asif Ashraf10-Aug-08 22:49
professionalAsif Ashraf10-Aug-08 22:49 
GeneralRate Stuff Pin
bugnuker30-Jun-08 7:46
bugnuker30-Jun-08 7:46 
GeneralRe: Rate Stuff Pin
bossmojoman27-Aug-09 11:53
bossmojoman27-Aug-09 11:53 
Hi, I am currently working with this wrapper code. Do you have the Test data for rates? I cannot find it anywhere

Thanks in advance
Glen Colby
Sr. Developer
Neoreef LLC
www.Neoreef.com


bugnuker wrote:
Hey, Thanks for posting this code.

I did not see an example for rates here, so I started to play with it.
First off, you MUST use specific values for getting rate on the test server. You can find these values online. You will always get back invalid zip code if you dont use the correct values. Second, the GetRate method allows you to send "ALL" so you can get many rates. This is how most people would use the system I would gather, as you want a list of rates before you select a rate, right?

This code was not made for "ALL" rate. I added the "ALL". Once You do this, you must send Machinable value. This is not built into the system at all as far as I can see.

If I an missing this, can you point it out?
Other than that, I will modify to contain these values. If someone needs the test GetRate values, lemme know
-Shane

GeneralRe: Rate Stuff Pin
thelivingbrian10-May-11 6:46
thelivingbrian10-May-11 6:46 
GeneralRe: Rate Stuff Pin
DresCAn15-Sep-11 7:46
DresCAn15-Sep-11 7:46 
GeneralInternational rate Pin
tuhin2425-May-08 18:52
tuhin2425-May-08 18:52 
GeneralRe: International rate Pin
thelivingbrian10-May-11 13:07
thelivingbrian10-May-11 13:07 
GeneralTo get a Zip Code response you need to make this change Pin
defwebserver29-Nov-07 9:52
defwebserver29-Nov-07 9:52 
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 
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 

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.