Show personalized content to your users based on their IP geolocation, with a small, fast and globally distributed javascript file.
Get Started for freeContribute<script defer src="https://geoloc-red.vercel.app/api/js"></script>
<script>
document.addEventListener('geoloc-loaded', () => {
console.log(geoloc.country) // Will return EN
});
</script>
less than 0.3kb uncompressed.
Distribued worldwide, your users will be served from the nearest edge server.
Zero configuration, you don't need an API key, just add the script tag, and you are ready!
Geoloc is a service that provides geolocation information about the current user using a lightweight javascript file. Any page that has the script tag in the DOM will have access to a global `geoloc` object containing the following informations :
{
"longitude": "-80.484",
"latitude": "27.0614",
"country": "US",
"region": "Florida",
"timezone": "America/New_York",
"asOrganization": "Verizon"
}
You can load the script asynchronously that it will have little to no impact on your page load performance, and you can bind an event to the document to execute a function as soon as the geolocation information is available.
<!-- Load the script tag with either defer or async attribute -->
<script defer src="https://geoloc-red.vercel.app/api/js"></script>
<!-- Wait for the 'geoloc-loaded' event before using the global 'geoloc' variable -->
<script>
document.addEventListener('geoloc-loaded', () => {
console.log(geoloc.country) // Will return EN
});
</script>
If you don't need the geolocation information to be available globally and only want to fetch it on-demand, you can use the /json endpoint, see example below.
async function getGeolocInfo(){
const response = await fetch('https://geoloc-red.vercel.app/api/json');
return await response.json();
}
This '/json' endpoint cannot be used on server-side as it will return the informations about the server's IP address.