Algobook
- The developer's handbook
mode-switch
back-button
Buy Me A Coffee
Fri May 12 2023

How to get city information from a free API

In this tutorial, we will go show how we can get city information from a free API provider. If you have read my articles before or if you are using our APIs here on Algobook, you probably know where this is heading.

Yes, we have a City API for you to consume, and this is a promotion article!

In this guide, I will briefly share how the API can be used and what we offer. For more info, head over to our documentation page.

Consume the API

So, if we are interested in some basic data of a given city, such as population, country it belongs to or perhaps the coordinates - this is all available through the API.

Get data for Paris

We will start with getting data from a well known city, Paris. This is how we can do it in JavaScript

const response = await fetch("https://api.algobook.info/v1/city?search=Paris"); const data = await response.json(); console.log(data);

And the output will be

{ "city": { "id": 2988507, "name": "Paris", "population": 2138551, "lat": 48.85341, "lon": 2.3488, "country": "France" } }

Get data for "Phil"

If we want to get data for a partial search string, the API will return the city that with highest probability.

const response = await fetch("https://api.algobook.info/v1/city?search=Phil"); const data = await response.json(); console.log(data);

Add the output will then be

{ "city": { "id": 4560349, "name": "Philadelphia", "population": 1517550, "lat": 39.952335, "lon": -75.163789, "country": "United States" } }

Outro

That's it for this guide. I hope you enjoyed it and I hope you think this API can help you with your project in some way. If you have any questions or feedback, please send us an email here.

Thanks for reading and have a great day!

signatureFri May 12 2023
See all our articles