Click here to Skip to main content
6,822,613 members and growing! (23,054 online)
Email Password   helpLost your password?
Languages » C# » Utilities     Intermediate License: The Code Project Open License (CPOL)

United States Postal Service (USPS) Web Tools Wrapper

By johnnycantcode

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
C#, Windows, .NET, ASP.NET, Visual-Studio, Dev
Posted:19 Oct 2006
Updated:9 May 2008
Views:68,792
Bookmarked:72 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 5.66 Rating: 4.81 out of 5

1

2

3
2 votes, 13.3%
4
13 votes, 86.7%
5

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

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

About the Author

johnnycantcode


Member
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.
Occupation: Software Developer (Senior)
Company: Gologic Tech LLC.
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 56 (Total in Forum: 56) (Refresh)FirstPrevNext
GeneralFirmName Setter PinmemberMember 403999311:00 23 Apr '09  
GeneralFedEx, UPS, & DHL Tracking Pinmembercworkman2972918:22 15 Apr '09  
GeneralRe: FedEx, UPS, & DHL Tracking Pinmembermlumsden21:32 2 Aug '09  
GeneralHelp displaying test result PinmemberGary In SD6:29 27 Mar '09  
GeneralCertified mail Pinmemberchiajhu18:18 25 Mar '09  
GeneralA80040b1a API Authorization failure Pinmemberrishi_narula23:13 12 Feb '09  
AnswerRe: A80040b1a API Authorization failure Pinmembertim11982:44 21 Mar '09  
GeneralBad GateWay Error [modified] Pinmemberanildbest21:24 11 Feb '09  
GeneralPlease enter a valid ZIP Code for the sender PinmemberHelixpoint6:03 13 Jan '09  
GeneralRe: Please enter a valid ZIP Code for the sender Pinmembertim11982:48 21 Mar '09  
GeneralNeed Help Tracking Packages [modified] Pinmembercworkman297296:55 2 Dec '08  
GeneralRe: Need Help Tracking Packages PinmemberHelixpoint7:08 13 Jan '09  
GeneralHow to show label Pinmembersd218721:48 28 Oct '08  
GeneralHOW TO SHOW PDF FILE FROM UTF8 BYTE ARRAY. Pinmembersd21870:40 25 Oct '08  
AnswerRe: HOW TO SHOW PDF FILE FROM UTF8 BYTE ARRAY. PinmemberScottieW17:26 8 Jan '09  
GeneralNice Pinmemberhackrogenius23:49 10 Aug '08  
GeneralRate Stuff Pinmemberbugnuker8:46 30 Jun '08  
GeneralRe: Rate Stuff Pinmemberbossmojoman12:53 27 Aug '09  
GeneralInternational rate Pinmembertuhin2419:52 25 May '08  
GeneralTo get a Zip Code response you need to make this change Pinmemberdefwebserver10:52 29 Nov '07  
Generaldetails of USPS xml Pinmemberkcirnitram8:26 16 Oct '07  
GeneralAPI Authorization failure Pinmembershruti basnet22:44 18 Aug '07  
GeneralRe: API Authorization failure Pinmemberzhedex8:14 17 Jan '08  
GeneralRe: API Authorization failure Pinmemberrtsang11:57 10 Feb '08  
AnswerRe: API Authorization failure Pinmemberzhedex13:30 12 Feb '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 9 May 2008
Editor: Deeksha Shenoy
Copyright 2006 by johnnycantcode
Everything else Copyright © CodeProject, 1999-2010
Web22 | Advertise on the Code Project