The City API provides useful statistics about tens of thousands of cities around the world.
https://api.api-ninjas.com/v1/city
Get city data from either a name or population range. Returns a list of cities that satisfies the parameters.
At least one of the following parameters (other than limit
and offset
) must be set:
name
optionalName of city.
country
optionalCountry filter. Must be an ISO-3166 alpha-2 country code (e.g. US
).
min_lat
optionalMinimum latitude coordinate.
max_lat
optionalMaximum latitude coordinate.
min_lon
optionalMinimum longitude coordinate.
max_lon
optionalMaximum longitude coordinate.
min_population
optionalMinimum city population.
max_population
optionalMaximum city population.
limit
optionalHow many results to return. Must be between 1
and 30
. Default is 1
. To get more than 30 results, use the offset
parameter.
offset
premium optionalNumber of results to offset for pagination.
X-Api-Key
requiredAPI Key associated with your account.
https://api.api-ninjas.com/v1/city?name=San Francisco
Headers
X-Api-Key
Log in or sign up to get your API Key
1
2
3
4
5
6
7
8
9
10
[
{
"name": "San Francisco",
"latitude": 37.7562,
"longitude": -122.443,
"country": "US",
"population": 3592294,
"is_capital": false
}
]
1
2
3
4
5
6
7
8
9
import requests
name = 'San Francisco'
api_url = 'https://api.api-ninjas.com/v1/city?name={}'.format(name)
response = requests.get(api_url, headers={'X-Api-Key': 'YOUR_API_KEY'})
if response.status_code == requests.codes.ok:
print(response.text)
else:
print("Error:", response.status_code, response.text)
If your programming language is not listed in the Code Example above, you can still make API calls by using a HTTP request library written in your programming language and following the above documentation.