IP Geolocation API JQuery SDK

In this document, you will go through the basic steps to use IP Geolocation API JQuery SDK.
You need a valid 'IPGeolocation API key' to use this SDK. Sign up here and get your free API key if you don?t have one.

System Requirements

Internet connection is required to run this component.

Installation

CDN Link

Add the following script in your HTML page:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/ipgeolocation.min.js"></script>

Geolocation Lookup

There are four ways to query geolocation from IPGeolocation API. You can use the following functions to get the geolocation as you require.

// Query geolocation for the calling machine's IP address
ipgeoByApikey('YOUR_API_KEY');

// Query only specific geolocation fields e.g., 'country_code2,time_zone,currency' for the calling machine's IP address
ipgeoByApikeyAndFields('YOUR_API_KEY', 'country_code2,time_zone,currency');

// Query geolocation for an IP address e.g., '1.1.1.1'
ipgeoByApikeyAndIp('YOUR_API_KEY', '1.1.1.1');

// Query only specific geolocation fields e.g., 'country_code2,time_zone,currency' for an IP address e.g., '1.1.1.1'
ipgeoByApikeyFieldsAndIp('YOUR_API_KEY', 'geo,time_zone,currency', '1.1.1.1');

Bulk Geolocations Lookup

To query geolocation for multiple IP addresses, you can call the following function with your API key and list of IP addresses.
Note: This function can only be used for paid subscriptions.

// Query geolocation for multiple IP addresses
ipgeoByApikeyAndIps('YOUR_API_KEY', ['1.1.1.1','2.2.2.2','3.3.3.3']);

Time Zone API

You can also query time zone information in four different ways. You can use the following functions to get the time zone information as you require.

// Query time zone information for the calling machine's IP address
timezoneByApikey('YOUR_API_KEY');

// Query time zone information for an IP address e.g., '1.1.1.1'
timezoneByApikeyAndIp('YOUR_API_KEY', '1.1.1.1');

// Query time zone infomration for a time zone ID like 'America/New_York'
timezoneByApikeyAndTimezone('YOUR_API_KEY', 'America/New_York');

// Query time zone information by latitude and longitude of the location
timezoneByApikeyLatitudeAndLongitude('YOUR_API_KEY', '31.4816', '74.3551')

Example

Here is a sample code to use IP Geolocation API using JQuery SDK:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/ipgeolocation.min.js"></script>

<script>
    var ip = sessionStorage.getItem('ip');
    var country_name = sessionStorage.getItem('country_name');
    var country_code2 = sessionStorage.getItem('country_code2');
            
    if (!ip || !country_name || !country_code2) {
        var json = ipgeoByApikeyAndFields('PUT_YOUR_API_KEY_HERE', 'country_name,country_code2');
        ip = json.ip;
        country_name = json.country_name;
        country_code2 = json.country_code2;
                
        sessionStorage.setItem('ip', ip);
        sessionStorage.setItem('country_name', country_name);
        sessionStorage.setItem('country_code2', country_code2);
    }
                
    $(document).ready(function() {
        alert('Hello ' + country_name + '!');		
    });
</script>

Source Code

The complete source code for this SDK is avaliable at Github.