Click here to Skip to main content
15,867,453 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.8K   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

 
GeneralFedEx, UPS, & DHL Tracking Pin
MadMan-2972915-Apr-09 17:22
MadMan-2972915-Apr-09 17:22 
GeneralRe: FedEx, UPS, & DHL Tracking Pin
mlumsden2-Aug-09 20:32
mlumsden2-Aug-09 20:32 
GeneralHelp displaying test result Pin
Gary In SD27-Mar-09 5:29
Gary In SD27-Mar-09 5:29 
GeneralRe: Help displaying test result Pin
Jason Christian26-May-10 19:37
Jason Christian26-May-10 19:37 
GeneralRe: Help displaying test result [modified] Pin
phishbowler5-Nov-10 9:59
phishbowler5-Nov-10 9:59 
GeneralCertified mail Pin
chiajhu25-Mar-09 17:18
chiajhu25-Mar-09 17:18 
GeneralA80040b1a API Authorization failure Pin
rishi_narula12-Feb-09 22:13
rishi_narula12-Feb-09 22:13 
AnswerRe: A80040b1a API Authorization failure Pin
tim119821-Mar-09 1:44
tim119821-Mar-09 1:44 
Hi: I had the same problem, and fixed it by going to the production server. The test server is not set up to work with deliver confirmation. Also, you need to use the secure servers (that is in the email they send you) for the shipping label stuff. The documentation does not tell you this. You have to call them (800-344-7779) and be promoted to the production server:

'For Secure Servers (shipping labels, RMA), use these
Private Const ProductionUrl As String = "https://secure.shippingapis.com/ShippingAPI.dll"
Private Const TestingUrl As String = "https://secure.shippingapis.com/ShippingAPITest.dll"

'For Standard servers (address check, zip code check), use these
Private Const ProductionUrl As String = "http://production.shippingapis.com/ShippingAPI.dll"
Private Const TestingUrl As String = "http://testing.shippingapis.com/ShippingAPITest.dll"

Also, remember to use the: API=DelivConfirmCertifyV3 for testing label while on the production server.

Using these server settings, I'm able to retrieve the xml of the delivery confirmation number as well as the shipping label.

Good luck, tim1198
GeneralRe: A80040b1a API Authorization failure Pin
JMAWebTechnologies25-Feb-11 6:23
JMAWebTechnologies25-Feb-11 6:23 
GeneralBad GateWay Error [modified] Pin
anildbest11-Feb-09 20:24
anildbest11-Feb-09 20:24 
GeneralPlease enter a valid ZIP Code for the sender Pin
Helixpoint13-Jan-09 5:03
Helixpoint13-Jan-09 5:03 
GeneralRe: Please enter a valid ZIP Code for the sender Pin
tim119821-Mar-09 1:48
tim119821-Mar-09 1:48 
GeneralNeed Help Tracking Packages [modified] Pin
MadMan-297292-Dec-08 5:55
MadMan-297292-Dec-08 5:55 
GeneralRe: Need Help Tracking Packages Pin
Helixpoint13-Jan-09 6:08
Helixpoint13-Jan-09 6:08 
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 
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 

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.