How to track IP address of website visitors?


Do you run a website and want to change the content based on the location of the visitors? Or, do you want to access security threats coming from risky IP addresses? You can do all these things using ipstack API.

ipstack.com offers a powerful, real-time IP to geolocation API capable of looking up accurate location data and assessing security threats originating from risky IP addresses. Results are delivered within milliseconds in JSON or XML format. Using the ipstack API you will be able to locate website visitors at first glance and adjust your user experience and application accordingly.

The ipstack API is being used by thousands of developers and corporates worldwide to know where the visitors are coming from and change the content based on their location.

The ipstack database and API is integrated with many large ISPs worldwide to get the latest information about new and existing IP ranges and so provides accurate information. Ipstack is integrated with multiple channels delivering real-time IP data, which is why the database used by the ipstack API is updated regularly, with up to 24 database updates per day

Let's see how to use ipstack API to get the information about the website visitors using jQuery.

In order to call the ipstack API and get the IP address and other information about the visitors, you need to have an API key. For trial purpose, you can get it free by sign up with their free plan from here.

ipstack Sign-up

Once you sign up, you will get your free API access key, as shown below.

API Access Key

You can use your API access keys in three ways:

  • Standard Lookup: Look up the data behind an IP address.
  • Bulk Lookup: Look up the data behind multiple IP addresses at once.
  • Requester Lookup: Look up the data behind the IP address your API request is coming from.

The base URL will be http://api.ipstack.com.

For the standard lookup, send request to http://api.ipstack.com/<ipaddress>?access_key=<your access key>

For the bulk lookup, send request to http://api.ipstack.com/<ip address1, ip address2, ip address3>?access_key=<your access key>

For requester lookup, send request http://api.ipstack.com/check?access_key=<your access key>

Let's test requester lookup.

Open your browser and enter http://api.ipstack.com/check?access_key=XXX. Replace XXX with your API access key. You will get the JSON response as shown below.

Requester Lookup

Using jQuery

The following example demonstrates the standard lookup request using jQuery.

Example: Standard Lookup using jQuery
// set endpoint and your access key
var ip = '134.201.250.155'
var access_key = 'YOUR_ACCESS_KEY';

// get the API result via jQuery.ajax
$.ajax({
    url: 'http://api.ipstack.com/' + ip + '?access_key=' + access_key,   
    dataType: 'jsonp',
    success: function(json) {

        // output the "capital" object inside "location"
        alert(json.location.capital);
        
    }
});

Upon executing the above request, you will get the following JSON response.

{
        "ip": "134.201.250.155",
        "hostname": "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": "90013",
        "latitude": 34.0453,
        "longitude": -118.2413,
        "location": {
        "geoname_id": 5368361,
        "capital": "Washington D.C.",
        "languages": [
        {
            "code": "en",
            "name": "English",
            "native": "English"
        }
    ],
        "country_flag": "https://assets.ipstack.com/images/assets/flags_svg/us.svg",
        "country_flag_emoji": "🇺🇸",
        "country_flag_emoji_unicode": "U+1F1FA U+1F1F8",
        "calling_code": "1",
        "is_eu": false
    },
        "time_zone": {
        "id": "America/Los_Angeles",
        "current_time": "2018-03-29T07:35:08-07:00",
        "gmt_offset": -25200,
        "code": "PDT",
        "is_daylight_saving": true
    },
        "currency": {
        "code": "USD",
        "name": "US Dollar",
        "plural": "US dollars",
        "symbol": "$",
        "symbol_native": "$"
    },
        "connection": {
        "asn": 25876,
        "isp": "Los Angeles Department of Water & Power"
    },
        "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
    }
}

Thus, you can use ipstack API to know visitors IP addresses easily and quickly.

Visit ipstack documentation for detailed information.