Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GeorgeD's avatar

What should a developer send to the API for state and city ?

In my application I have a form with Country / State(County, Region) / City. Country is a dropdown with a list of all countries. For example, if the a users select USA, the State and City are dropdown, if they choose other country they are text input. What should a developer send to the API for state and city ? I have in mind 3 options:

  1. Always send state and city as string and if the country_code isn't usa I save them as they are else if it is usa I check if the string match one of the state and city in the database (if they doesn't match return validation error if they match I save the ID)

  2. I tell user to send for USA county_id and state_id (have in API methods to get all states and cities) and for not USA send string on country and city variables.

  3. The API should accept both solution, if the country is usa if they send country_id and city_id I use that (solution2), if they are sending text in country and city I check the text(solution 1). This solution seems better but I should explain it very well in the documentation, and I don't know how professional it is for user to send something it 2 ways.

What should you think it would be better, or if you have any other solution would be great.

0 likes
1 reply
youkineko's avatar

If you could get the full list of state/city to all countries and the accept only id it would be perfect. But since this isn't that easy I will just switch to string for state/city for all countries including USA. The API will be consistent this way and this is a good thing.

Please or to participate in this conversation.