4

I'd like to use an API service of some sort to take in a zipcode and return any cities that match this zipcode.

USPS manual lookup on their site does this, and Crate and Barrel does this in their checkout.

It seems like the zipcode API from USPS is only able to return one city though. I've looked into Geonames database, but it also seems set up for only one city per zip.

Is there a way to accomplish this?

Crate and Barrel's checkout zipcode autocomplete

Community
  • 1
  • 1
xEmptyCanx
  • 528
  • 3
  • 10
  • 22
  • For a website form for your visitors or do you have a list of ZIP codes you need to match up? – Matt Jun 03 '14 at 13:30
  • I'd like this to be used in a website form, so that someone gets the convenience of the autocompleted city/state, with the benefit of being able to pick the city they are used to shipping to. – xEmptyCanx Jun 03 '14 at 17:42
  • 1
    You might try [SmartyStreets' ZIP Code API](http://smartystreets.com/kb/liveaddress-api/zipcode-api) -- the city names come proper-cased and it's free for a certain number of lookups. I don't know of a way to do this with USPS directly. (I hesitate to post this as an answer since I work at SmartyStreets, unless you decide to use it.) – Matt Jun 03 '14 at 18:12

1 Answers1

3

I'll go ahead and post @Matt's answer, since I've used the SmartyStreets ZIP Code API for just that purpose. You can query this API one of three ways:

  1. ZIP Code
  2. City + State
  3. City + State + ZIP Code

For a single ZIP Code (which is what you are asking about) this is a simple GET request:

curl -v 'https://us-zipcode.api.smartystreets.com/lookup?
        auth-id=YOUR+AUTH-ID+HERE&
        auth-token=YOUR+AUTH-TOKEN+HERE&
        zipcode=65202'

The response (with valid AUTH-ID and AUTH-TOKEN) is:

[
  {
      "input_index": 0,
      "city_states": [
          {
              "city": "Columbia",
              "state_abbreviation": "MO",
              "state": "Missouri",
              "mailable_city": true
          },
          {
              "city": "Hinton",
              "state_abbreviation": "MO",
              "state": "Missouri",
              "mailable_city": false
          },
          {
              "city": "Lindbergh",
              "state_abbreviation": "MO",
              "state": "Missouri",
              "mailable_city": false
          },
          {
              "city": "Midway",
              "state_abbreviation": "MO",
              "state": "Missouri",
              "mailable_city": false
          },
          {
              "city": "Murry",
              "state_abbreviation": "MO",
              "state": "Missouri",
              "mailable_city": false
          },
          {
              "city": "Prathersville",
              "state_abbreviation": "MO",
              "state": "Missouri",
              "mailable_city": false
          },
          {
              "city": "Shaw",
              "state_abbreviation": "MO",
              "state": "Missouri",
              "mailable_city": false
          },
          {
              "city": "Stephens",
              "state_abbreviation": "MO",
              "state": "Missouri",
              "mailable_city": false
          }
      ],
      "zipcodes": [
          {
              "zipcode": "65202",
              "zipcode_type": "S",
              "default_city": "Columbia",
              "county_fips": "29019",
              "county_name": "Boone",
              "state_abbreviation": "MO",
              "state": "Missouri",
              "latitude": 38.99775,
              "longitude": -92.30798,
              "precision": "Zip5",
              "alternate_counties": [
                  {
                      "county_fips": "29027",
                      "county_name": "Callaway",
                      "state_abbreviation": "MO",
                      "state": "Missouri"
                  }
              ]
          }
      ]
  }
]

As you can see, this gives the same list of cities as your picture example. It also includes more detailed information on the first city_state in the list.

For completeness, here is a sample request demonstrating all three query options. (n.b. For any request outside of a single ZIP Code, the API requires a POST call):

curl -v 'https://us-zipcode.api.smartystreets.com/lookup?
        auth-id=YOUR+AUTH-ID+HERE&
        auth-token=YOUR+AUTH-TOKEN+HERE'
-H "Content-Type: application/json"
--data-binary '
[
    {
        "zipcode":"12345"
    },
    {
        "city":"North Pole",
        "state":"AK"
    },
    {
        "city":"cupertino",
        "state":"CA",
        "zipcode":"95014"
    }
]'

And here is the response for that example request:

[
  {
      "input_index": 0,
      "city_states": [
          {
              "city": "Schenectady",
              "state_abbreviation": "NY",
              "state": "New York",
              "mailable_city": true
          },
          {
              "city": "General Electric",
              "state_abbreviation": "NY",
              "state": "New York",
              "mailable_city": false
          },
          {
              "city": "Schdy",
              "state_abbreviation": "NY",
              "state": "New York",
              "mailable_city": false
          }
      ],
      "zipcodes": [
          {
              "zipcode": "12345",
              "zipcode_type": "U",
              "default_city": "Schenectady",
              "county_fips": "36093",
              "county_name": "Schenectady",
              "state_abbreviation": "NY",
              "state": "New York",
              "latitude": 42.81565,
              "longitude": -73.94232,
              "precision": "Zip5"
          }
      ]
  },
  {
      "input_index": 1,
      "city_states": [
          {
              "city": "North Pole",
              "state_abbreviation": "AK",
              "state": "Alaska",
              "mailable_city": true
          }
      ],
      "zipcodes": [
          {
              "zipcode": "99705",
              "zipcode_type": "S",
              "default_city": "North Pole",
              "county_fips": "02090",
              "county_name": "Fairbanks North Star",
              "state_abbreviation": "AK",
              "state": "Alaska",
              "latitude": 64.77911,
              "longitude": -147.36885,
              "precision": "Zip5"
          }
      ]
  },
  {
      "input_index": 2,
      "city_states": [
          {
              "city": "Cupertino",
              "state_abbreviation": "CA",
              "state": "California",
              "mailable_city": true
          }
      ],
      "zipcodes": [
          {
              "zipcode": "95014",
              "zipcode_type": "S",
              "default_city": "Cupertino",
              "county_fips": "06085",
              "county_name": "Santa Clara",
              "state_abbreviation": "CA",
              "state": "California",
              "latitude": 37.32056,
              "longitude": -122.03865,
              "precision": "Zip5"
          }
      ]
  }
]

I hope that helps!

EDIT - I've updated this with an example specific to the question. Thanks to @AmyAnuszewski for calling that out.

LHM
  • 721
  • 12
  • 31
  • 2
    It would be better if the code sample matched the query, but thanks for pointing out a service that does return all the city results for a given zip. – Amy Anuszewski Aug 15 '18 at 19:39
  • 1
    Good point, @AmyAnuszewski - I've updated the answer accordingly – LHM Aug 17 '18 at 13:21