I found several ways to do this using the web APIs. I think the US Postal Service will be the most accurate, since Zip codes are their business, but Ziptastic looks a lot simpler.
US HTTP / XML API Usage
According to this page on the U.S. Postal Service website, which documents its XML-based web API , in particular section 4.0 (page 22) of this PDF , 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 appropriate city and state.
According to their documentation, here is what you would send:
http://SERVERNAME/ShippingAPITest.dll?API=CityStateLookup&XML=<CityStateLookupRequest%20USERID="xxxxxxx"><ZipCode ID= "0"><Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>
And here is what you get back:
<?xml version="1.0"?> <CityStateLookupResponse> <ZipCode ID="0"> <Zip5>90210</Zip5> <City>BEVERLY HILLS</City> <State>CA</State> </ZipCode> </CityStateLookupResponse>
USPS requires you to register with them before using the API, but as far as I can tell, there is no charge for access to it. By the way, their API has some other features: you can standardize addresses and search for zip codes, as well as the entire set of tracking, delivery, labels, etc.
Using the Ziptastic HTTP / JSON API
This is a fairly new service, but according to their documentation, all you have to do is send a GET request to http://ziptasticapi.com , for example
GET http:
And they will return the JSON object line by line:
{"country": "US", "state": "MI", "city": "OWOSSO"}
Indeed, it works. You can verify this from the command line by doing something like:
curl http: