Click here to Skip to main content
11,645,499 members (79,543 online)
Click here to Skip to main content

United States Postal Service (USPS) Web Tools Wrapper

, 9 May 2008 CPOL 177K 3.6K 86
Rate this:
Please Sign up or sign in to vote.
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! Smile | :)

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

///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

///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

///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

///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

///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)

Share

About the Author

johnnycantcode
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.

You may also be interested in...

Comments and Discussions

 
QuestionThank you so much bro! Pin
Member 102334081-Apr-15 11:20
memberMember 102334081-Apr-15 11:20 
QuestionUSPS zip code lookup and city state fuction not working Pin
shiva mca16-Mar-15 21:50
membershiva mca16-Mar-15 21:50 
QuestionI get this Error Pin
vishal2cool08925-Feb-15 20:14
membervishal2cool08925-Feb-15 20:14 
QuestionFor anyone who wants to do ExpressMailLabelCertify Pin
CloudWebi24-Dec-14 1:42
memberCloudWebi24-Dec-14 1:42 
QuestionUSPS Web Tools Wrapper Pin
Dannoman12345-Nov-14 5:53
memberDannoman12345-Nov-14 5:53 
AnswerRe: USPS Web Tools Wrapper Pin
Member 1186380826-Jul-15 1:42
memberMember 1186380826-Jul-15 1:42 
QuestionUSPSManager Pin
Member 1109016217-Sep-14 11:39
memberMember 1109016217-Sep-14 11:39 
QuestionThe bin directory is missing from the download Pin
Member 1109016217-Sep-14 9:49
memberMember 1109016217-Sep-14 9:49 
QuestionShipping Calculation Pin
yatinlad12-Feb-14 1:12
memberyatinlad12-Feb-14 1:12 
QuestionTrack and Confirm by Email API??? Pin
Member 81644882-Feb-14 2:48
memberMember 81644882-Feb-14 2:48 
AnswerRe: Track and Confirm by Email API??? Pin
sbrazier4-Feb-14 1:34
membersbrazier4-Feb-14 1:34 
GeneralMilitary address validation Pin
tbillett12-Jul-12 18:53
membertbillett12-Jul-12 18:53 
QuestionShipping label creation Pin
wonder-FOOL22-Aug-11 7:00
memberwonder-FOOL22-Aug-11 7:00 
GeneralUnable to write the image Pin
asif m@hmood30-Apr-11 2:57
memberasif m@hmood30-Apr-11 2:57 
GeneralCopyright Question Pin
thumb221-Mar-11 5:47
memberthumb221-Mar-11 5:47 
GeneralAPI Testing Pin
jordanbaucke24-Aug-10 11:21
memberjordanbaucke24-Aug-10 11:21 
GeneralFirmName Setter Pin
Member 403999323-Apr-09 10:00
memberMember 403999323-Apr-09 10:00 
GeneralFedEx, UPS, & DHL Tracking Pin
cworkman2972915-Apr-09 17:22
membercworkman2972915-Apr-09 17:22 
GeneralRe: FedEx, UPS, & DHL Tracking Pin
mlumsden2-Aug-09 20:32
membermlumsden2-Aug-09 20:32 
GeneralHelp displaying test result Pin
Gary In SD27-Mar-09 5:29
memberGary In SD27-Mar-09 5:29 
GeneralRe: Help displaying test result Pin
Jason Christian26-May-10 19:37
memberJason Christian26-May-10 19:37 
GeneralRe: Help displaying test result [modified] Pin
phishbowler5-Nov-10 9:59
memberphishbowler5-Nov-10 9:59 
GeneralCertified mail Pin
chiajhu25-Mar-09 17:18
memberchiajhu25-Mar-09 17:18 
GeneralA80040b1a API Authorization failure Pin
rishi_narula12-Feb-09 22:13
memberrishi_narula12-Feb-09 22:13 
AnswerRe: A80040b1a API Authorization failure Pin
tim119821-Mar-09 1:44
membertim119821-Mar-09 1:44 
GeneralRe: A80040b1a API Authorization failure Pin
JMAWebTechnologies25-Feb-11 6:23
memberJMAWebTechnologies25-Feb-11 6:23 
Hi,

