IP Geolocation API

By  on  

Detecting geolocation information from your user provides you a massive advantage: improved conversion, better segmenting and analytics, and providing more relevant content. There are numerous paid services which will provide geolocation info but they're oftentimes too costly or short of valuable information. I was super impressed with IP Geolocation API, a free geolocation API that provides extra details like languages spoken and currency code!

The URL format for IP Geolocation API is simple:

GET https://api.ipgeolocationapi.com/geolocate/91.213.103.0

The response payload look like:

{
   "continent":"North America",
   "address_format":"{{recipient}}\n{{street}}\n{{city}} {{region_short}} {{postalcode}}\n{{country}}",
   "alpha2":"US",
   "alpha3":"USA",
   "country_code":"1",
   "international_prefix":"011",
   "ioc":"USA",
   "gec":"US",
   "name":"United States of America",
   "national_destination_code_lengths":[  
      3
   ],
   "national_number_lengths":[  
      10
   ],
   "national_prefix":"1",
   "number":"840",
   "region":"Americas",
   "subregion":"Northern America",
   "world_region":"AMER",
   "un_locode":"US",
   "nationality":"American",
   "postal_code":true,
   "unofficial_names":[
      "United States",
      "Vereinigte Staaten von Amerika",
      "États-Unis",
      "Estados Unidos",
      "アメリカ合衆国",
      "Verenigde Staten"
   ],
   "languages_official":[
      "en"
   ],
   "languages_spoken":[  
      "en"
   ],
   "geo":{  
      "latitude":37.09024,
      "latitude_dec":"39.44325637817383",
      "longitude":-95.712891,
      "longitude_dec":"-98.95733642578125",
      "max_latitude":71.5388001,
      "max_longitude":-66.885417,
      "min_latitude":18.7763,
      "min_longitude":170.5957,
      "bounds":{  
         "northeast":{  
            "lat":71.5388001,
            "lng":-66.885417
         },
         "southwest":{  
            "lat":18.7763,
            "lng":170.5957
         }
      }
   },
   "currency_code":"USD",
   "start_of_week":"sunday"
}

A few bits of extra information I appreciate:

  • address_format - Not every country formats their addresses the same way, and knowing the address format helps to ensure a delivery gets there
  • country_code - Useful for phone numbers
  • unofficial_names - Useful in getting localized names for a country
  • languages_official && languages_spoken - Useful for translation purposes.

In short, you get an incredible amount of information based on IP address!

IP Geolocation isn't just specific to IPs -- the API also provides an endpoint to get information about countries:

// https://api.ipgeolocationapi.com/countries/US

{
  "continent": "North America",
  "address_format": "{{recipient}}\n{{street}}\n{{city}} {{region_short}} {{postalcode}}\n{{country}}",
  "alpha2": "US",
  "alpha3": "USA",
  "country_code": "1",
  "international_prefix": "011",
  "ioc": "USA",
  "gec": "US",
  "name": "United States of America",
  "national_destination_code_lengths": [
    3
  ],
  "national_number_lengths": [
    10
  ],
  "national_prefix": "1",
  "number": "840",
  "region": "Americas",
  "subregion": "Northern America",
  "world_region": "AMER",
  "un_locode": "US",
  "nationality": "American",
  "postal_code": true,
  "unofficial_names": [
    "United States",
    "Vereinigte Staaten von Amerika",
    "États-Unis",
    "Estados Unidos",
    "アメリカ合衆国",
    "Verenigde Staten"
  ],
  "languages_official": [
    "en"
  ],
  "languages_spoken": [
    "en"
  ],
  "geo": {
    "latitude": 37.09024,
    "latitude_dec": "39.44325637817383",
    "longitude": -95.712891,
    "longitude_dec": "-98.95733642578125",
    "max_latitude": 71.5388001,
    "max_longitude": -66.885417,
    "min_latitude": 18.7763,
    "min_longitude": 170.5957,
    "bounds": {
      "northeast": {
        "lat": 71.5388001,
        "lng": -66.885417
      },
      "southwest": {
        "lat": 18.7763,
        "lng": 170.5957
      }
    }
  },
  "currency_code": "USD",
  "start_of_week": "sunday"
}

Best of all, IP Geolocation API is open source, so you can inspect the code, run your own server, and even contribute if you wish! Open source FTW!

IP Geolocation API is a very useful tool to help localize, monetize, convert, and provide a great user experience.

Recent Features

  • By
    5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

Incredible Demos

  • By
    Introducing LazyLoad 2.0

    While improvements in browsers means more cool APIs for us to play with, it also means we need to maintain existing code.  With Firefox 4's release came news that my MooTools LazyLoad plugin was not intercepting image loading -- the images were loading regardless of...

  • By
    WebKit-Specific Style:  -webkit-appearance

    I was recently scoping out the horrid source code of the Google homepage when I noticed the "Google Search" and "I'm Feeling Lucky" buttons had a style definition I hadn't seen before:  -webkit-appearance.  The value assigned to the style was "push-button."  They are buttons so that...

Discussion

  1. Šime Vidas

    But how do you get the user’s IP address?

    • Tiago Celestino

      You can create to server to get this information.

  2. Hristo Chakarov

    Great feature will be if this provides TZ (timezone). Other than that – great tool, 10x for sharing

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