A modern Python client library for ipapi.co IP geolocation API
Project description
IPyAPI
A modern, feature-complete Python client library for the ipapi.co IP geolocation API. Built with httpx and full async support.
Features
- Complete API Coverage: All ipapi.co endpoints supported
- Multiple Response Formats: JSON, XML, CSV, YAML, JSONP
- Sync & Async: Both synchronous and asynchronous clients
- Type Hints: Full type annotations for better IDE support
- Error Handling: Comprehensive exception handling for all API errors
- Well Tested: Extensive test coverage with pytest
- Modern Python: Built for Python 3.10+
Installation
Install using pip:
pip install ipyapi
Install using uv (recommended):
uv add ipyapi
Quick Start
Synchronous Usage
from ipyapi import IPyAPI
# Free tier (no API key required)
with IPyAPI() as client:
# Get complete location data
location = client.get_location("8.8.8.8")
print(f"{location.city}, {location.country_name}")
# Output: Mountain View, United States
# Get specific field
country = client.get_country("8.8.8.8")
print(country) # Output: US
# Get your own IP info
my_location = client.get_location()
print(my_location.ip)
# With API key (for paid plans)
with IPyAPI(api_key="your_api_key_here") as client:
location = client.get_location("8.8.8.8")
print(location.country_name)
Asynchronous Usage
import asyncio
from ipyapi import AsyncIPyAPI
async def main():
async with AsyncIPyAPI() as client:
# Get complete location data
location = await client.get_location("8.8.8.8")
print(f"{location.city}, {location.country_name}")
# Get specific field
country = await client.get_country("8.8.8.8")
print(country)
asyncio.run(main())
Usage Examples
Get Complete Location Information
from ipyapi import IPyAPI
with IPyAPI() as client:
location = client.get_location("8.8.8.8")
# Access all available fields
print(f"IP: {location.ip}")
print(f"City: {location.city}")
print(f"Region: {location.region}")
print(f"Country: {location.country_name}")
print(f"Timezone: {location.timezone}")
print(f"Currency: {location.currency}")
print(f"ASN: {location.asn}")
print(f"Organization: {location.org}")
print(f"Coordinates: {location.latitude}, {location.longitude}")
Get Data as Dictionary
with IPyAPI() as client:
# Return as dictionary instead of IPLocation object
data = client.get_location("8.8.8.8", return_type="dict")
print(data["country_name"])
Get Single Fields
with IPyAPI() as client:
# Convenience methods for common fields
ip = client.get_ip()
city = client.get_city("8.8.8.8")
country = client.get_country("8.8.8.8")
country_name = client.get_country_name("8.8.8.8")
timezone = client.get_timezone("8.8.8.8")
currency = client.get_currency("8.8.8.8")
asn = client.get_asn("8.8.8.8")
org = client.get_org("8.8.8.8")
# Or use generic get_field method
postal = client.get_field("postal", "8.8.8.8")
Different Response Formats
with IPyAPI() as client:
# Get response in different formats
json_data = client.get_location_raw("8.8.8.8", format="json")
xml_data = client.get_location_raw("8.8.8.8", format="xml")
csv_data = client.get_location_raw("8.8.8.8", format="csv")
yaml_data = client.get_location_raw("8.8.8.8", format="yaml")
Error Handling
from ipyapi import IPyAPI
from ipyapi.exceptions import (
RateLimitError,
InvalidIPAddressError,
ReservedIPAddressError,
IPyAPIError
)
with IPyAPI() as client:
try:
location = client.get_location("invalid-ip")
except InvalidIPAddressError as e:
print(f"Invalid IP: {e.message}")
except ReservedIPAddressError as e:
print(f"Reserved IP: {e.message}")
except RateLimitError as e:
print(f"Rate limited: {e.message}")
except IPyAPIError as e:
print(f"API error: {e.message}")
Custom Configuration
# Custom timeout
client = IPyAPI(timeout=30.0)
# Custom base URL (for testing or proxy)
client = IPyAPI(base_url="https://custom.api.url")
API Reference
IPyAPI / AsyncIPyAPI
Main client classes for interacting with the ipapi.co API.
Methods
get_location(ip_address=None, return_type="object")- Get complete location dataget_field(field, ip_address=None)- Get a single field valueget_location_raw(ip_address=None, format="json")- Get raw response in specified format
Convenience Methods
get_ip()- Get client's IP addressget_city(ip_address=None)- Get city nameget_country(ip_address=None)- Get country codeget_country_name(ip_address=None)- Get country nameget_timezone(ip_address=None)- Get timezoneget_currency(ip_address=None)- Get currency codeget_asn(ip_address=None)- Get ASNget_org(ip_address=None)- Get organization name
IPLocation
Dataclass containing complete IP location information.
Fields:
ip,network,versioncity,region,region_codecountry,country_name,country_code,country_code_iso3country_capital,country_tld,continent_codein_eu,postallatitude,longitudetimezone,utc_offsetcountry_calling_codecurrency,currency_namelanguagescountry_area,country_populationasn,orghostname(optional)
Exceptions
All exceptions inherit from IPyAPIError:
BadRequestError- 400 Bad RequestForbiddenError- 403 Forbidden (Authentication Failed)NotFoundError- 404 Not FoundMethodNotAllowedError- 405 Method Not AllowedRateLimitError- 429 Too Many RequestsInvalidIPAddressError- Invalid IP address formatReservedIPAddressError- Reserved/private IP address
Development
Setup with uv
# Clone the repository
git clone https://github.com/wihlarkop/ipyapi.git
cd ipyapi
# Install with dev dependencies
uv sync --all-extras
# Run tests
uv run pytest
# Run tests with coverage
uv run pytest --cov=ipyapi --cov-report=html
Running Tests
# Run all tests
uv run pytest
# Run specific test file
uv run pytest tests/test_client.py
# Run with coverage
uv run pytest --cov=ipyapi --cov-report=term-missing
Rate Limits
The free ipapi.co API has rate limits:
- Free tier: 1,000 requests per day
- No registration: 30 requests per minute
For higher limits, consider ipapi.co's paid plans.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built on top of the excellent ipapi.co API
- Uses httpx for HTTP requests
- Managed with uv for fast, reliable package management
Links
- Documentation: ipapi.co API docs
- PyPI: pypi.org/project/ipyapi
- Source Code: github.com/wihlarkop/ipyapi
- Issue Tracker: github.com/wihlarkop/ipyapi/issues
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ipyapi-0.1.0.tar.gz.
File metadata
- Download URL: ipyapi-0.1.0.tar.gz
- Upload date:
- Size: 48.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
076c42aa68a3d6908531ae1c83e1c831874e27bf0a01de49498d3df46d46b920
|
|
| MD5 |
aa8192dc092649b681f71377dc1036a4
|
|
| BLAKE2b-256 |
916f68dd3aa4572f9d1ad40fe0df3e24c92838eb6167c783a99c96df092cd5d6
|
File details
Details for the file ipyapi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ipyapi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ddc569dc279978cfc6ec71b8f9a4793b88580a0da9892b59a46e008032a3d93d
|
|
| MD5 |
41bf7910085312945f6bc31422f8b4f2
|
|
| BLAKE2b-256 |
38114cee744bd131a2fa68479f6c9686812bbaff6a1dbbcf0dfb8015c16f2323
|