Ever wondered how Google is able to give you location-based search results based on where you are? What Google is doing is an IP Geo-location lookup. Finding the location from an IP address is quite simple given the large number of geo-location service providers that are available nowadays.

What is an IP Address?

Mobile Phone

Locating made easy.

To put it simply, an IP address is like your twitter handle. Just as @vitarsi identifies this website’s handle on Twitter, the IP address identifies each device connected to a network (including the internet). IP Addresses generally look something like 104.18.2.127 (an IPv4 address).

Although, the IP address usually keeps changing every time you connect to the network, the range of addresses that you might be assigned with depends on your ISP (Internet Service Provider).

That’s what enables us to geo-locate an IP address. All ISP’s, be it your broadband service provider or your mobile phone carrier, are given a block of IP addresses which are unique to each location.

IP Address Geo-location Service providers

There are a plenty of free IP Address geo-location service providers available nowadays. Although they provide limited look-ups a day, most users will never need more than their free quotas.

One such provider that I’ll recommend is ip-api.com. They provide up to 45 look-ups in an hour which is more than what most users will need.

Let’s get started

ip-api.com provides JSON responses to queries. A simple GET request to http://ip-api.com/json/104.18.2.127 gives the following result.

{
  "query": "104.18.2.127",
  "status": "success",
  "country": "Canada",
  "countryCode": "CA",
  "region": "ON",
  "regionName": "Ontario",
  "city": "Toronto",
  "zip": "M5A",
  "lat": 43.6532,
  "lon": -79.3832,
  "timezone": "America/Toronto",
  "isp": "Cloudflare, Inc.",
  "org": "Cloudflare, Inc.",
  "as": "AS13335 Cloudflare, Inc."
}

As you can see, you can easily get approximate location details like Latitude/Longitude, City, Country, ISP details, etc from the IP address. You can even select the fields you need. For example,

GET http://ip-api.com/json/104.18.2.127?fields=status,country,regionName,city

{
  "status": "success",
  "country": "Canada",
  "regionName": "Ontario",
  "city": "Toronto"
}

What if you want details on your own IP address? That’s also quite simple. You could do it from Command Prompt or Terminal with a curl or wget. I’ve removed all the identifiable information, but you get the idea. Very handy indeed.

C:\>curl ip-api.com
{
"status" : "success",
"continent" : "****",
"continentCode": "**",
"country" : "****",
"countryCode" : "**",
"region" : "**",
"regionName" : "****",
"city" : "****",
"district" : "",
"zip" : "****",
"lat" : xx.xxx,
"lon" : yy.yyy,
"timezone" : "****/****",
"offset" : zzzz,
"currency" : "****",
"isp" : "****",
"org" : "****",
"as" : "****",
"asname" : "****",
"mobile" : false,
"proxy" : false,
"hosting" : false,
"query" : "****"
}

Summary

I’m sure you know how to implement this in your next big project to geo-locate IP Addresses. Do let me know in the comments if you need specific examples on how to use this.