IpApi.Net is a .NET wrapper for IPQuery IP API, making it easy to query IP-related information in your .NET projects.
To get started, install the IpApi NuGet package:
dotnet add package IpApi
-
Register the IpApiClient in your service collection:
services.AddIpApiClient();
-
Inject the
IIpApiClient
interface into your constructor:public class MyService { private readonly IIpApiClient _ipApiClient; public MyService(IIpApiClient ipApiClient) { _ipApiClient = ipApiClient; } public async Task DoSomethingAsync(CancellationToken cancellationToken) { var myIp = await _ipApiClient.QueryMyIpAsync(cancellationToken); var ipInfo = await _ipApiClient.QueryIpAsync("8.8.8.8", cancellationToken); } }
The IIpApiClient
interface exposes the following methods:
Retrieves your public IP address.
Retrieves information about a specific IP address.
- Parameters:
ip
: The IP address to query.cancellationToken
: A token to cancel the operation.
Retrieves information about a collection of IP addresses.
- Parameters:
ips
: An array of IP addresses to query.cancellationToken
: A token to cancel the operation.
var myIp = await _ipApiClient.QueryMyIpAsync(cancellationToken);
Console.WriteLine($"My public IP: {myIp}");
var ipInfo = await _ipApiClient.QueryIpAsync("8.8.8.8", cancellationToken);
Console.WriteLine($"IP Info: {ipInfo?.City}, {ipInfo?.Country}");
var ipInfos = await _ipApiClient.QueryIpsAsync(new[] {"8.8.8.8", "1.1.1.1"}, cancellationToken);
foreach (var info in ipInfos ?? Array.Empty<IpInfo>())
{
Console.WriteLine($"IP: {info.Ip}, City: {info.City}, Country: {info.Country}");
}
This project is licensed under the MIT License.