Reliable IP Geolocation with Superface
Tags: javascript, api, geolocation, node
Published: 2022-03-21 08:43:54


This blog post will help you understand what is IP Geolocation, and how can Superface help with the reliability of selected IP Geolocation providers.

## What is IP Geolocation?

IP Geolocation is a way to obtain the approximate location, time zone, or country of a computer, or mobile device connected to the internet. IP Geolocation uses IPv4 or IPv6 addresses to get the geolocation data.

IP Geolocation accuracy is limited and gives you very raw information about the location of the device. For example, ipgeolocation.io provider is 99% accurate at the country level and around 75% accurate at the city level according to their [FAQ](https://ipgeolocation.io/faq.html).

Anyway, the advantage of IP Geolocation is that you do not need the user’s consent to get the location compared to methods like [HTML5 Geolocation API](https://www.w3.org/TR/2016/REC-geolocation-API-20161108/).

There are various use-cases where you can take advantage of IP Geolocation:

- Fraud prevention by alerting users of suspicious activity around login or purchases.
- Offer customers customized products, for example, online lessons for distant customers and offline lessons for those living in your place.
- Display relevant business opening hours to your customers.
- Redirect users to the localized page in a language they understand.

## The pitfalls of integrating third party providers

When you decide to use IP Geolocation in your project, you can choose whatever IP Geolocation provider you find on the market and integrate with it. But this integration has its drawbacks:

- Your project will gain a dependency on a specific provider API. This dependency might not be easy to change in the future.
- The provider becomes a single point of failure of your project. When the provider’s service is not working your service functionality will be limited or not working at all.

This is where Superface strengths can be utilized. [Superface OneSDK](https://github.com/superfaceai/one-sdk-js) helps with securing resiliency of your Node.js service and losing the dependency on concrete Geolocation API provider.

## IP Geolocation providers compared

Currently you can use these providers with Superface (if you miss your favorite one, [let us know](https://superface.ai/docs/support)):

### [ipgeolocation](https://ipgeolocation.io/)

- free plan with 1K free requests per day
- [pricing details](https://ipgeolocation.io/pricing.html)

### [ipbase](https://ipbase.com/)

- free plan with 150 requests per month
- [pricing details](https://ipbase.com/pricing/)

### [ipdata](https://ipdata.co/)

- free plan with 1.5K requests per day
- free plan is restricted to non-commercial use
- [pricing details](https://ipdata.co/pricing.html)

### [ipstack](https://ipstack.com/)

- free plan with 100 requests per month
- ssl encryption and timezone not available in free plan
- [pricing details](https://ipdata.co/pricing.html)

### [FreeGeoIp](https://freegeoip.app/)

- FreeGeoIp has changed its name to ipbase
- FreeGeoIp is still available in Superface catalog for backward compatibility

## Integrate IP Geolocation provider using Superface OneSDK

Follow these steps to integrate with `ipgeolocation` provider in your Node.js application using the Superface OneSDK:

1. In the root folder of your project install `address/ip-geolocation` profile.

    ```console
    npx @superfaceai/cli install address/ip-geolocation
    ```

1. Configure `ipgeolocation` provider.

    ```shell
    npx @superfaceai/cli configure ipgeolocation -p address/ip-geolocation
    ```

1. Install Superface OneSDK package.

    ```shell
    npm install @superfaceai/one-sdk
    ```

1. Use the following code in your Node.js application:

    ```js
    const { SuperfaceClient } = require('@superfaceai/one-sdk');

    const sdk = new SuperfaceClient();

    async function getIPGeolocation(ipAddress) {
      // Load the installed profile
      const profile = await sdk.getProfile('address/ip-geolocation');

      // Use the profile
      const result = await profile.getUseCase('IpGeolocation').perform({
        ipAddress,
      });
 
      return result.unwrap();
    }
    ```

1. Get a free API key by registering on [ipgeolocation.io](https://ipgeolocation.io/) web. Then set the key as an environment variable `IPGEOLOCATION_API_KEY`:

    ```shell
    export IPGEOLOCATION_API_KEY=<your-key-from-ipgeolocation>
    ```

You can find the instructions for configuring other providers in the [Superface registry](https://superface.ai/address/ip-geolocation).

Once you install at least two IP Geolocation providers and provide valid access keys you can enable [automatic failover](https://superface.ai/docs/guides/using-multiple-providers#failover) between providers.

### Use it

Call `getIPGeolocation` function with IP address argument in your code and you will receive geolocation data in the response. Example result data:

```json
{
  "ipAddress": "8.8.8.8",
  "addressCountryCode": "US",
  "addressCountry": "United States",
  "timeZone": "America/Chicago",
  "latitude": 37.751,
  "longitude": -97.822
}
```

## Relevant Superface guides

- [Getting started](https://superface.ai/docs/getting-started)
- [Setting provider API keys](https://superface.ai/docs/guides/api-keys)
- [Using multiple providers](https://superface.ai/docs/guides/using-multiple-providers)