Real-time Geolocation & Reverse IP Lookup with ipapi

By (Sponsor)  on  

ipapi

There's an incredible amount you can do when you know where your web visitor is located.  You can target advertising to the user, you can load maps of nearby search queries, you can localize website verbiage, you can customize content for their region, etc.  I guess what I'm trying to say is reliable geolocation data can make your users happy and you a lot of money.

Whenever I need an API for critical tasks, from currency conversion and email validation, to phone number validation, I look to apilayer, who provide amazing, reliable APIs.  Their latest offering is ipapi, another amazingly simple but reliable and powerful API to get user geolocation information.

Quick Hits

  • Trusted by Wix, Allianz, and gengo
  • Provides location, ISP, currency, and timezone information
  • Free to sign up!
  • Provides TOR detection and fraud prevention
  • Allows response format of JSON or XML
  • Allows JSONP for client side requests

ipapi

Using ipapi

The easiest way to use ipapi is by simply sending the IP address:

curl http://api.ipapi.com/75.135.72.238?access_key=MY_ACCESS_KEY&format=1

You quickly receive a response with location details based on the IP address:

{  
   "ip":"75.135.72.238",
   "type":"ipv4",
   "continent_code":"NA",
   "continent_name":"North America",
   "country_code":"US",
   "country_name":"United States",
   "region_code":"WI",
   "region_name":"Wisconsin",
   "city":"Madison",
   "zip":"53711",
   "latitude":43.0356,
   "longitude":-89.4526,
   "location":{  
      "geoname_id":5261457,
      "capital":"Washington D.C.",
      "languages":[  
         {  
            "code":"en",
            "name":"English",
            "native":"English"
         }
      ],
      "country_flag":"http:\/\/assets.ipapi.com\/flags\/us.svg",
      "country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8",
      "country_flag_emoji_unicode":"U+1F1FA U+1F1F8",
      "calling_code":"1",
      "is_eu":false
   }
}

You can also customize the data returned by ipapi by taking advantage of the fields parameter:

https://api.ipapi.com/api/75.135.72.238?access_key=MY_ACCESS_KEY&fields=latitude,longitude
{"latitude":43.0356,"longitude":-89.4526}

ipapi even allows you to specify a response language for select response properties:

http://api.ipapi.com/api/161.185.160.93?access_key=MY_ACCESS_KEY&language=es
{
    "ip": "161.185.160.93",
    "type": "ipv4",
    "continent_code": "NA",
    "continent_name": "Norteamérica",  <-- translated
    "country_code": "US",
    "country_name": "Estados Unidos",  <-- translated
    "region_code": "NY",
    "region_name": "Nueva York",  <-- translated
    "city": "Brooklyn",
    "zip": "11238",
    "latitude": 40.676,
    "longitude": -73.9629,
    [...]
}

If you're looking for security-centric data for the IP address, ipapi has you covered there:

curl https://api.ipapi.com/api/75.135.72.238?access_key=MY_ACCESS_KEY&security=1
{
    [...]
    "security": {
        "is_proxy": false,
        "proxy_type": null,
        "is_crawler": false,
        "crawler_name": null,
        "crawler_type": null,
        "is_tor": false,
        "threat_level": "high",
        "threat_types": [
            "attack_source",
            "attack_source_mail"
        ]
    }
}

ipapi also allows you to provide a callback parameter so that you can take advantage of JSONP on the client side:

// set endpoint and your access key
var ip = '75.135.72.238'
var access_key = 'MY_ACCESS_KEY';

// get the API result via jQuery.ajax
$.ajax({
    url: 'http://api.ipapi.com/' + ip + '?access_key=' + access_key,   
    dataType: 'jsonp',
    success: function(json) {

        // output the "calling_code" object inside "location"
        alert(json.location.calling_code);
        
    }
});

ipapi is another amazingly practical and useful API from apilayer.  Having geolocation data for the user is an invaluable tool in improving user experience, conversion, utility, and anything your imagine can dream up.  Better yet is that the API is flexible, reliable, and backed by proven API experts.  If you want an advantage in visitor user experience and ad space, check out ipapi -- you wont be disappointed!

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!