Please get Address Vertification (Address Verify API) permissions from the USPS. I called the first time and I didn't get it. I asked for it the second time and it was activated. Here is the phone #:

Telephone: 1-800-344-7779 (7:00AM to 11:00PM ET)

Press 3
GeneralBad GateWay Error [modified] Pin
anildbest11-Feb-09 20:24
memberanildbest11-Feb-09 20:24 
GeneralPlease enter a valid ZIP Code for the sender Pin
Helixpoint13-Jan-09 5:03
memberHelixpoint13-Jan-09 5:03 
GeneralRe: Please enter a valid ZIP Code for the sender Pin
tim119821-Mar-09 1:48
membertim119821-Mar-09 1:48 
GeneralNeed Help Tracking Packages [modified] Pin
cworkman297292-Dec-08 5:55
membercworkman297292-Dec-08 5:55 
GeneralRe: Need Help Tracking Packages Pin
Helixpoint13-Jan-09 6:08
memberHelixpoint13-Jan-09 6:08 
QuestionHow to show label Pin
sd218728-Oct-08 20:48
membersd218728-Oct-08 20:48 
AnswerRe: How to show label Pin
Luke Schoen24-Oct-10 11:53
memberLuke Schoen24-Oct-10 11:53 
QuestionHOW TO SHOW PDF FILE FROM UTF8 BYTE ARRAY. Pin
sd218724-Oct-08 23:40
membersd218724-Oct-08 23:40 
AnswerRe: HOW TO SHOW PDF FILE FROM UTF8 BYTE ARRAY. Pin
ScottieW8-Jan-09 16:26
memberScottieW8-Jan-09 16:26 
GeneralRe: HOW TO SHOW PDF FILE FROM UTF8 BYTE ARRAY. Pin
Luke Schoen24-Oct-10 11:51
memberLuke Schoen24-Oct-10 11:51 
GeneralRate Stuff Pin
bugnuker30-Jun-08 7:46
memberbugnuker30-Jun-08 7:46 
GeneralRe: Rate Stuff Pin
bossmojoman27-Aug-09 11:53
memberbossmojoman27-Aug-09 11:53 
GeneralRe: Rate Stuff Pin
thelivingbrian10-May-11 6:46
memberthelivingbrian10-May-11 6:46 
GeneralRe: Rate Stuff Pin
Member 823803315-Sep-11 7:46
memberMember 823803315-Sep-11 7:46 
GeneralInternational rate Pin
tuhin2425-May-08 18:52
membertuhin2425-May-08 18:52 
GeneralRe: International rate Pin
thelivingbrian10-May-11 13:07
memberthelivingbrian10-May-11 13:07 
GeneralTo get a Zip Code response you need to make this change Pin
defwebserver29-Nov-07 9:52
memberdefwebserver29-Nov-07 9:52 
GeneralRe: To get a Zip Code response you need to make this change Pin
Winterwheat23-Mar-10 6:04
memberWinterwheat23-Mar-10 6:04 
Generaldetails of USPS xml Pin
kcirnitram16-Oct-07 7:26
memberkcirnitram16-Oct-07 7:26 
GeneralAPI Authorization failure Pin
shruti basnet18-Aug-07 21:44
membershruti basnet18-Aug-07 21:44 
GeneralRe: API Authorization failure Pin
zhedex17-Jan-08 7:14
memberzhedex17-Jan-08 7:14 
GeneralRe: API Authorization failure Pin
rtsang10-Feb-08 10:57
memberrtsang10-Feb-08 10:57 
AnswerRe: API Authorization failure Pin
zhedex12-Feb-08 12:30
memberzhedex12-Feb-08 12:30 
GeneralRe: API Authorization failure Pin
Dr_Chanesaw4-Apr-10 11:55
memberDr_Chanesaw4-Apr-10 11:55 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 9 May 2008
Article Copyright 2006 by johnnycantcode
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid