IPBot
Get Started
Back to Blog
| Guides

What Is IP Geolocation? Complete Guide for Developers

Learn what IP geolocation is, how it works, accuracy levels, use cases like fraud prevention and content localization, and how to integrate IP geolocation APIs.

IP GeolocationAPIDeveloper GuideGeolocation

IP geolocation is the process of mapping an IP address to a physical location on Earth. It’s a fundamental technology powering everything from fraud detection to content localization. This guide explains how IP geolocation works, its accuracy limitations, and practical implementation strategies.

What Is IP Geolocation?

IP geolocation is the practice of determining the physical geographic location of a device connected to the internet using its IP address. Unlike GPS, which uses satellite signals, IP geolocation works by mapping IP addresses to locations stored in databases.

How IP Geolocation Works

  1. IP Allocation: Regional Internet Registries (RIRs) allocate IP address blocks to organizations
  2. Database Building: Geolocation providers map IP blocks to geographic locations
  3. Lookup: When queried, the API returns location data from its database

IP Geolocation Data Points

A typical IP geolocation API response includes:

Data PointDescriptionTypical Accuracy
CountryISO country code and name99%+
RegionState, province, or region90-95%
CityCity name50-80%
Postal CodeZIP or postal code40-70%
CoordinatesLatitude and longitude50-150km radius
TimezoneLocal timezone95%+
ISP/OrgInternet Service Provider99%+

Understanding IP Geolocation Accuracy

Country Level (99%+ accuracy)

Most reliable level. ISPs are assigned IP blocks by country, making this highly accurate.

City Level (50-80% accuracy)

Less precise due to:

  • ISPs routing through regional hubs
  • Mobile networks showing tower locations
  • Corporate networks using centralized IPs

Precise Location

IP geolocation cannot provide precise location (street address). This is a technical limitation, not a privacy feature. IPs are allocated regionally, not to specific addresses.

IP Geolocation Use Cases

1. Fraud Prevention

// Detect suspicious logins from unusual locations
const currentLocation = await getIPLocation(userIP);
const knownLocation = getUserKnownLocation(userId);
if (distance(currentLocation, knownLocation) > 1000) {
flagForVerification();
}

2. Content Localization

// Show localized content based on country
const country = await getIPCountry(requestIP);
if (country === "US") {
showPricesInUSD();
} else if (country === "GB") {
showPricesInGBP();
}

3. Geo-blocking

// Restrict content by region
const restrictedCountries = ["CN", "RU", "KP"];
const userCountry = await getIPCountry(requestIP);
if (restrictedCountries.includes(userCountry)) {
return new Response("Content not available in your region", { status: 403 });
}

4. Analytics

Track visitor demographics to:

  • Optimize CDN locations
  • Understand market penetration
  • Measure campaign effectiveness by region

How to Implement IP Geolocation

Using the IPBot API

Terminal window
# Lookup your own IP
curl https://api.ipbot.com/
# Lookup a specific IP
curl https://api.ipbot.com/8.8.8.8

JavaScript Example

async function getUserLocation(ip) {
const response = await fetch(`https://api.ipbot.com/${ip}`);
const data = await response.json();
return {
country: data.country_code,
countryName: data.country_name,
region: data.region,
city: data.city,
timezone: data.timezone,
isp: data.isp,
};
}

Python Example

import requests
def get_ip_location(ip=""):
"""Get geolocation data for an IP address"""
response = requests.get(f"https://api.ipbot.com/{ip}")
return response.json()
# Example usage
location = get_ip_location("8.8.8.8")
print(f"Location: {location['city']}, {location['country_name']}")

IP Geolocation vs Other Location Methods

MethodAccuracyBattery ImpactPermission Required
IP GeolocationCity-levelNoneNo
GPSPreciseHighYes
WiFi Triangulation20-50mMediumYes
Cell Tower100-1000mLowYes

Limitations and Considerations

Privacy Implications

IP geolocation doesn’t reveal precise location, but it can identify:

  • User’s city and region
  • ISP and potentially employer
  • Connection type (mobile, broadband, corporate)

Dynamic IPs

Many residential IP addresses are dynamic and can change:

  • Upon router restart
  • Periodically (daily, weekly, monthly)
  • When switching ISPs

VPNs and Proxies

Users can mask their real location:

  • VPNs: Route traffic through servers in other countries
  • Proxies: Similar to VPNs but typically less secure
  • Tor: Routes through multiple nodes for anonymity

IPBot includes VPN/proxy detection to help identify masked locations.

Best Practices for IP Geolocation

1. Handle Uncertainty Gracefully

// Don't assume location data is perfectly accurate
const location = await getIPLocation(ip);
// Always allow manual override
showLocation detected: ${location.city}, ${location.country}
<a href="/change-location">Change location</a>

2. Cache Results

// IP location doesn't change frequently
const cache = new Map();
const CACHE_TTL = 24 * 60 * 60 * 1000; // 24 hours
async function getCachedLocation(ip) {
const cached = cache.get(ip);
if (cached && Date.now() - cached.timestamp < CACHE_TTL) {
return cached.data;
}
const location = await getIPLocation(ip);
cache.set(ip, { data: location, timestamp: Date.now() });
return location;
}

3. Respect User Privacy

// Always allow opt-out
if (userHasLocationConsent()) {
const location = await getIPLocation(ip);
applyLocationPersonalization(location);
}

4. Use Appropriate Accuracy Levels

// Country-level is often sufficient
const country = await getIPCountry(ip);
if (needsHighAccuracy) {
// Only fetch city data when needed
const city = await getIPCity(ip);
}

Common Questions

Can IP geolocation find my exact address?

No. IP geolocation cannot pinpoint a specific address. The best it can do is approximate to a city level, often with a margin of error of tens of miles.

Why does my IP show the wrong city?

This happens when:

  • Your ISP routes traffic through a regional hub
  • You’re on a mobile network (shows tower location)
  • The geolocation database has outdated information
  • You’re using a VPN or proxy

How accurate is free IP geolocation?

Free services like IPBot provide country-level accuracy of 99%+. City-level accuracy varies (50-80%), which is sufficient for most use cases like content localization and fraud detection.