What is IP Geolocation

ipapi ~ ipapi.co
2 min readJul 3, 2019

--

A primer on IP location lookup. Let us start by defining “what is an IP address” and what do we mean by “geolocation”.

An IP (or IP Address) is a label assigned to a device connected to a computer network (e.g. a laptop or a mobile phone). It could either be a 32-bit number (IPv4) or a 128-bit one (IPv6). An IP address uniquely identifies the device and establishes a path to reach it on the network. When a client (user) visits a website, the browser initiates a TCP connection to the web server and communicates the client’s IP address. Once the connection is established, the browser sends an HTTP(s) request to retrieve the website’s content. At this stage, the webserver is aware of the client’s IP address and it can use a service for IP location to find the client’s real world location.

Geolocation refers to identifying the geographic (or physical) location of the device. There are various techniques for geolocating a device connected to a network, its IP address is one of the easiest, non-intrusive and quickest ways to achieve that.

API (or Application Programming Interface) is a set of instructions that allows software applications to communicate with one another. For example, the ipapi — IP lookup service is at one end of the “API” and your website or mobile application is at the other end. If your website sends an HTTPs request to https://ipapi.co/json , it’ll receive an object as a response that provides the location of the client.

Now that we have defined some key terms, let us look at an example :

  • Open https://ipapi.co/json in your web browser. The response is a structured array that shows your location.
  • Similarly, https://ipapi.co/8.8.8.8/json shows the location of an IP address i.e. 8.8.8.8
  • Another example : https://ipapi.co/2001:4860:4860::8888/json shows the location of an IPv6 address
  • Our API returns location in JSON / CSV / XML / YAML etc. formats (see the docs)

Let’s add some real code to illustrate how the API works:

Python

# Using the requests library (i.e. pip install requests)from requests import getloc = get(‘https://ipapi.co/8.8.8.8/json/')
print loc.json()

Ruby


require ‘net/http’
require ‘json’
loc = Net::HTTP.get(URI(‘https://ipapi.co/8.8.8.8/json/'))
puts JSON.parse(loc)

PHP

<?php
$loc = file_get_contents(‘https://ipapi.co/8.8.8.8/json/');
echo $loc;
$obj = json_decode($loc);
?>

Node.js

var https = require(‘https’);https.get(‘https://ipapi.co/8.8.8.8/json/', function(resp){
var body = ‘’
resp.on(‘data’, function(data){
body += data;
});
resp.on(‘end’, function(){
var loc = JSON.parse(body);
console.log(loc);
});
});

Javascript (jQuery)

$.getJSON(‘https://ipapi.co/8.8.8.8/json/', function(data){
console.log(data)
})

Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class Ipapi {public static void main(String[] args) throws IOException {
URL ipapi = new URL(“https://ipapi.co/8.8.8.8/json/");
URLConnection c = ipapi.openConnection();
c.setRequestProperty(“User-Agent”, “java-ipapi-client”);
BufferedReader reader = new BufferedReader(
new InputStreamReader(c.getInputStream())
);
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
reader.close();
}
}

--

--

ipapi ~ ipapi.co
ipapi ~ ipapi.co

Written by ipapi ~ ipapi.co

IP Lookup | IP Geolocation API | IP Address Locator by Kloudend, Inc. USA. Trusted by Fortune 500 !

No responses yet