Prerequisites
- A recent stable Rust toolchain with an async runtime (Tokio)
- The
ip-api-iocrate added (cargo add ip-api-io) - A free ip-api.io API key
Geolocate one IP with lookup_ip
Pass any IPv4 or IPv6 address, or use lookup() to geolocate the caller's
own IP. Nullable fields are Option<T> — match before use.
let client = Client::new(std::env::var("IP_API_IO_KEY")?);
let info = client.lookup_ip("8.8.8.8").await?;
println!("{}", info.ip); // "8.8.8.8"
if let Some(isp) = &info.isp {
println!("{isp}"); // "Google LLC"
}
if let Some(country) = &info.location.country {
println!("{country}"); // "United States"
}
println!("{}", info.suspicious_factors.is_datacenter); // true
Resolve the IP of the machine making the request by calling lookup():
let me = client.lookup().await?;
println!("{}", me.ip); X-Forwarded-For header to lookup_ip(ip) rather than relying
on the socket address. See the
SDK overview for an Axum handler 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
returns an Error if the slice is empty or longer than 100.
let batch = client
.lookup_batch(&["8.8.8.8", "1.1.1.1", "9.9.9.9"])
.await?;
println!("{}", batch.total_processed); // 3
println!("{}", batch.successful_lookups); // 3
println!("{}", batch.failed_lookups); // 0
for (ip, info) in &batch.results {
println!("{ip} {}", info.suspicious_factors.is_vpn);
}