Ahmad Awais

Senior Full Stack WordPress developer — WP Core Contributor — Front-end Fanatic — Accidental Writer. I love to write, talk, build, and share everything about WordPress. You can find me making small automation scripts for DevOps stuff or toying around with WP Business ideas. Let's catch up at Twitter @MrAhmadAwais.

SHARE


What Is the Best API for Geolocating an IP Address?

Ahmad AwaisAhmad Awais

While working on a recent project for a client, I found myself in the need of a good and reasonably reliable Geolocation API to find out more about a user that visits your site.

So, I did a little research for currently available APIs and came up with somewhat an exhaustive list. That is what I plan to share today.

I would use a web service that can return JSON data. Below are all the free *active* IP lookup services I could find and the information they return. If you know of any more, then do add a comment and I’ll update the list.

//api.db-ip.com/addrinfo?api_key=<*your api key*>&addr=<*ip address*>

Returns:

{
"address": "119.152.180.314",
"country": "PK",
"stateprov": "Central Pakistan",
"city": "Pakistan"
}

Limitations:

– 2,500 requests per hour
– Doesn’t support JSONP callbacks
– Requires IP address parameter
– Requires email address to get your API key

$.getJSON( '//freegeoip.net/json/?callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});

Returns:

{
"ip": "119.152.180.314",
"country_code": "PK",
"country_name": "Pakistan",
"region_code": "PB",
"region_name": "Punjab",
"city": "Lahore",
"zip_code": "55110",
"time_zone": "Asia/Karachi",
"latitude": 31.5497,
"longitude": 74.3436,
"metro_code": 0
}

Limitations:

– 10,000 requests per hour

$.getJSON( '//gd.geobytes.com/GetCityDetails?callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});

Returns:

{
"geobytesforwarderfor": "",
"geobytesremoteip": "119.152.180.314",
"geobytesipaddress": "119.152.180.314",
"geobytescertainty": "99",
"geobytesinternet": "SA",
"geobytescountry": "Saudi Arabia",
"geobytesregionlocationcode": "SASH",
"geobytesregion": "Ash Sharqiyah",
"geobytescode": "SH",
"geobyteslocationcode": "SASHJUBA",
"geobytescity": "Jubail",
"geobytescityid": "13793",
"geobytesfqcn": "Jubail, SH, Saudi Arabia",
"geobyteslatitude": "27.004999",
"geobyteslongitude": "49.660999",
"geobytescapital": "Riyadh ",
"geobytestimezone": "+03:00",
"geobytesnationalitysingular": "Saudi Arabian ",
"geobytespopulation": "22757092",
"geobytesnationalityplural": "Saudis",
"geobytesmapreference": "Middle East ",
"geobytescurrency": "Saudi Riyal",
"geobytescurrencycode": "SAR",
"geobytestitle": "Saudi Arabia"
}

Limitations:

– 16,384 requests per hour
– Can return the wrong location (I am not in Saudi Arabia)

$.getJSON( '//geoip.nekudo.com/api/', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});

Returns:

{
"city": "Pakistan",
"country": {
"name": "Pakistan",
"code": "PK"
},
"location": {
"accuracy_radius": 50,
"latitude": 1.2855,
"longitude": 103.8565,
"time_zone": "Asia/Karachi"
},
"ip": "119.152.180.314"
}

Limitations:

– Requires IP address parameter

$.getJSON( '//www.geoplugin.net/json.gp?jsoncallback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});

Returns:

{
"geoplugin_request": "119.152.180.314",
"geoplugin_status": 200,
"geoplugin_credit": "Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.",
"geoplugin_city": "Pakistan",
"geoplugin_region": "Pakistan ( general )",
"geoplugin_areaCode": "0",
"geoplugin_dmaCode": "0",
"geoplugin_countryCode": "PK",
"geoplugin_countryName": "Pakistan",
"geoplugin_continentCode": "AS",
"geoplugin_latitude": "31.5497",
"geoplugin_longitude": "103.855797",
"geoplugin_regionCode": "00",
"geoplugin_regionName": "Pakistan ( general )",
"geoplugin_currencyCode": "PKD",
"geoplugin_currencySymbol": "Rs.",
"geoplugin_currencySymbol_UTF8": "Rs.",
"geoplugin_currencyConverter": 1.4239
}

Limitations:

– 120 requests per minute

$.getJSON( '//ip-api.com/json?callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});

Returns:

{
"as": "PTCL",
"city": "Pakistan",
"country": "Pakistan",
"countryCode": "PK",
"isp": "PTCL Pte Ltd",
"lat": 31.5497,
"lon": 74.3436,
"org": "Pakistan Telecommunications",
"query": "119.152.180.314",
"region": "01",
"regionName": "Central Pakistan Community Development Council",
"status": "success",
"timezone": "Asia/Karachi",
"zip": ""
}

Limitations:

– 150 requests per minute

$.getJSON( '//api.ipify.org?format=jsonp&callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});

Returns:

{
"ip": "119.152.180.314"
}

Limitations:

– None ( unlimited bandwidth )

$.getJSON( '//ipinfo.io/json', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});

Returns:

{
"ip": "119.152.180.314",
"hostname": "No Hostname",
"city": "Pakistan",
"region": "Central Pakistan Community Development Council",
"country": "PK",
"loc": "31.5497,74.3436",
"org": "AS3758 SingNet"
}

Limitations:

– 1,000 requests per day

$.getJSON( '//api.ipinfodb.com/v3/ip-city/?key=&format=json&callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});

Returns:

{
"statusCode": "OK",
"statusMessage": "",
"ipAddress": "119.152.180.314",
"countryCode": "PK",
"countryName": "Pakistan",
"regionName": "Pakistan",
"cityName": "Pakistan",
"zipCode": "54000",
"latitude": "1.28967",
"longitude": "103.85",
"timeZone": "+08:00"
}

Limitations:

– Two requests per second
– Requires registration to get your API key

$.getJSON( '//jsonip.com/?callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});

Returns:

{
"ip": "119.152.180.314",
"about": "/about",
"Pro!": "http://getjsonip.com"
}

Limitations:

– None ( unlimited bandwidth )

$.getJSON( '//ip.jsontest.com/?callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});

Returns:

{
"ip": "119.152.180.314"
}

Limitations:

– Goes down a lot ( over quota ), so I wouldn’t use it for production.

//api.snoopi.io/v1/?apikey=<*your api key*>&user_ip_address=<*ip address*>

Returns:

{
"remote_address": "119.152.180.314",
"requested_address": "119.152.180.314",
"requested_time": "11:25:35 pm",
"requested_date": "January 31, 2017",
"CountryCode": "PK",
"Region": null,
"Region_Full": "Pakistan",
"City": "Lahore",
"PostalZip": "54000",
"TimeZone_Name": "Asia\/Pakistan",
"Latitude": "31.5497",
"Longitude": "74.3436"
}

Limitations:

– 2,500 requests per day
– Doesn’t support JSONP callbacks
– Requires IP address parameter
– Requires registration to get your API key

Before You Begin!

Keep in mind that since these are all free services, your mileage may vary in terms of exceeding quota and uptime, and who knows when/if they will be taken offline down the road. Most of these services also offer a paid tier in case you want something more robust.

It’d be a good thing if you tweet this list. No pressure!
🎩

WPTakeaway: Premium WordPress Newsletter!

Takeaway my professional opinion about Full Stack WordPress Development, building a business, growing a family, and everything web! Delivered to your inbox twice a month. 👻 I hate spam! Pinky Promise!

Senior Full Stack WordPress developer — WP Core Contributor — Front-end Fanatic — Accidental Writer. I love to write, talk, build, and share everything about WordPress. You can find me making small automation scripts for DevOps stuff or toying around with WP Business ideas. Let's catch up at Twitter @MrAhmadAwais.