Show personalized content to you users based on their IP geolocation, with a small, fast and globally distributed javascript file.
<script defer src="https://geoloc-red.vercel.app/api/js"></script>
<script>
document.addEventListener("geoloc-loaded", () => {
console.log(geoloc.country); // Will return FR
});
</script>
Lightweight: less than 0.3kb uncompressed
Super Fast: Distribued worldwide, your users will be served from the nearest edge server
Quick Setup: 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": "2.2574",
"latitude": "48.8954",
"country": "FR",
"region": "Île-de-France",
"timezone": "Europe/Paris",
"asOrganization": "BOUYGUES Telecom"
}
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 FR
});
</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();
}
GET /api/js
GET /api/json
GET /api/ip