NewConnect Claude, Gemini, ChatGPT, and other AI agents to API Ninjas via our MCP server

Geocoding API

The Geocoding API enables you to convert any city from any country to latitude and longitude coordinates. For reverse-geocoding, check out our Reverse Geocoding API.

(4.5)

From 2,473 users

700+

Applications using this API

API Status

Online - All Systems Operational

API Response Times

Average
P50
P75
P90
P95
194ms239ms287ms671ms873ms

Didn't find what you were looking for? Suggest an improvement


/v1/geocoding GET

https://api.api-ninjas.com/v1/geocoding

Get current city coordinates by city and country name.

Parameters

At least one of the following parameters must be set:

  • city  required

    City name.

  • state  optional

    US state (for United States cities only).

  • country  optional

    Country name, 2-letter ISO country code, or 3-letter ISO country code.

  • zipcode  optional

    5-digit zipcode (for United States cities only).

Headers

  • X-Api-Key  required

    API Key associated with your account.

Response

A JSON array of objects, each containing the following fields, or an error if the request is unsuccessful.

  • name

    The name of the city.

  • latitude

    Latitude coordinate of the city.

  • longitude

    Longitude coordinate of the city.

  • country

    2-letter ISO 3166 alpha-2 country code.

Sample Request Live Demo!

city
state
country
zipcode

Try this endpoint live with a free API key

Instant access — no credit card required.

https://api.api-ninjas.com/v1/geocoding?city=London&country=GB

Headers

X-Api-KeyLog in or sign up to get your API Key

Sample Response

JSON
[
    {
      name:"London",
      latitude:51.5085,
      longitude:-0.1257,
      country:"GB"
    }
]

Code Examples

1 2 curl -X GET "https://api.api-ninjas.com/v1/geocoding?city=London&country=GB" \ -H "X-Api-Key: YOUR_API_KEY"

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.

Frequently Asked Questions

  • Yes, but you must have a premium subscription. Commercial use of the Geocoding API is not permitted on the free tier. See our pricing page to choose a plan that fits your needs.
  • Sign up for a free account to instantly get your API key, then pass it in the X-Api-Key header on every request. The documentation above includes ready-to-run examples in cURL, Python, JavaScript, Node.js, and more.
  • Your API requests will simply be denied once you hit your monthly quota — you will never be charged more than the plan you signed up for. To increase your quota, you can upgrade your plan any time on our pricing page.
  • The API responds with a standard HTTP status code and a JSON error message describing the problem. See our error codes reference for the full list of codes and how to resolve each one, or contact support if you need help.
  • Pass the country parameter alongside city — it accepts a country name, a 2-letter ISO code, or a 3-letter ISO code (for example country=GB for London). For United States cities you can further narrow results with the state parameter or a 5-digit zipcode. Browse related location tools in the API catalog.
  • It returns a JSON array of objects, each containing the city name, its latitude and longitude coordinates, and the 2-letter ISO 3166 alpha-2 country code. Failed requests return an error instead, as described in our error codes documentation.
  • The Geocoding endpoint only converts forward, from a city to its latitude and longitude; to go the other direction and resolve coordinates into a location, use our Reverse Geocoding API instead.
  • This endpoint returns only name, latitude, longitude, and country; for richer data such as population or region, use the City API, or look up the local time at those coordinates with the Timezone API.