DEV Community

Jan Halama for Superface

Posted on • Updated on • Originally published at superface.ai

Reliable IP Geolocation with Superface

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.

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.

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 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):

ipgeolocation

ipbase

ipdata

  • free plan with 1.5K requests per day
  • free plan is restricted to non-commercial use
  • pricing details

ipstack

  • free plan with 100 requests per month
  • ssl encryption and timezone not available in free plan
  • pricing details

FreeGeoIp

  • 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.

    npx @superfaceai/cli install address/ip-geolocation
    
  2. Configure ipgeolocation provider.

    npx @superfaceai/cli configure ipgeolocation -p address/ip-geolocation
    
  3. Install Superface OneSDK package.

    npm install @superfaceai/one-sdk
    
  4. Use the following code in your Node.js application:

    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();
    }
    
  5. Get a free API key by registering on ipgeolocation.io web. Then set the key as an environment variable IPGEOLOCATION_API_KEY:

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

You can find the instructions for configuring other providers in the Superface registry.

Once you install at least two IP Geolocation providers and provide valid access keys you can enable automatic 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:

{
  "ipAddress": "8.8.8.8",
  "addressCountryCode": "US",
  "addressCountry": "United States",
  "timeZone": "America/Chicago",
  "latitude": 37.751,
  "longitude": -97.822
}
Enter fullscreen mode Exit fullscreen mode

Relevant Superface guides

Top comments (0)