Beginner 10 minutes
Get Your IP Address and Location
Learn how IP geolocation works by building a simple tool that finds your location from your IP address.
What You'll Build
A script that detects your IP address and displays your approximate geographic location, timezone, and ISP.
Prerequisites
- ✓ Basic JavaScript knowledge
1
Get your API key
Sign up at ipgeolocation.io to get a free API key. The free tier allows 1,000 requests per day.
2
Fetch your location
Call the API without specifying an IP to auto-detect your own location.
javascript
const response = await fetch(
'https://api.ipgeolocation.io/ipgeo?apiKey=YOUR_API_KEY'
);
const data = await response.json();
console.log(`IP: ${data.ip}`);
console.log(`Location: ${data.city}, ${data.state_prov}, ${data.country_name}`);
console.log(`Timezone: ${data.time_zone.name}`);
console.log(`ISP: ${data.isp}`);
console.log(`Coordinates: ${data.latitude}, ${data.longitude}`); 3
Look up any IP address
Pass a specific IP address as a parameter to look up the location of any IP.
javascript
async function lookupIP(ip) {
const response = await fetch(
`https://api.ipgeolocation.io/ipgeo?apiKey=YOUR_API_KEY&ip=${ip}`
);
const data = await response.json();
return {
ip: data.ip,
city: data.city,
country: data.country_name,
lat: data.latitude,
lon: data.longitude,
timezone: data.time_zone.name
};
}
// Look up Google's DNS server
const location = await lookupIP('8.8.8.8');
console.log(location); Next Steps
- → Display the location on a map using Leaflet.js
- → Combine with weather API to show local weather
- → Build an IP lookup tool with a web form