Redirecting Users by Country

A common use-case for geolocation is to redirect users based on their country. With Ipregistry you can, for example, redirect a user to a country-specific store or Web page of your international Website:

const MAPPINGS = {
    FR: 'https://fr.store.acme.com',
    DE: 'https://de.store.acme.com',
    US: 'https://us.store.acme.com',
};

fetch('https://api.ipregistry.co/?key=tryout')
    .then(function(response) { return response.json(); })
    .then(function(payload) {
        const userCountryCode = payload['location']['country']['code'];
        let redirectionUrl = MAPPINGS[userCountryCode];

        if (!redirectionUrl) {
            redirectionUrl = 'https://store.acme.com'
        }

        window.location.href = redirectionUrl;
    }).catch(function (error) {
    	console.error("Handle errors here", error);
    });

The code snippet above redirects users from France to https://fr.store.acme.com, users from Germany to https://de.store.acme.com and users from the United States of America to https://us.store.acme.com. Otherwise, the redirection fallbacks to https://store.acme.com.

You can easily find the ISO 3166-1 alpha-2 code associated with a country on Wikipedia.