Need a Client Side API for Determing Geo Location of Ip Addresses

Need a client side API for determing geo location of IP addresses

I was looking at these guys a couple months ago: http://www.maxmind.com/app/geolitecountry

It seemed OK, for my limited purposes.

How can I approximate user location through their IP address on server side?

For a local solution, you can use a database file. You can find free ones with virtually limited accuracy below:

  • Maxmind: https://dev.maxmind.com/geoip/geoip2/geolite2/
  • Ip2location: https://lite.ip2location.com/database/ip-country-region-city-latitude-longitude-zipcode-timezone

There exists paying variants with better accuracy. However, when using a local database file, you should ask yourself some questions:

  • Do you want to implement the local database file loading/lookup by yourself?
  • Do you know how to update your database file with no service interruption (IP allocations change frequently)?
  • Is your solution scaling to your needs (in terms of requests/second and latency)?
  • Don't you need to deduce transient information (timezone, currency, etc.)?

If the answer is No to one of the previous questions, you should most probably use an IP Geolocation API. I suggest having a look at my service, Ipregistry: it is fast, reliable and inexpensive.

Why server side geo location detection is preferred over client side API call?

One given reason is security - if you query a commercial API via Javascript and pass the API key in your requests someone else might use it at your expense.

Also with JavaScript you have to issue your request and then wait for the response before you continue to render your page. With a server side solution querying, error handling, etc. is already done when the browser renders the page; also you might possibly cache the requests to you API to lower your costs and speed up delivery.

Location detecting techniques for IP addresses

You can't do this without a database that maps IP addresses to cities/countries/providers. There are commercial offerings such as ip2location that you could use. AFAIK there is no free alternative though, as maintaining such a IP database is quite a lot of work. Free alternative: GeoIP2

Update:
There are several things that allow you to create such a db, if you invest enough time:

  1. Use the databases provided by regional and local registries to find an IP's owner.
  2. Many ISPs use a naming schema that allows you to locate the user. Sometimes you can
    even read the city name in plain text if you do a reverse-DNS lookup. Sometimes it is
    more cryptic. For example I currently have p5dcf6c4a.dip.t-dialin.net , and I have no
    idea that the naming scheme is..
  3. Use a traceroute. If you can't identify the location of a user, you can still find out
    the location of its uplink

Location changed to Ashburn after deploying to heroku

When using an IP geolocation API, you should distinguish 2 scenarios: whether you call your geolocation API from the client-side and from the server-side.

In the former case, it means you are querying the IP geolocation API from the client browser, using JavaScript for instance. In that case, most geolocation APIs provide an origin endpoint that you can invoke without specifying any IP address. You will get information for the IP address associated with the client running the browser. Here is an example using Ipregistry:

https://api.ipregistry.co/?key=tryout&pretty=true

In the latter case, you are receiving a request to your server and you are invoking the IP geolocation API from your server. It could be using PHP, NodeJS, Java, whatever programming language behind an application server or proxy. If we draw a diagram flow, this looks as follows:

A client --> Your server --> IP geolocation API

If you invoke, an IP geolocation API without specifying more information, the best that can be done by the geolocation API is to discover what is the IP address behind your server (i.e. from where the request originates). That's the issue you described and what you experienced.

Hopefully, most IP geolocation APIs include an endpoint where you can specify what IP address to lookup data for. So when you receive a request from a client on your server, you need to extract the client IP address from your request and then use a dedicated IP geolocation API endpoint that allows passing an IP address.

How to extract the client IP address from a request you receive on your server? This is specific to your application. That's why there is no one fit all solution. Most of the time there is a request header that includes the value you are looking for. For instance, the header X-Forwarded-For is quite common but not standard. In PHP you need to use $_SERVER['REMOTE_ADDR'].

Let's say you identify your client IP address as being 200.77.121.230, then using Ipregistry, you would get location and threat data with an HTTP GET request to the next endpoint:

https://api.ipregistry.co/200.77.121.230?key=tryout&pretty=true

How to detect browser country in client site?

There are multiple options to determine the locale. In descending order of usefulness, these are:

  1. Look up the IP address, with an IP geolocation service like Maxmind GeoIP. This is the location the request is coming from, i.e. if an American vacations in Italy and uses a Swedish VPN, it will return Sweden.

It can only be done with the help of the server. The main advantage is that it's very reliable. The accuracy will be country or region for free services, city or region for paid ones.


  1. Look up the precise location on Earth from the browser with the geolocation API. An American vacationing in Italy using a Swedish VPN will register as Italy.

The answer will be very precise, usually no more than 10m off. In principle, it could work client-side, although you may want to perform the coordinate -> country lookup on the server. The main disadvantages are that not all devices have either GPS or WiFi position, and that it generally requires explicit user consent.


  1. Look in the Accept-Language header on the server (or with the help of the server), and extract the locale information. An American vacationing in Italy using a Swedish VPN will register as USA.

The downside is that this is a setting that's extremely easy to change. For instance, English speakers around the world may prefer en-US settings in order to avoid machine-translated text. On modern browsers (as of writing not IE/Edge, and only Safari 11+), you can also request navigator.languages.


  1. navigator.language is the first element of the navigator.languages header. All of the considerations of navigator.languages apply. On top, this information can sometimes be just the language without any locale (i.e. en instead of en-US).

  2. Use another, third-party service. For instance, if the user signs in via a Single-Sign-On system such Facebook connect, you can request the hometown of the user. This information is typically very unreliable, and requires a third party.

Best node.js module for finding location?

Using IP-based geolocation is possible, but not very accurate. So I suggest you to think about going with a hybrid approach, like trying to get the users location via the HTML5 geolocation API inside the browser and fallback to the serverside if necessary.

I took a look at the two most used geoip/-location modules available for node.js and they both use the datasets provided by MaxMind. AFAIK you have to keep these databases up-2-date manually, which is a clear downer. So you may consider writing a little sync service / script, which updates the database once per month. (More information on MaxMinds free solution can be found here).

kuno/GeoIP is a GeoIP binding, so at it's core it's using the libGeoIP C library, which is okay, but maybe not as portable as the pure JavaScript implementation bluesmoon/node-geoip offers. Both are okay and it's up to you which library you like more. In terms of performance you have to do some benchmakrs (if this topic matters to you) … There is no general answer to the question, which type of module (C-binding/native) will be faster.

If you're willing to spend a few bucks you could also look into MaxMinds Web Service, which is a simple REST API and will be the most precise way to go. The documentation is quite good – so getting started with that won't be a problem.



Related Topics



Leave a reply



Submit