Prerequisites
- Python 3.8 or higher
- The
ip-api-iopackage installed (pip install ip-api-io) - A free ip-api.io API key
Geolocate one IP with lookup
Pass any IPv4 or IPv6 address, or omit the argument to geolocate the caller's own IP.
import os
from ipapi_io import IpApiClient
client = IpApiClient(api_key=os.environ["IP_API_IO_KEY"])
info = client.lookup("8.8.8.8")
print(info["ip"]) # "8.8.8.8"
print(info["isp"]) # "Google LLC"
print(info["location"]["country"]) # "United States"
print(info["location"]["city"]) # "Mountain View"
print(info["location"]["latitude"],
info["location"]["longitude"])
print(info["location"]["timezone"]) # "America/Los_Angeles"
print(info["suspicious_factors"]["is_datacenter"]) # True
Resolve the IP of the machine making the request by calling lookup() with
no argument:
me = client.lookup()
print(me["ip"], me["location"]["country"]) X-Forwarded-For header to lookup(ip) rather than relying on
the socket address. See the
SDK overview for a Flask example.
Geolocate up to 100 IPs with lookup_batch
Enrich logs, sign-up events, or historical data without a round trip per IP. The SDK
validates the limit client-side and raises ValueError if the list is
empty or longer than 100.
batch = client.lookup_batch(["8.8.8.8", "1.1.1.1", "9.9.9.9"])
print(batch["total_processed"]) # 3
print(batch["successful_lookups"]) # 3
print(batch["failed_lookups"]) # 0
for ip, info in batch["results"].items():
print(ip, info["location"]["country"], info["suspicious_factors"]["is_vpn"])