Hackernoon logoEasily getting a user’s country, currency, timezone or language from their IP address by@bigilui

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

Bigi Lui Hacker Noon profile picture

@bigiluiBigi Lui

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.


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/

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';
(err, response, body) => {
const result = JSON.parse(body);

Example in jQuery

url: 'http://usercountry.com/v1.0/json/',
dataType: 'json',
success: function(result) {

Example code in PHP (using curl)

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

Example code in Ruby

require 'net/http'
require 'json'
url = 'http://usercountry.com/v1.0/json/'
uri = URI(url)
response = Net::HTTP.get(uri)
result = JSON.parse(response)

Example code in Python

import requests
r = requests.get('http://usercountry.com/v1.0/json/')
result = r.json()

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!


Join Hacker Noon

Create your free account to unlock your custom reading experience.