Goal: Know Where Your Users Are
Understanding the geographic location of an IP address is fundamental for many applications. Whether you need to comply with data sovereignty laws, customize user experiences, or perform risk analysis, precise geolocation data is critical.
Accessing Geolocation Data
The GET /intelligence/v1/ip/{address}
endpoint provides a rich geolocation
object in its JSON response.
When you query an IP, you'll receive the following key fields:
geolocation.country
: Contains the country'sname
and standardiso_code
(e.g., "US").geolocation.city
: Provides thename
of the city.geolocation.coordinates
: Gives you the preciselatitude
andlongitude
, along with anaccuracy_radius
in kilometers.geolocation.postal_code
: The postal code for the location.geolocation.time_zone
: The IANA time zone name (e.g., "America/Los_Angeles").
Common Applications
Here are a few ways you can use this data:
- Data Compliance: Before storing user data, check the IP's
country.iso_code
. If it's in a region covered by GDPR or another data privacy law, you can trigger the appropriate consent mechanisms or data handling workflows. - Content Localization: Customize your website or application by reading the
country.iso_code
. You can automatically set the correct language, currency, and display region-specific content. - Fraud Prevention: In e-commerce, you can compare the IP's location with the billing or shipping address provided by the user. A significant mismatch could be a flag for a fraudulent transaction.
- Analytics: Enhance your user analytics by mapping usage patterns to geographic locations, helping you understand your audience and market penetration.
Python Code Example
This code snippet demonstrates how to fetch and display key geolocation data for an IP address using the Python SDK.
import noctis_frontgraph_sdk
from noctis_frontgraph_sdk.rest import ApiException
import os
# Configure authentication
configuration = noctis_frontgraph_sdk.Configuration(access_token = os.environ["BEARER_TOKEN"])
with noctis_frontgraph_sdk.ApiClient(configuration) as api_client:
api_instance = noctis_frontgraph_sdk.IntelligenceServicesApi(api_client)
ip_address = '8.8.8.8'
try:
# Get comprehensive IP address intelligence
api_response = api_instance.get_ip_intelligence(ip_address)
# Extract and print the geolocation object
geo_data = api_response.geolocation
print(f"Geolocation data for IP: {ip_address}")
print(f" Country: {geo_data.country.name} ({geo_data.country.iso_code})")
print(f" City: {geo_data.city.name}")
print(f" Coordinates: Lat {geo_data.coordinates.latitude}, Lon {geo_data.coordinates.longitude}")
print(f" Time Zone: {geo_data.coordinates.time_zone}")
except ApiException as e:
print("Exception when calling IntelligenceServicesApi->get_ip_intelligence: %s\n" % e)