I want to be able to give a best guess for what city and state a zip code is in via a web application. Is there some web service I can use to figure this out?
Hate to do this to you, but look here...
I believe the USPS has an API for some queries, you should look into that.
However, here are a few from the above resource:
- http://www.cedar.buffalo.edu/AdServ/zip-search.html - Includes the database so you can host one yourself
- http://www.zipinfo.com/search/zipcode.htm - commercial site, but they have a simple search
- http://zip4.usps.com/zip4/citytown_zip.jsp - USPS interface, probably has an API
- http://www.webservicex.net/uszip.asmx?op=GetInfoByZIP - SOAP interface
-Adam
I found a couple of ways to do this with web based APIs. I think the US Postal Service would be the most accurate, since Zip codes are their thing, but Ziptastic looks much easier.
Using the US Postal Service HTTP/XML API
According to this page on the US Postal Service website which documents their XML based web API, specifically Section 4.0 (page 22) of this PDF document, they have a URL where you can send an XML request containing a 5 digit Zip Code and they will respond with an XML document containing the corresponding City and State.
According to their documentation, here's what you would send:
http://SERVERNAME/ShippingAPITest.dll?API=CityStateLookup&XML=<CityStateLookupRequest%20USERID="xxxxxxx"><ZipCode ID= "0"><Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>
And here's what you would receive back:
<?xml version="1.0"?>
<CityStateLookupResponse>
<ZipCode ID="0">
<Zip5>90210</Zip5>
<City>BEVERLY HILLS</City>
<State>CA</State>
</ZipCode>
</CityStateLookupResponse>
USPS does require that you register with them before you can use the API, but, as far as I could tell, there is no charge for access. By the way, their API has some other features: you can do Address Standardization and Zip Code Lookup, as well as the whole suite of tracking, shipping, labels, etc.
Using the Ziptastic HTTP/JSON API
This is a pretty new service, but according to their documentation, it looks like all you need to do is send a GET request to http://ziptasticapi.com, like so:
GET http://ziptasticapi.com/48867
And they will return a JSON object along the lines of:
{"country": "US", "state": "MI", "city": "OWOSSO"}
Indeed, it works. You can test this from a command line by doing something like:
curl http://ziptasticapi.com/48867
-
1Only problem is that there can be multiple cities/counties in a zip code. Always returning the first one is a bad user experience. To be truly accurate and user friendly you would need some sort of div to contain the results (if more than one) and allow the user to select the proper one. – DarrellNorton Jun 14 '13 at 10:09
-
DarrelNorton: was wondering if you had an example of a zip code that contains multiple cities so we can see what the results from ZipTastic look like for such cases. – Richard Jones Jun 14 '13 at 19:04
-
95961 has multiple entries. There is a preferred one, and then there are acceptable entries. Of course counties are different. For example the zip code 38583 serves 5 counties. And there is no requirement for a county to have a city associated with it - so technically city should be optional. However most post offices will use some sort of city designator. – DarrellNorton Jun 14 '13 at 19:32
-
It looks like the 95961 zip code contains two cities: Olivehurst, CA and Plumas Lake, CA. Ziptastic only returns a single result though: Olivehurst. Something to consider for anybody planning to use Ziptastic. – Richard Jones Jun 15 '13 at 16:12
-
it's a shame they don't return an array instead of a single object -right?! I've done my time with addresses using data from MelissaData (so I can have it locally to my app and don't have to worry about service timeouts) - so I know the pain it can be. – DarrellNorton Jun 16 '13 at 10:17
You can get a database that maps zip code to longitude/latitude, and another database that provides longitude/latitude for all US cities. Then you could just do that on your side, without having to send out to a web service.
I've seen both these databases, but I can't remember where to find them right now. I'll poke around and try to remember to add a comment.