Provides a cached HttpClient configured for OpenAI moderation requests.
dotnet add package Soenneker.OpenAI.Moderation.HttpClientsusing Microsoft.Extensions.DependencyInjection;
using Soenneker.OpenAI.Moderation.HttpClients.Registrars;
services.AddOpenAIModerationHttpClientAsSingleton();Use AddOpenAIModerationHttpClientAsScoped() when the wrapper must follow a consumer scope. Both registrations reuse the singleton HTTP-client cache; disposing a scoped wrapper does not destroy the shared client.
The API key is required:
{
"OpenAI": {
"Moderation": {
"ApiKey": "..."
}
}
}Keep the key in a secret store or environment variable. The client uses https://api.openai.com/v1 and an Authorization: Bearer <token> header by default. Compatible endpoints can be configured with these optional keys:
{
"OpenAI": {
"ClientBaseUrl": "https://example.com/v1",
"AuthHeaderName": "Authorization",
"AuthHeaderValueTemplate": "Bearer {token}"
}
}{token} is replaced with OpenAI:Moderation:ApiKey.
Inject IOpenAIModerationHttpClient and retrieve the cached client:
using Soenneker.OpenAI.Moderation.HttpClients.Abstract;
HttpClient client = await moderationHttpClient.Get(cancellationToken);
using var request = new HttpRequestMessage(HttpMethod.Get, "models");
using HttpResponseMessage response = await client.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();Do not dispose the returned HttpClient; its lifetime is owned by the singleton cache.