Your query isn't (IMHO) a good one for an API, as your results are very sparse but need to run over all the data (no general indexing).
So get an OSM extact, filter it with osmosis and then do your own processing on the results.
Keep in mind, that a town can be modelled differently in OSM:
- as tagged node e.g. place=city
- as landuse area (also with additional place tags)
- as boundary so, a (multipolygon)relation
You might also want to have a look at nominatim for geocoding and the legal FAQ for distributing OSM data.