GeoIP API
✨ A self-hosted IP geolocation API and Python package that works completely offline! 🚀
Powered by MaxMind's GeoLite2 databases ツ
✨ Features
- 🌍 Fast and reliable IP geolocation lookups
- 🔒 Self-hosted solution with no external API dependencies
- 🛠️ Dual functionality: Python package and REST API
- 🐳 Easy deployment with Docker and cloud platforms
- 📊 Get country, city, coordinates, timezone, ISP, and ASN data
- 🎨 Beautiful, interactive demo UI for testing
- 🚀 Built with FastAPI for high performance
- 📦 Automatic GeoLite2 database downloads and updates
🛠️ Usage
Python Package
Installation
pip install geoip-py
Basic Usage
from geoip_api import GeoIPLookup
# Initialize the lookup service (downloads DB files if needed)
lookup = GeoIPLookup(download_if_missing=True)
# Look up an IP address
result = lookup.lookup('8.8.8.8')
print(result)
# Output:
# {
# "code": "US",
# "country": "United States",
# "city": "Mountain View",
# "lat": 37.4056,
# "lon": -122.0775,
# "tz": "America/Los_Angeles",
# "isp": "Google LLC",
# "asn": 15169
# }
REST API
✅ Demo : https://geoip-api.malith.dev/
Simple Endpoints
# Simple path parameter
https://your-domain.com/8.8.8.8
# Query parameter
https://your-domain.com/?ip=8.8.8.8
Standard API Endpoint
https://your-domain.com/api/v1/geoip/lookup/8.8.8.8
Response Format
{
"ip": "8.8.8.8",
"code": "US",
"country": "United States",
"city": "Mountain View",
"lat": 37.4056,
"lon": -122.0775,
"tz": "America/Los_Angeles",
"isp": "Google LLC",
"asn": 15169
}
📦 Deployment Options
🚀 Cloud Deployment
One-click deployment to popular platforms:
🐳 Docker
The fastest way to deploy your own GeoIP API:
docker pull malithrukshan/geoip-api
docker run -p 8000:8000 malithrukshan/geoip-api
Your API will be available at http://localhost:8000
Docker Compose
Create a docker-compose.yml file:
version: '3.8'
services:
geoip-api:
image: malithrukshan/geoip-api
ports:
- "8000:8000"
# volumes:
# - ./data:/app/api/db
environment:
- ENVIRONMENT=production
Then run:
docker-compose up -d
🔨 Building Docker Image Locally
If you want to build and run the Docker image from source:
- Clone the repository
git clone https://github.com/Malith-Rukshan/geoip-api.git cd geoip-api
- Build the Docker image
docker build -t geoip-api .
- Run the container
docker run -p 8000:8000 geoip-api
- Access the API at http://localhost:8000
💻 Local Development
Prerequisites
- Python 3.9+
- pip
Setup
-
Clone the repository
git clone https://github.com/Malith-Rukshan/geoip-api.git cd geoip-api
-
Install dependencies
pip install -r requirements/dev.txt pip install -e .
-
Download GeoIP databases (optional, will be downloaded automatically if needed)
mkdir -p ~/.geoip_api curl -L -o ~/.geoip_api/GeoLite2-City.mmdb https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-City.mmdb curl -L -o ~/.geoip_api/GeoLite2-ASN.mmdb https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-ASN.mmdb
-
Run the FastAPI application
uvicorn api.main:app --reload
-
Visit http://localhost:8000 to see the API in action
Running Tests
pytest
black --check src tests api
mypy src tests api
🌐 Use Cases
- Security & Compliance: Enhance security systems with IP-based threat detection while maintaining data sovereignty
- Content Localization: Deliver region-specific content based on visitor location without sharing user data
- Analytics: Analyze traffic patterns and user demographics with geographic data that remains within your infrastructure
- Fraud Prevention: Identify suspicious login attempts based on geographic anomalies
- Development Environment: Use a local GeoIP service in your development environment without external API dependencies
📜 License
This project is licensed under the MIT License - see the LICENSE file for details.
⚠️ Database License Notice
This project uses GeoLite2 data created by MaxMind, available from https://www.maxmind.com. The GeoLite2 databases are licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
🔧 Acknowledgements
- GeoLite2 databases provided by MaxMind
- Mirror of GeoLite2 databases maintained by P3TERX
- Built with FastAPI and Python
- Powered by geoip2 library
🇺🇳 Flags By : Animated Country Flags
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
🌟 Support and Community
If you found this project helpful, please give it a ⭐ on GitHub. This helps more developers discover the project! 🫶
📬 Contact
If you have any questions, feedback, or just want to say hi, you can reach out to me:
- Email: hello@malith.dev
- GitHub: @Malith-Rukshan
🧑💻 Built with 💖 by Malith Rukshan
Metadata
Release files for geoip-py 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| geoip_py-1.0.2.tar.gz | 13.3 kB | Without provenance | Details for geoip_py-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| geoip_py-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for geoip_py-1.0.2-py3-none-any.whl |
Total release size: 24.8 kB
Release files / geoip_py-1.0.2.tar.gz
| Download URL | geoip_py-1.0.2.tar.gz |
|---|---|
| Size | 13.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
55a33866ef7360ab55fe792689ee22c4a2c362554c4662a7045c26b04ea08f4d
|
|
BLAKE2b-256 checksum How to use checksums |
66d27fe129ecb842b857c8b56a65fb48a984faeaa070fecf4df21ad57d976643
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / geoip_py-1.0.2-py3-none-any.whl
| Download URL | geoip_py-1.0.2-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e161360a34c23a998d7c6133d586af3e85e108594e1816549fdb70b701acfebd
|
|
BLAKE2b-256 checksum How to use checksums |
f12432a63af5720f81d86be487fa9f842cd3974a66608c264d2ee7c4815adead
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Security for geoip-py 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.