Aspire.Hosting.OpenAI
13.1.0
Prefix Reserved
dotnet add package Aspire.Hosting.OpenAI --version 13.1.0
NuGet\Install-Package Aspire.Hosting.OpenAI -Version 13.1.0
<PackageReference Include="Aspire.Hosting.OpenAI" Version="13.1.0" />
<PackageVersion Include="Aspire.Hosting.OpenAI" Version="13.1.0" />
<PackageReference Include="Aspire.Hosting.OpenAI" />
paket add Aspire.Hosting.OpenAI --version 13.1.0
#r "nuget: Aspire.Hosting.OpenAI, 13.1.0"
#:package Aspire.Hosting.OpenAI@13.1.0
#addin nuget:?package=Aspire.Hosting.OpenAI&version=13.1.0
#tool nuget:?package=Aspire.Hosting.OpenAI&version=13.1.0
Aspire.Hosting.OpenAI library
Provides extension methods and resource definitions for an Aspire AppHost to configure OpenAI resources and models.
Getting started
Prerequisites
- An OpenAI account with access to the OpenAI API
- OpenAI API key
Install the package
In your AppHost project, install the Aspire OpenAI Hosting library with NuGet:
dotnet add package Aspire.Hosting.OpenAI
Usage example
Then, in the AppHost.cs file of AppHost, add an OpenAI resource and one or more model resources, and consume connections as needed:
var builder = DistributedApplication.CreateBuilder(args);
var openai = builder.AddOpenAI("openai");
var chat = openai.AddModel("chat", "gpt-4o-mini");
var myService = builder.AddProject<Projects.MyService>()
.WithReference(chat);
The WithReference method passes that connection information into a connection string named chat in the MyService project. Multiple models can share the same OpenAI API key via the parent OpenAIResource.
In the Program.cs file of MyService, the connection can be consumed using the client library Aspire.OpenAI:
OpenAI client usage
builder.AddOpenAIClient("chat");
When no model resources are defined, clients can be created with the OpenAI resource only:
builder.AddOpenAIClient("openai")
.AddChatClient("gpt-4o-mini");
Configuration
The OpenAI resources can be configured with the following options:
API Key
The API key is configured on the parent OpenAI resource via a parameter named {resource_name}-openai-apikey or the OPENAI_API_KEY environment variable.
Then in user secrets:
{
"Parameters":
{
"openai-openai-apikey": "YOUR_OPENAI_API_KEY_HERE"
}
}
You can replace the parent API key with a custom parameter on the parent resource:
var apiKey = builder.AddParameter("my-api-key", secret: true);
var openai = builder.AddOpenAI("openai").WithApiKey(apiKey);
// share a single key across multiple models
var chat = openai.AddModel("chat", "gpt-4o-mini");
var embeddings = openai.AddModel("embeddings", "text-embedding-3-small");
Then in user secrets:
{
"Parameters":
{
"my-api-key": "YOUR_OPENAI_API_KEY_HERE",
"alt-key": "ANOTHER_OPENAI_API_KEY"
}
}
Available Models
OpenAI supports various AI models. Some popular options include:
gpt-4o-minigpt-4ogpt-4-turbogpt-3.5-turbotext-embedding-3-smalltext-embedding-3-largedall-e-3whisper-1
Check the OpenAI Models documentation for the most up-to-date list of available models.
Custom endpoint
By default, the OpenAI service endpoint is https://api.openai.com/v1. To use an OpenAI-compatible gateway or self-hosted endpoint, set a custom endpoint on the parent resource:
var openai = builder.AddOpenAI("openai")
.WithEndpoint("https://my-gateway.example.com/v1");
var chat = openai.AddModel("chat", "gpt-4o-mini");
Both the parent and model connection strings will include the custom endpoint.
Connection Properties
When you reference an OpenAI resource using WithReference, the following connection properties are made available to the consuming project:
OpenAIResource
The OpenAI resource exposes the following connection properties:
| Property Name | Description |
|---|---|
Endpoint |
The base endpoint URI for the OpenAI API, with the format https://api.openai.com/v1 |
Uri |
The endpoint URI (same as Endpoint), with the format https://api.openai.com/v1 |
Key |
The API key for authentication |
OpenAI model
The OpenAI model resource combines the parent properties above and adds the following connection property:
| Property Name | Description |
|---|---|
ModelName |
The model identifier for inference requests, for instance gpt-4o-mini |
Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called chat becomes CHAT_URI.
Additional documentation
- https://platform.openai.com/docs/models
- https://github.com/dotnet/aspire/tree/main/src/Components/README.md
Feedback & contributing
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
-
net8.0
- Aspire.Hosting (>= 13.1.0)
- AspNetCore.HealthChecks.Uris (>= 9.0.0)
- Google.Protobuf (>= 3.33.0)
- Grpc.AspNetCore (>= 2.71.0)
- Grpc.Net.ClientFactory (>= 2.71.0)
- Grpc.Tools (>= 2.72.0)
- Humanizer.Core (>= 2.14.1)
- JsonPatch.Net (>= 3.3.0)
- KubernetesClient (>= 18.0.5)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Diagnostics.HealthChecks (>= 8.0.22)
- Microsoft.Extensions.FileSystemGlobbing (>= 10.0.1)
- Microsoft.Extensions.Hosting (>= 8.0.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.Extensions.Primitives (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.4)
- Polly.Core (>= 8.6.4)
- Semver (>= 3.0.0)
- StreamJsonRpc (>= 2.22.23)
- System.IO.Hashing (>= 9.0.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 13.1.0 | 1,481 | 12/17/2025 | |
| 13.0.2 | 6,610 | 12/4/2025 | |
| 13.0.1 | 619 | 11/26/2025 | |
| 13.0.0 | 4,680 | 11/11/2025 | |
| 9.5.2-preview.1.25522.3 | 1,074 | 10/23/2025 | |
| 9.5.1-preview.1.25502.11 | 993 | 10/3/2025 | |
| 9.5.0-preview.1.25474.7 | 412 | 9/25/2025 |