🤯 50% Off! 700+ courses, assessments, and books

GeoIP JSON Example

Michael Wanyoike

This article series was rewritten in mid 2017 with up-to-date information and fresh examples.

GeoIP is a type of Geolocation software that is used to deduce the geographical location of a person or an object using an IP address. There are a number of free and paid geolocation databases which offer these GeoIP services. Accuracy among these databases generally ranges from country, state, city and up to post code level. This technology is often used for geo marketing, regional pricing, fraud detection and criminal investigation.

In this example we will look at an online database, IP-API.com, that returns GeoIP data in JSON format. Simply opening http://ip-api.com/json/ will return the following JSON result:

  "as": "AS16509 Amazon.com, Inc.",
  "city": "Boardman",
  "country": "United States",
  "countryCode": "US",
  "isp": "Amazon",
  "lat": 45.8696,
  "lon": -119.688,
  "org": "Amazon",
  "query": "",
  "region": "OR",
  "regionName": "Oregon",
  "status": "success",
  "timezone": "America\/Los_Angeles",
  "zip": "97818"

To see your own Geolocation data in JSON format, just open http://ip-api.com/json/.

Building an application that utilizes this service is pretty simple. You can take a quick look at the docs to see how to implement callbacks and other features. In the event of an error, the following JSON response will be returned:

  "status": "fail",
  "message": "ERROR MESSAGE",

You should note that this service is not absolutely free. If you need to perform more than 150 requests per minute you will have to sign up for a pro account.

Here are the other examples in this series: