This sample demonstrates the configuration-driven approach available in the OpenAI .NET client library starting with version 2.10.0, where a ResponsesClient is constructed from a strongly-typed ResponsesClientSettings instance bound from IConfiguration. It calls the OpenAI Responses API.
- Configuration-driven registration:
ResponsesClientis bound fromIConfigurationviaResponsesClientSettingsusing thebuilder.AddResponsesClient(...)extension method - Strongly-typed settings:
ResponsesClientSettingsenables validation for client configuration - Thread-Safe:
ResponsesClientis thread-safe and registered for the application's lifetime - Configurable Model: Model selection via configuration (appsettings.json)
- Modern ASP.NET Core: Uses minimal APIs with async/await patterns
- .NET 10 SDK or later
- An OpenAI API key
-
Store your OpenAI API key using one of the following methods:
-
Environment variable:
$env:Clients__ResponsesClient__Credential__Key = "<your-API-key-here>" -
.NET User Secrets Manager:
Run the following command in your project's root directory:
dotnet user-secrets set "Clients:ResponsesClient:Credential:Key" "<your-API-key-here>"
-
-
Install dependencies, build, & run the app:
dotnet run -
Send a request using one of the following approaches:
-
curl:
curl -X POST "https://localhost:7098/responses/create" \ -H "Content-Type: application/json" \ -d "{\"message\": \"What is the capital of France?\"}" -
PowerShell:
Invoke-RestMethod -Uri "https://localhost:7098/responses/create" ` -Method POST ` -ContentType "application/json" ` -Body '{"message": "What is the capital of France?"}'
-
builder.AddResponsesClient("Clients:ResponsesClient");AddResponsesClient is an IHostApplicationBuilder extension provided by the OpenAI library. It reads the named configuration section (here Clients:ResponsesClient), binds it to ResponsesClientSettings, and registers the resulting ResponsesClient with the DI container. The credential is resolved from the Credential subsection (e.g., Clients:ResponsesClient:Credential:Key), so no manual ApiKeyCredential plumbing is required.
ResponsesClientSettings is annotated [Experimental("SCME0002")]. This warning is suppressed via <NoWarn>SCME0002</NoWarn> in the project file.
app.MapPost("/responses/create",
async (ResponsesRequest request, ResponsesClient client, IConfiguration configuration) =>
{
string model = configuration["Clients:ResponsesClient:Model"]
?? throw new InvalidOperationException("Model not configured at Clients:ResponsesClient:Model.");
ResponseResult response = await client.CreateResponseAsync(model, request.Message);
return new ResponsesResponse(response.GetOutputText());
});The model is read from appsettings.json on each call.