Getting new state/province values after user changes country

edited February 2014 in Developers

So my client is on vacation in Costa Rica, and he took a few minutes off to look at my test site, and discovered something strange - when it gets to the page that I created using the API /booking/form call, it pre-selected Costa Rica as the country, and the entries in the State dropdown were pre-filled with states in Costa Rica. Obviously you're using some sort of ip geocoding, which is cool (although I'm not sure how you do it since I'm calling the API from a server in the US). But his credit card address is in the US, and so he changed the country to United States, but there is no API call I can call to get the new list of states/provinces for the country he's chosen, so he's still seeing Costa Rican states instead of US states. Since vacationers are considered a big potential market for this service we're offering, we'd like it if people connecting to the internet in the US but who live in some other country can put in their home address correctly. Is there a good work-around, or maybe a third party service I can use?


  • The SDK library sends an 'X-Forwarded-For' header containing the originating IP, which is how the geocoding determines the client location when using the API.  To get the same region data used by the forms on the hosted page, you can make a call to 'ui/country' with parameter 'country_id' set to one of the country codes returned earlier, and it will return a group of select options you could pop straight into your form.  

    If you prefer to handle them in another format, you could parse what's there, or there are a few public services and datasets that provide some pretty detailed country information... although off the top of my head I can't think of a specific service that stands out.

  • I'm glad you have an interface for the country code thing, because I'd resorted to reverse engineering what your own interface calls, calling the undocumented "/reserve/api?call=ui_country&country_id=xx". This is cleaner.

    I noticed one slight inconsistency though - most countries that don't use a state or province in their address format returns "0" for region_type and region, but for the UK it returns "Region" for region_type and "0" for region. Also if you call /booking/form with a UK IP, it returns customer_region as a text field, although my UK friends say that's not a field that anybody uses in their address.

This discussion has been closed.