How to determine the user's location by IP address

Description

The API endpoint returns the IATA code, the name of the nearest city to the user, and additional information.

Request

http://www.travelpayouts.com/whereami?locale=en&ip=95.90.254.107

Request parameters

  • locale — language in which the name of the city is returned (see all languages supported in Aviasales' affiliate tools);
  • ip — IP address of the user (If the address is not transferred, the system determines the IP from the header of the request).

Response

iata: "HAM"
name: "Hamburg"
country_name: "Germany"
country_code: "DE"
coordinates: "9.991586:53.553814"
accuracy: "city"

Response parameters

  • iata — the IATA code of the city
  • name — the city name where the user is located
  • country_name — the country name
  • country_code — the country code
  • coordinates — city coordinates in longitude:latitude format
  • accuracy — how precisely the location was detected. Tells you how much you can trust the returned city:
    - city — the city was detected from the IP address;
    - nearest_city — the city from our IP database is unknown to us, so the nearest city with an airport (within 1,000 km of the user's coordinates) is returned;
    - country — only the country is known from the IP address, so its capital is returned;
    - default — the location could not be detected, so the most popular city for the requested language is returned.

The request always returns a city, even when the location could not be detected, so check accuracy before prefilling a search form with it.

city and nearest_city mean the user's location is known from their IP address and can be trusted. 

country and default mean the city is a fallback rather than where the user actually is — this happens with data center, VPN, and mobile carrier IP addresses. In those cases, it is better to leave the search field empty and let the user pick a city themselves.