Prerequisites
- A .NET 6+ project with an async entry point
- The
IpApiIopackage added (dotnet add package IpApiIo) - A free ip-api.io API key
Geolocate one IP with LookupAsync
Pass any IPv4 or IPv6 address, or use LookupAsync() to geolocate the caller's
own IP. Nullable fields are string? / double? — null-check before use.
var client = new IpApiClient(Environment.GetEnvironmentVariable("IP_API_IO_KEY")!);
var info = await client.LookupAsync("8.8.8.8");
Console.WriteLine(info.Ip); // "8.8.8.8"
Console.WriteLine(info.Isp); // "Google LLC"
Console.WriteLine(info.Location.Country); // "United States"
Console.WriteLine(info.SuspiciousFactors.IsDatacenter); // True
Resolve the IP of the machine making the request by calling LookupAsync():
var me = await client.LookupAsync();
Console.WriteLine(me.Ip); X-Forwarded-For header to LookupAsync(ip) rather than relying
on the socket address. See the
SDK overview for an ASP.NET Core example.
Geolocate up to 100 IPs with LookupBatchAsync
Enrich logs, sign-up events, or historical data without a round trip per IP. The SDK throws if the list is empty or longer than 100.
var batch = await client.LookupBatchAsync(
new[] { "8.8.8.8", "1.1.1.1", "9.9.9.9" });
Console.WriteLine(batch.TotalProcessed); // 3
Console.WriteLine(batch.SuccessfulLookups); // 3
Console.WriteLine(batch.FailedLookups); // 0
foreach (var (ip, info) in batch.Results)
Console.WriteLine($"{ip} {info.SuspiciousFactors.IsVpn}");