How to block users based on their country

Blocking users based on their country is a very common use case. In this tutorial, we are going to look at an example, where we restrict certain content from a list of countries.


In this example, we are specifically blocking users from accessing a website. It is worth mentioning that this example specifically aims to block users that have no association to us or our servers (e.g. a user account). Typically, we are talking about website visitors.

Writing the code

We are sending a GET request to the ipbase IP info endpoint, which provides us with the associated country information. In fact, the infoendpoint (Read more) provides an entire data object that is dedicated towards providing country-specific information (including currency, timezones, etc.). For our use case, the alpha2 code, which represents the ISO 3166 ALPHA-2 standard, is most sufficient.

That being said, we are checking the user client's IP address for it's alpha2-country code against a list of countries. If the user's country is part of the list, he is being blocked from the content. If not, he is allowed to see the content. Instead of actually blocking the users, we are working with "you are blocked" or "you can see this messages" messages as examples.

import Ipbase from '@everapi/ipbase-js'

const blockedCountries = ['DE', 'AT', 'ES', 'CH']
const ipBase = new Ipbase('Gvq8SwM11WP0qfgJEczrlFZknvmMfY2jzIacnAe5')
var userCountry = await => {

if(blockedCountries.includes(userCountry)) {
    console.log('You are blocked');
} else {
    console.log('You can see this');

There are plenty of use cases where you can exclude users because of their country of origin. For example, some companies want to exclude users that are from the European Union due to GDPR issues.

Register your API key to get started, here and enter the API key in the example above.