Unparalleled suite of productivity-boosting Web APIs & cloud-based micro-service applications for developers and companies of any size.

APILocation

How do I make an app to convert IP to geolocation?

Applications can obtain the locations of their visitors by integrating the services that provide IP Geolocation API services into their projects, and offer special content to their visitors. By replacing the Amazon currency with the official currency of your location, by setting the official language in your Facebook location as default on the website, Google Maps offers its users environments where they can see their locations live on the map and create routes. Applications that use this and similar IP Geolocation API services can detect the Geolocations of their visitors from their IP addresses.

With this method, which increases user satisfaction, companies can attract more visitors. In this article, we will focus on developing an application using the service of ipstack, which offers free IP Geolocation API service. This application will return the Geolocation information corresponding to the given IP address. So let’s start.

Why Ipstack API?

Ipstack is currently one of the popular services that provide IP Geolocation API services. The ipstack API was preferred for this application because of the instantaneous information of the locations in detail, the ability to return data as JSON or XML, fast results and reasonable prices. In the free package, 100 requests are offered free of charge. It is the most notable among the free IP Geolocation API services.

Making An App To Convert IP To Geolocation with Nodejs

An API key is needed to integrate and use Ipstack API, which provides IP Geolocation API service free of charge. With the free package, API key can be obtained as a member.

After obtaining the API key, we can integrate the Ipstack API into the project.

We create a basic Nodejs project on your desktop with the npm init command.

The library I will use to make the Rest call from the Nodejs project will be ‘axios’. We download and install the ‘axios’ library into the Nodejs project with the ‘npm i axios‘ command.

Then we run the application by writing the following code block in the index.js file, which is the default root file of the npm project.

var axios = require(‘axios’);

//Assume that this value comes from UI
var ip = ‘134.201.250.155’;

var config = {
    method: ‘get’,
    url: ‘http://api.ipstack.com/’+ip+‘?access_key=c1*****1’,
    headers: {}
};

axios(config)
    .then(function(response) {
        console.log(JSON.stringify(response.data));
    })
    .catch(function(error) {
        console.log(error);
    });

When Ipstack API, which provides free IP Geolocation API service, is quickly integrated into the application by default and run, the following response is printed on the console of the application.

{
  “ip”: “134.201.250.155”,
  “type”: “ipv4”,
  “continent_code”: “NA”,
  “continent_name”: “North America”,
  “country_code”: “US”,
  “country_name”: “United States”,
  “region_code”: “CA”,
  “region_name”: “California”,
  “city”: “Los Angeles”,
  “zip”: “90012”,
  “latitude”: 34.0655517578125,
  “longitude”: -118.24053955078125,
  “location”: {
    “geoname_id”: 5368361,
    “capital”: “Washington D.C.”,
    “languages”: [
      {
        “code”: “en”,
        “name”: “English”,
        “native”: “English”
      }
    ],
    “country_flag”: “https://assets.ipstack.com/flags/us.svg”,
    “country_flag_emoji”: “🇺🇸”,
    “country_flag_emoji_unicode”: “U+1F1FA U+1F1F8”,
    “calling_code”: “1”,
    “is_eu”: false
  }
}

Now by changing the IP address, we configure our application to return the name of the country where the IP address is located.

var axios = require(‘axios’);

//Assume that this value comes from UI
var ip = ‘54.93.127.5’;

var config = {
    method: ‘get’,
    url: ‘http://api.ipstack.com/’ + ip + ‘?access_key=c1*****1’,
    headers: {}
};

axios(config)
    .then(function(response) {
        console.log(JSON.stringify(response.data.country_name));
    })
    .catch(function(error) {
        console.log(error);
    });

When this code is run, the result will be as follows.

“Germany”

Conclusion

As can be seen in the integration processes, we can access detailed information about the location of the IP address by sending an IP address to Ipstack API, which provides IP Geolocation API service free of charge. After obtaining the geolocation of the IP address, you can provide a high level of satisfaction to your visitors

Related posts
APICurrency

Exchange Rate API Integration in E-Commerce App in 2024

APICurrency

Building a Real-Time Currency Converter in Java Using Currencylayer API

API

10 Best Stocks APIs For Developers

API

Top 10 Flight Search APIs in 2024

Leave a Reply

Your email address will not be published. Required fields are marked *