How to validate the given address using USPS?

The United States Postal Service (USPS) does offer this service via their Address Information APIs:

  • USPS: Ecommerce APIs

Here's a Code Project article/library how you could use this services in .NET:

  • United States Postal Service (USPS) Web Tools Wrapper

Note

  • Before you can use this library, you will need to get a USPS Web Tools ID by filling out this form.

  • To request the Address Informational APIs (Verify etc.) you need additional permissions. Also fill out this form to apply for these permissions.


From here

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

NOTE: For some reason, you need to put the actual Address as Address2. If you attempt to put Address1 as "6406 Ivy Lane" it will fail. Address1 is apparently for apartment or suite number. Courtesy Simon Weaver's comment below.


If I may chime in here -- I used to work in the address validation industry for SmartyStreets, which is a CASS-Certified vendor of these services.

Note first that, while the USPS is the authority on address data, their forte is NOT maintaining an API and providing support. Also, be sure to note the agreement which you signed:

  • User agrees to use the USPS Web site, APIs and USPS data to facilitate USPS shipping transactions only.

So unless you're mailing or shipping with the USPS by the use of their API, it's not agreeable to use the API at all. This among the other problems you're having is grounds to look for a better solution -- if I was you.

Anyway, there are actually quite a few out there. I'll let you do your own research, but of course I would suggest one that I've worked on called LiveAddress. It's free, and returns more data and is more reliable than the USPS' API.

Update: Here's some C# code examples on GitHub that will probably be useful.

Tags:

C#