API – Get the country code as per your IP addresses for free

We were searching for the free API which gives the country code as per your IP addresses as per our requirement we were looking to show team members as per the Country, defaulting different currency as per the active country, defaulting different language as per the active country, etc. Our research found ip-api.com which is free for non-commercial use, no API key required, easy to integrate, and available in JSON, which fulfilled all our requirements, so we use ip-api.com. As they provide free for non-commercial use so we like to share with you as well.

We use a basic fetch request of Javascript. The following code is to get the country code as per your IP address

var requestOptions = {
  method: 'GET',
  redirect: 'follow'
};
fetch("http://ip-api.com/json/?fields=countryCode", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

A PHP code block can be like this:

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'http://ip-api.com/json/?fields=countryCode',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

Read more: Opencart API uses and its documentation

Have a look at their documentation of Geolocation API

We can get the following fields from the API:
status, message, continent, continentCode, country, countryCode, region, regionName, city, district, zip, lat, on, timezone, offset, currency, isp, org, as, asname, reverse, mobile, proxy, hosting, query

Their endpoints are limited to 45 HTTP requests per minute from an IP address but they provide a Pro version with more benefits and unlimited HTTP requests.

So, hope this helps for your personal project to get the country code as per IP address and can use it as per your benefits. With this simple and easy API, we are able to show different team members as per country, and similarly, show different currencies and languages as per the country. Let us know if you find any other easy and better solutions.

Previous articleShow selected categories as featured categories OpenCart module free
Next articleJavascript custom email validation for company email only, Unbounce page

LEAVE A REPLY

Please enter your comment!
Please enter your name here