A Developer's Journey to Seamless Country Identification
As a backend developer, I recently encountered a common yet frustrating roadblock: reliably determining a user's country of origin from their IP address. I needed a lightweight, accurate, and fast solution for a new localization feature. After spending hours wrestling with bloated SDKs and outdated local databases, I stumbled upon a simple GitHub repository that completely streamlined my workflow.
The Discovery
While searching for a clean and efficient approach, I discovered the fast-ip-geolocation-integration repository on GitHub:
https://github.com/lunar-echo-5433/fast-ip-geolocation-integration.
It offered exactly what I was looking for: a simple Python CLI tool designed to retrieve the country, latitude, and longitude of any IP address using the Fast IP Geolocation & Country Lookup API on RapidAPI.
Quick Setup and Prerequisites
The beauty of this solution lies in its simplicity. The repository’s documentation outlines a very straightforward setup process:
- Environment: It requires Python 3.6+ and the widely used
requestslibrary. - Authentication: You simply sign up on RapidAPI and subscribe to the API. It is incredibly developer-friendly, offering 1000 free requests per month.
- Credentials: Once subscribed, you receive an
x-rapidapi-keyto authenticate your requests.
Diving Into the Code
The repository contains a beautifully written Python script (ip_geolocation.py). It operates by making a GET request to the API endpoint. It seamlessly handles authentication by passing the x-rapidapi-key and x-rapidapi-host headers.
import requests
import os
import sys
def get_geolocation(ip_address, api_key):
url = "[https://fast-ip-geolocation-country-lookup.p.rapidapi.com/v1/lookup](https://fast-ip-geolocation-country-lookup.p.rapidapi.com/v1/lookup)"
querystring = {"ip": ip_address}
headers = {
"x-rapidapi-key": api_key,
"x-rapidapi-host": "fast-ip-geolocation-country-lookup.p.rapidapi.com"
}
try:
response = requests.get(url, headers=headers, params=querystring)
response.raise_for_status()
data = response.json()
country = data.get("country_name")
# Extracts latitude and longitude as well
As shown above, it successfully parses the JSON response to extract the country_name and location (latitude and longitude) without any complex parsing logic.
Running the Tool
The script acts as a versatile command-line interface. You can pass your RapidAPI key securely via an environment variable or directly as a command-line argument. Here is how I test IP addresses directly from my terminal:
export RAPIDAPI_KEY="your_api_key_here"
python ip_geolocation.py 8.8.8.8
Example Output Received:
IP Address: 8.8.8.8
Country: United States
Latitude: 37.386
Longitude: -122.0838
Conclusion
This integration saved me hours of development time. It handles both IPv4 and IPv6 addresses effortlessly and integrates cleanly into any Python backend. If you are a developer struggling with IP geolocation, I highly recommend giving this sample code a try. It is proof that sometimes, the most effective solutions are the simplest ones.
Top comments (0)