Click here to Skip to main content
15,887,361 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.7K   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: 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 
The issue is on their side. I sent an email explaining my situation and they replied. I'll post my email and their reply here:

My Email:

Hello. I've just gotten off the phone with the ICCC and wanted to switch to the production server, but I was told that I needed two successful tries, which I cannot get because I keep getting this
error. I've already read the technical guide, and I was also told to send my code to the mail department, and patiently wait, which is what I am doing right now. I've already sent an email but didn't send the code. I am developing using Microsoft Visual C# .NET Framework 1.1. I created a new web application, and added a .dll to the references (MSXML2.dll which can be googled and downloaded easily), which I use for the XML. Here is my code:

private void Page_Load(object sender, System.EventArgs e)
{
string API = "API=DeliveryConfirmationV3&XML=";
string USPSUrl = "https://secure.shippingapis.com/ShippingAPITest.dll?";
string strXML = API + "<DeliveryConfirmationV3.0Request
USERID=\"XXXXXX\">\n\n<Option>1</Option>\n\n<ImageParameters
/>\n\n<FromName>John Smith</FromName>\n\n<FromFirm>U.S. Postal
Headquarters</FromFirm>\n\n<FromAddress1 />\n\n<FromAddress2>475
L'Enfant Plaza,
SW</FromAddress2>\n\n<FromCity>Washington</FromCity>\n\n<FromState>DC</FromState>\n\n<FromZip5>20260</FromZip5>\n\n<FromZip4>0004</FromZip4>\n\n<ToName>Joe
Customer</ToName>\n\n<ToFirm>U.S. Postal Service
NCSC</ToFirm>\n\n<ToAddress1>STE 201</ToAddress1>\n\n<ToAddress2>6060
PRIMACY PKWY</ToAddress2>\n\n<ToCity>MEMPHIS</ToCity>\n\n<ToState>TN</ToState>\n\n<ToZip5>38119</ToZip5>\n\n<ToZip4>5718</ToZip4>\n\n<WeightInOunces>2</WeightInOunces>\n\n<ServiceType>Priority</ServiceType>\n\n<POZipCode>20260</POZipCode>\n\n<ImageType>TIF</ImageType>\n\n<LabelDate>07/08/2004</LabelDate>\n\n<CustomerRefNo>A45-3928</CustomerRefNo>\n\n<AddressServiceRequested>TRUE</AddressServiceRequested>\n\n</DeliveryConfirmationV3.0Request>";

MSXML2.XMLHTTPClass XMLhttp = new MSXML2.XMLHTTPClass();
XMLhttp.open("POST", USPSUrl, false, null, null);
XMLhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
XMLhttp.send(strXML);

string responseMsg = XMLhttp.responseText;
string HTTPStatusText = XMLhttp.statusText;
int HTTPStatusCode = XMLhttp.status;

Response.Write(responseMsg);

}

Any help is appreciated. I hope this issue can be solved. Thank you.

--

ICCC's reply:

Thank you for contacting the USPS Internet Customer Care Center in regards to Web Tools ID XXXXXXX.

I apologize, it is not currently possible to generate a label from our test servers. Your profile has been updated to allow you access to the Production Server, and you can test your code using sample labels.

1. The Production Server URL is:

http://production.shippingapis.com.

2. Labels require a secure connection. Please use our secure server:

https://secure.shippingapis.com.

3. There is a line of code that refers to "shippingapitest.dll". You'll need to remove the word "test".

Please refer to the "Run Sample Requests" section of our technical guides for more information on generating sample labels:

http://www.usps.com/webtools/technical.htm

Once you have finished testing your code and want to be given access to generate live labels, we need to know whether you will be customizing (e.g. adding your logo, changing the layout, etc...) the labels in any way.

If you are using third party software and need assistance, please contact the vendor of the software. They should be able to assist you in obtaining live information using our APIs.

If you have any additional questions or concerns please contact us again.

--

I hope this helps
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 
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 

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.