MX.Api.Client
2.0.155.1
See the version list below for details.
dotnet add package MX.Api.Client --version 2.0.155.1
NuGet\Install-Package MX.Api.Client -Version 2.0.155.1
<PackageReference Include="MX.Api.Client" Version="2.0.155.1" />
<PackageVersion Include="MX.Api.Client" Version="2.0.155.1" />
<PackageReference Include="MX.Api.Client" />
paket add MX.Api.Client --version 2.0.155.1
#r "nuget: MX.Api.Client, 2.0.155.1"
#:package MX.Api.Client@2.0.155.1
#addin nuget:?package=MX.Api.Client&version=2.0.155.1
#tool nuget:?package=MX.Api.Client&version=2.0.155.1
MX.Api.Client
This library provides a comprehensive implementation for creating resilient, authenticated REST API clients as part of the MX API Abstractions approach. Built on top of MX.Api.Abstractions, it offers base classes, interfaces, and utilities for creating API clients with features such as authentication, token management, request execution, and standardized response processing.
Features
- Support for multiple authentication methods (API Key and Entra ID authentication)
- Automatic token acquisition and caching with thread-safe operations
- Built-in retry policies with exponential backoff and circuit breaker patterns
- Thread-safe REST client management
- Standardized error handling and response processing using ApiResponse<T> model
- Support for API key authentication with resilient handling
- Integration with Microsoft.Extensions.Logging for comprehensive diagnostics
Installation
dotnet add package MX.Api.Client
Usage
Basic Setup
// Register the API client services
services.AddApiClient()
.WithApiKeyAuthentication("your-api-key");
// Or with Entra ID authentication
services.AddApiClient()
.WithEntraIdAuthentication("api://your-api-audience");
// Configure client options
services.Configure<ApiClientOptions>(options =>
{
options.BaseUrl = "https://api.example.com";
options.ApiPathPrefix = "v2";
options.MaxRetryCount = 3;
});
Creating a Custom API Client
// Inherit from BaseApi
public class MyApiClient : BaseApi
{
private readonly ILogger<MyApiClient> logger;
public MyApiClient(
ILogger<MyApiClient> logger,
IApiTokenProvider apiTokenProvider,
IRestClientService restClientService,
IOptions<ApiClientOptions> options)
: base(logger, apiTokenProvider, restClientService, options)
{
this.logger = logger;
}
// Implement custom API methods
public async Task<ApiResponse<ResourceDto>> GetResourceAsync(string id, CancellationToken cancellationToken = default)
{
try
{
var request = await CreateRequestAsync($"resources/{id}", Method.Get, cancellationToken);
var response = await ExecuteAsync(request, false, cancellationToken);
return response.ToApiResponse<ResourceDto>();
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
logger.LogError(ex, "Failed to retrieve resource with ID {ResourceId}", id);
var errorResponse = new ApiResponse<ResourceDto>(new ApiError("InternalError", "An unexpected error occurred"));
return new HttpResponseWrapper<ResourceDto>(HttpStatusCode.InternalServerError, errorResponse);
}
}
}
Updating API Key at Runtime
// Update authentication options
var authOptions = new ApiKeyAuthenticationOptions
{
ApiKey = "new-api-key",
HeaderName = "Ocp-Apim-Subscription-Key" // or your custom header name
};
Authentication Methods
API Key Authentication
Use this when your API requires an API key in a header (like Azure API Management).
services.AddApiClient()
.WithApiKeyAuthentication("your-api-key", "X-API-Key"); // Custom header name
Entra ID Authentication
Use this when your API requires OAuth tokens from Entra ID (formerly Azure AD).
services.AddApiClient()
.WithEntraIdAuthentication("api://your-api-audience");
With custom credential options:
services.AddApiClient()
.WithEntraIdAuthentication("api://your-api-audience", options =>
{
options.ExcludeManagedIdentityCredential = true;
// Other DefaultAzureCredentialOptions
});
Error Handling and Resilience
The library includes built-in retry policies with exponential backoff for transient failures:
services.Configure<ApiClientOptions>(options =>
{
options.MaxRetryCount = 3; // Configure retry count (default is 3)
});
You can also customize the retry behavior:
// Custom retry policy
services.AddApiClient()
.WithCustomRetryPolicy(retryCount =>
Policy
.Handle<HttpRequestException>()
.OrResult<RestResponse>(r => r.StatusCode == HttpStatusCode.TooManyRequests)
.WaitAndRetryAsync(
retryCount,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)) +
TimeSpan.FromMilliseconds(Random.Shared.Next(0, 1000))
)
);
Advanced Usage
Working with Collections and Filtering
public async Task<ApiResponse<CollectionModel<ResourceDto>>> GetResourcesAsync(FilterOptions filter, CancellationToken cancellationToken = default)
{
try
{
var request = await CreateRequestAsync("resources", Method.Get, cancellationToken);
// Add filter options to request
request.AddFilterOptions(filter);
var response = await ExecuteAsync(request, false, cancellationToken);
return response.ToApiResponse<CollectionModel<ResourceDto>>();
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
logger.LogError(ex, "Failed to retrieve resources");
var errorResponse = new ApiResponse<CollectionModel<ResourceDto>>(new ApiError("InternalError", "An unexpected error occurred"));
return new HttpResponseWrapper<CollectionModel<ResourceDto>>(HttpStatusCode.InternalServerError, errorResponse);
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Azure.Identity (>= 1.14.1)
- Microsoft.Extensions.Caching.Abstractions (>= 9.0.6)
- Microsoft.Extensions.Caching.Memory (>= 9.0.6)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.6)
- Microsoft.Extensions.Http (>= 9.0.6)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.6)
- Microsoft.Extensions.Options (>= 9.0.6)
- MX.Api.Abstractions (>= 2.0.155.1)
- Newtonsoft.Json (>= 13.0.3)
- Polly (>= 8.6.1)
- RestSharp (>= 112.1.0)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on MX.Api.Client:
| Package | Downloads |
|---|---|
|
XtremeIdiots.Portal.Repository.Api.Client.V1
Versioned client for the XtremeIdiots Portal Repository API V1. |
|
|
XtremeIdiots.Portal.Repository.Api.Client.V2
Versioned client for the XtremeIdiots Portal Repository API V2. |
|
|
XtremeIdiots.Portal.Integrations.Servers.Api.Client.V1
Client for the XtremeIdiots Portal Servers API. |
|
|
MX.GeoLocation.Api.Client.V1
This package provides a web service client to query the geolocation service. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.195.1 | 206 | 11/6/2025 |
| 2.0.194.1 | 187 | 10/30/2025 |
| 2.0.193.1 | 182 | 10/23/2025 |
| 2.0.192.1 | 196 | 10/16/2025 |
| 2.0.191.1 | 202 | 10/9/2025 |
| 2.0.190.1 | 2,676 | 10/2/2025 |
| 2.0.186.1 | 889 | 9/11/2025 |
| 2.0.185.1 | 389 | 9/4/2025 |
| 2.0.184.1 | 400 | 8/28/2025 |
| 2.0.183.1 | 278 | 8/26/2025 |
| 2.0.182.1 | 1,739 | 8/23/2025 |
| 2.0.181.1 | 123 | 8/22/2025 |
| 2.0.180.1 | 1,340 | 8/21/2025 |
| 2.0.179.1 | 382 | 8/20/2025 |
| 2.0.178.1 | 177 | 8/20/2025 |
| 2.0.177.1 | 1,232 | 8/18/2025 |
| 2.0.176.1 | 178 | 8/11/2025 |
| 2.0.175.1 | 354 | 8/4/2025 |
| 2.0.174.1 | 300 | 7/28/2025 |
| 2.0.173.1 | 382 | 7/21/2025 |
| 2.0.172.1 | 221 | 7/14/2025 |
| 2.0.171.1 | 762 | 7/8/2025 |
| 2.0.170.1 | 226 | 7/8/2025 |
| 2.0.169.1 | 175 | 7/8/2025 |
| 2.0.168.1 | 176 | 7/8/2025 |
| 2.0.166.1 | 160 | 7/7/2025 |
| 2.0.165.1 | 284 | 7/6/2025 |
| 2.0.164.1 | 202 | 7/6/2025 |
| 2.0.163.1 | 171 | 7/6/2025 |
| 2.0.162.1 | 167 | 7/6/2025 |
| 2.0.161.1 | 157 | 7/5/2025 |
| 2.0.160.1 | 114 | 7/5/2025 |
| 2.0.159.1 | 107 | 7/5/2025 |
| 2.0.158.1 | 92 | 7/5/2025 |
| 2.0.157.1 | 107 | 7/5/2025 |
| 2.0.156.1 | 106 | 7/5/2025 |
| 2.0.155.1 | 104 | 7/5/2025 |
| 2.0.154.1 | 104 | 7/5/2025 |
| 2.0.153.1 | 102 | 7/5/2025 |
| 2.0.152.1 | 108 | 7/5/2025 |
| 2.0.151.1 | 102 | 7/5/2025 |