Bigi Lui
2017-08-03 ⋅ 5 min read

Easily getting a user’s country, currency, timezone or language from their IP address

In many projects in the past, when I’ve had to use a Geo IP API service to look up a user’s IP address, it was usually for one of several reasons — getting the user’s country (for geo-locking certain features, or defaulting to certain sections or categories of the site), defaulting a user’s created account to a certain currency, getting their timezone to appropriately time retention emails or notifications, or setting a default language. (for the last case, usually in conjunction with an Accept-Language HTTP header)

Instead of having to find a Geo IP API service to do the IP lookup, and then look for another service or database for the currency or language lookup, you can now use the User Country API service to do all of this in one step.

Simple REST API

The API call itself is just a HTTP GET request. In fact, you could even just test it from your own browser by going to: https://usercountry.com/v1.0/json/173.194.192.101

You’ll see a JSON-formatted response that looks just like the sample on the homepage. This will already contain the key data you want for the user of that IP — currency, official language of the country, and timezone information. There are also a few more additional things like continent, more precise region data (even zip/postal codes).

Below is some sample codes in major languages to demonstrate how easy it is to get the default currency of a user just from their IP address! You could just as easily modify them to get their timezone, country language, or postal code.

Example code in Node.js (using request package)

Install request library from npm:

npm install request

App code:

import request from 'request';

request('http://usercountry.com/v1.0/json/173.194.192.101',
  (err, response, body) => {
    const result = JSON.parse(body);
    result.currency.code;
  }
);

Example in jQuery

$.ajax({
  url: 'http://usercountry.com/v1.0/json/173.194.192.101',
  dataType: 'json',
  success: function(result) {
    result.currency.code;
  }
});

Example code in PHP (using curl)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://usercountry.com/v1.0/json/173.194.192.101');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
$result['currency']['code'];

Example code in Ruby

require 'net/http'
require 'json'

url = 'http://usercountry.com/v1.0/json/173.194.192.101'
uri = URI(url)
response = Net::HTTP.get(uri)
result = JSON.parse(response)
result['currency']['code']

Example code in Python

import requests

r = requests.get('http://usercountry.com/v1.0/json/173.194.192.101')
result = r.json()
result['currency']['code']

Simple enough, right?

Take note that in all of the above examples, you should add a little bit more error checking such as checking to see if the request succeeded, and also checking the response object’s status field to ensure it says success from the User Country API call before using the results.

Happy coding!