Libraries

C# SDK

The C# SDK provides a modern and intuitive way to integrate GeoIP lookups into your .NET applications.


Installation

Install the NuGet package:

dotnet add package GeoIP

Or using Package Manager Console:

Install-Package GeoIP

Quick Start

using GeoIP;

var sdk = new GeoIp(serverUrl: "https://api.geoipapi.com");

try
{
    // Get current IP address
    var currentIp = await sdk.GeoIPEndpoints.GetIpAsync();
    Console.WriteLine($"Current IP: {currentIp}");
    
    // Get detailed geolocation data
    var geoData = await sdk.GeoIPEndpoints.GetIpDataAsync();
    Console.WriteLine("Location data retrieved successfully!");
}
catch (Exception ex)
{
    Console.WriteLine($"Error: {ex.Message}");
}

Configuration

Configure the client with custom settings:

var sdk = new GeoIp(
    serverUrl: "https://api.geoipapi.com"
);

Available Methods

GetIpAsync()

Get your current public IP address:

var currentIp = await sdk.GeoIPEndpoints.GetIpAsync();
Console.WriteLine($"Current IP: {currentIp}");

GetIpDataAsync()

Get comprehensive geolocation data:

var geoData = await sdk.GeoIPEndpoints.GetIpDataAsync();
Console.WriteLine("Geolocation data retrieved successfully!");

Response Model

The C# SDK uses strongly-typed response models:

// Response models are auto-generated based on API specification
// Check the Models namespace for exact type definitions
public class GeoIPResponse
{
    public string IP { get; set; }
    public string Type { get; set; }
    public Country Country { get; set; }
    public Location Location { get; set; }
    public ASN ASN { get; set; }
}

public class Country
{
    public bool IsEuMember { get; set; }
    public string CurrencyCode { get; set; }
    public string Continent { get; set; }
    public string Name { get; set; }
    public string CountryCode { get; set; }
    public string State { get; set; }
    public string City { get; set; }
    public string Zip { get; set; }
    public string Timezone { get; set; }
}

public class Location
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

public class ASN
{
    public int Number { get; set; }
    public string Name { get; set; }
    public string Network { get; set; }
    public string Type { get; set; }
}

Error Handling

Handle different types of errors:

using GeoIP.Models.Errors;

try
{
    var result = await sdk.GeoIPEndpoints.GetIpDataAsync();
    // Handle successful response
}
catch (HTTPValidationError validationError)
{
    Console.WriteLine($"Validation error: {validationError.Message}");
}
catch (APIException apiError)
{
    Console.WriteLine($"API error: {apiError.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"Unexpected error: {ex.Message}");
}

Dependency Injection

Use with dependency injection in ASP.NET Core:

// In Program.cs or Startup.cs
services.AddSingleton<GeoIp>(provider => 
    new GeoIp(serverUrl: "https://api.geoipapi.com"));

// In your service or controller
public class LocationService
{
    private readonly GeoIp _geoIp;

    public LocationService(GeoIp geoIp)
    {
        _geoIp = geoIp;
    }

    public async Task<string> GetCurrentLocationAsync()
    {
        var geoData = await _geoIp.GeoIPEndpoints.GetIpDataAsync();
        return "Location data retrieved";
    }
}

Cancellation Support

Support for cancellation tokens:

var cancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token;

try
{
    var result = await sdk.GeoIPEndpoints.GetIpDataAsync();
    Console.WriteLine("Location data retrieved successfully!");
}
catch (OperationCanceledException)
{
    Console.WriteLine("Operation was cancelled");
}

Testing

Run the test suite:

# Run all tests
dotnet test

# Run tests with coverage
dotnet test --collect:"XPlat Code Coverage"
Previous
Java
Next
PHP