Need to determine a user's location from their IP address? This guide covers everything about IP geolocation via API, including accuracy considerations, data available, and implementation examples in multiple programming languages.
What is IP Geolocation?
IP geolocation is the process of determining the geographic location of an IP address. It uses databases that map IP ranges to physical locations based on data from ISPs, regional internet registries, and other sources.
Example: IP 8.8.8.8 maps to Mountain View, California, United States (Google DNS)
Available Geolocation Data
Typical data returned by geolocation services:
Location Data
Country, region/state, city, postal code, and latitude/longitude coordinates.
Network Data
ISP name, organization, and autonomous system number (ASN).
Additional Data
Timezone, currency, and language preferences for the region.
Using the IP Geolocation API
TinyFn provides a simple endpoint to get location from IP:
GET https://api.tinyfn.io/v1/geo/ip?ip=8.8.8.8
Headers: X-API-Key: your-api-key
{
"ip": "8.8.8.8",
"country": "United States",
"country_code": "US",
"region": "California",
"city": "Mountain View",
"postal_code": "94035",
"latitude": 37.386,
"longitude": -122.0838,
"timezone": "America/Los_Angeles",
"isp": "Google LLC"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
ip |
string | The IP address to lookup (required) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/geo/ip?ip=8.8.8.8',
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const location = await response.json();
console.log(`${location.city}, ${location.country}`);
// Mountain View, United States
Python
import requests
response = requests.get(
'https://api.tinyfn.io/v1/geo/ip',
params={'ip': '8.8.8.8'},
headers={'X-API-Key': 'your-api-key'}
)
location = response.json()
print(f"{location['city']}, {location['country']}")
# Mountain View, United States
cURL
curl "https://api.tinyfn.io/v1/geo/ip?ip=8.8.8.8" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Content Localization: Show content in user's language or currency
- Fraud Detection: Flag suspicious activity from unexpected locations
- Analytics: Understand geographic distribution of users
- Compliance: Enforce geographic restrictions (GDPR, licensing)
- Personalization: Show local weather, news, or store locations
Best Practices
- Cache results: IP locations change infrequently; cache for performance
- Handle failures: Have fallback behavior when geolocation fails
- Respect privacy: Inform users about geolocation and provide opt-out
- Don't over-rely: Use geolocation as a hint, not absolute truth
Try the IP Geolocation API
Get your free API key and start looking up IP locations in seconds.
Get Free API Key