How to block users by country

We'll use the Ipdata API to locate the user by their IP address then determine if they're in a country or continent blacklist.

By Country

This example shows you how to ensure your content is not distributed to certain countries.


// The API returns the ISO ALPHA-2 Country Code

// List of countries we want to block
// To see this in action add your country code to the array
var blacklist = ['US', 'CA', 'UK']

// Getting the country code from the user's IP
$.get("https://api.ipdata.co", function (response) {

  // Checking if the user's country code is in the blacklist
  // You could inverse the logic here to use a whitelist instead
  if (blacklist.includes(response.country_code)) {
    alert('This content is not available at your location.');
  }
}, "jsonp");

By Continent

This example shows you how to restrict consumption of your content to certain continents.


// The API returns the two letter continent code.

// List of countries we want to block
var whitelist = ['AS', 'SA']

// Getting the continent code from the user's IP
$.get("https://api.ipdata.co", function (response) {

  // Checking if the user's country code is in the whitelist
  if (whitelist.includes(response.country_code)) {
    alert('This content is not available at your location.');
  }
}, "jsonp");

The above can be implemented in server side and in multiple languages. For examples on how to recreate the above example in other languages see our Documentation.


All Continent codes
Code Continent name
AF Africa
AN Antarctica
AS Asia
EU Europe
NA North america
OC Oceania
SA South america