How to Use a Real Time Location API to Enhance Your App

Apps and websites strive to provide the best experience for their visitors. Businesses that maximize the visit experience to their visitors have the power of high traffic and transaction volume. If the visitors are satisfied with the processes they spend in the applications, they become permanent users of that application. This provides a great opportunity for applications to get ahead of their competitors.

Businesses aim to do this by obtaining some information from their visitors through their applications and websites. Nowadays, it is a very popular method to increase visitor satisfaction by using the location information of the visitors.

By using services that provide real time location API service, location information can be obtained from the IP address of the users. From the visitor location information obtained, many customized opportunities or content can be presented to the visitors.

In this article, we will talk about how to integrate Ipstack API, which is one of the most popular services providing real time location API, and how to use it.

Integration Ipstack API to Nodejs

Before integrating and exploring the Ipstack API, which provides real-time location API service, we need an API key. You can find a lot of information about obtaining an API key and Ipstack APIhere. We choose one of the affordable and various packages on the Ipstack website and sign up.

Before integrating and exploring the Ipstack API, which provides real-time location API service, we need an API key. You can find a lot of information about obtaining an API key and Ipstack API here. We choose one of the affordable and various packages on the Ipstack website and sign up.

Now let's create a Nodejs application and include the express and request libraries in the project with the following commands

 npm i express router 

Then let's paste the following codes into the index.js file.


    constexpress = require ('express');
    const locationRouter = require('./routes/locationRoutes');
    const app = express();
    app .use ('/api', locationRouter);
        var server = app.listen(3000, function() {
              console.log('App is running!');

});

Next, let's create the locationRoutes.js file that makes a request to the Ipstack API and paste the following codes.


    constexpress = require ('express');
    const router = express.Router();
    let request = require('request');

    let apiKey = 'c1*****51';
    router.get('/locations/:ipAddress', (req, res) => {
    let ip = req.params.ipAddress;
    let url = `http://api.ipstack.com/${ip}?access_key=${apiKey}`;
    request(url, function (err, response, body) {
        if (err){
             console.log('error:', error);
            } else {
                 consoleconsole.log('body:', body);
                res.json(body);
        }
      });

    });

        module.exports = router;
  
    

Yes, everything looks ready. We developed the visitor's IP information to take parameters to the Rest API we wrote. Now let's run the following command from the terminal and start the application

 npm start index.js 

From the Postman application, we will make a Get request to http://localhost:3000/api/locations/{ipAddress}. We will add 72.229.28.185 as the IP address to the query and throw the following request. http://localhost:3000/api/locations/72.229.28.185

When we send this request, the response is as follows.

    {
         "ip": "72.229.28.185",
        "type": "ipv4",
        "continent_code": "NA",
        "continent_name": "North America",
        "country_code": "US",
        "country_name": "United States",
        "region_code": "NY",
        "region_name": "New York
        "city": "Manhattan",
        "zip": "10020",
        "latitude": 40.7589111328125,
        "longitude": -73.97901916503906,
        "location": {
            "geoname_id": 5125771,
            "capital": "Washington D.C.",
            "languages": [
      {
        "code": "en",
        "name": "English",
        "native": "English"
      }
    ],
    "country_flag": "https://assets.ipstack.com/flags/us.svg",
    "country_flag_emoji": "\ud83c\uddfa\ud83c\uddf8",
    "country_flag_emoji_unicode": "U+1F1FA U+1F1F8",
    "calling_code": "1",
    "is_eu": false
  },
  "time_zone": {
    "id": "America/New_York",
    "current_time": "2022-08-24T11:33:22-04:00",
    "gmt_offset": -14400,
    "code": "EDT",
    "is_daylight_saving": true
  },
  "currency": {
    "code": "USD",
    "name": "US Dollar",
    "plural": "US dollars",
    "symbol": "$",
    "symbol_native": "$"
  },
  "connection": {
    "asn": 12271,
    "isp": "Charter Communications Inc"
  },
  "security": {
    "is_proxy": false,
    "proxy_type": null,
    "is_crawler": false,
    "crawler_name": null,
    "crawler_type": null,
    "is_tor": false,
    "threat_level": "low",
    "threat_types": null
  }
}

                    

Let's change the IP address to 95.65.134.148 and request again.

{
    "ip": "95.65.134.148",
    "type": "ipv4",
    "continent_code": "AS",
    "continent_name": "Asia",
    "country_code": "TR",
    "country_name": "Turkey",
    "region_code": "34",
    "region_name": "Istanbul",
    "city": "Istanbul",
    "zip": "34091",
    "latitude": 41.02790069580078,
    "longitude": 28.940000534057617,
    "location": {
        "geoname_id": 745044,
        "capital": "Ankara",
        "languages": [
    {
    "code": "tr",
    "name": "Turkish",
    "native": "T\u00fcrk\u00e7e"
    }
],
    "country_flag": "https://assets.ipstack.com/flags/tr.svg",
    "country_flag_emoji": "\ud83c\uddf9\ud83c\uddf7",
    "country_flag_emoji_unicode": "U+1F1F9 U+1F1F7",
    "calling_code": "90",
    "is_eu": false
  },
  "time_zone": {
    "id": "Europe/Istanbul",
    "current_time": "2022-08-24T18:52:57+03:00",
    "gmt_offset": 83821,
    "code": "+03",
    "is_daylight_saving": false
  },
  "currency": {
    "code": "TRY",
    "name": "Turkish Lira",
    "plural": "Turkish Lira",
    "symbol": "TL",
    "symbol_native": "TL"v
  },
  "connection": {
    "asn": 9999,
    "isp": "Vodafone Net Iletisim Hizmetleri Anonim Sirketi"
  },
  "security": {
    "is_proxy": false,
    "proxy_type": null,
    "is_crawler": false,
    "crawler_name": null,
    "crawler_type": null,
    "is_tor": false,
    "threat_level": "low",
    "threat_types": null
  }
}


Consolusion

We have integrated the Ipstack API, which provides real-time location API service, into the application. After obtaining the API key, it only took a few steps to integrate the Ipstack API into the application. You can improve the user experience of your visitors by integrating Ipstack API into your applications in just a few steps.