Skip to content
Docs Try Aspire
Docs Try

Set up OpenAI in the AppHost

OpenAI logo

This article is the reference for the Aspire OpenAI hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.ts — that you use to model an OpenAI parent resource and model resources in your AppHost project.

If you’re new to the OpenAI integration, start with the Get started with OpenAI integrations guide. For how consuming apps read the connection information this page exposes, see Connect to OpenAI.

To start building an Aspire app that uses OpenAI, install the 📦 Aspire.Hosting.OpenAI NuGet package:

Terminal
aspire add openai

Learn more about aspire add in the command reference.

Or, choose a manual installation approach:

C# — AppHost.cs
#:package Aspire.Hosting.OpenAI@*
XML — AppHost.csproj
<PackageReference Include="Aspire.Hosting.OpenAI" Version="*" />

Once you’ve installed the hosting integration in your AppHost project, you can add an OpenAI parent resource and then add one or more model resources beneath it:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var openai = builder.AddOpenAI("openai");
var chat = openai.AddModel("chat", "gpt-4o-mini");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(chat);
// After adding all resources, run the app...
  1. Calling AddOpenAI (or addOpenAI) creates an OpenAIResource that models the OpenAI account. It registers a secret parameter for the API key that defaults to the OPENAI_API_KEY environment variable.

  2. Calling AddModel (or addModel) creates an OpenAIModelResource child whose connection string is composed from the parent endpoint, API key, and the model name you supply.

  3. The AppHost reference call configures a connection in the consuming project named after the referenced model resource, such as chat in the preceding example. Multiple models can share the single API key and endpoint from the parent resource.

Add multiple model children beneath the same parent to share a single API key and endpoint:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var openai = builder.AddOpenAI("openai");
var chat = openai.AddModel("chat", "gpt-4o-mini");
var embeddings = openai.AddModel("embeddings", "text-embedding-3-small");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(chat)
.WithReference(embeddings);
// After adding all resources, run the app...

Referencing chat passes a connection string named chat to the project, and referencing embeddings passes a connection string named embeddings. Both share the parent API key and endpoint.

Calling AddOpenAI("openai") (or addOpenAI("openai")) automatically creates a secret parameter named openai-openai-apikey. Aspire resolves its value in this order:

  1. The Parameters:openai-openai-apikey configuration key (user secrets, appsettings.*, or environment variables).
  2. The OPENAI_API_KEY environment variable.

If neither source provides a value, startup throws an exception. Provide the key via the Aspire CLI:

Terminal
aspire secret set Parameters:openai-openai-apikey sk-your-api-key

Replace the default parameter by creating your own secret parameter and passing it to WithApiKey (or withApiKey) on the parent:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var apiKey = builder.AddParameter("my-api-key", secret: true);
var openai = builder.AddOpenAI("openai")
.WithApiKey(apiKey);
var chat = openai.AddModel("chat", "gpt-4o-mini");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(chat);
// After adding all resources, run the app...

Override the default https://api.openai.com/v1 endpoint to use a proxy, an Azure OpenAI endpoint, or any OpenAI-compatible gateway. Call WithEndpoint (or withEndpoint) on the parent resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var openai = builder.AddOpenAI("openai")
.WithEndpoint("https://my-gateway.example.com/v1");
var chat = openai.AddModel("chat", "gpt-4o-mini");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(chat);
// After adding all resources, run the app...

All model resources beneath the parent inherit the overridden endpoint. The overridden endpoint is reflected in the Endpoint and Uri connection properties that Aspire injects into consuming apps.

Add an optional health check to a model resource to validate endpoint reachability, API key validity, and model existence at startup:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var openai = builder.AddOpenAI("openai");
var chat = openai.AddModel("chat", "gpt-4o-mini")
.WithHealthCheck();
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(chat);
// After adding all resources, run the app...

The model health check validates endpoint reachability, API key validity (401), and model existence (404). It executes only once per application instance to limit rate-limit implications.

Common model identifiers for the OpenAI API:

CategoryModel identifiers
Chatgpt-5, gpt-4o, gpt-4o-mini, gpt-4-turbo
Reasoningo3, o3-mini, o4-mini
Realtimegpt-realtime
Embeddingstext-embedding-3-small, text-embedding-3-large
Imagesdall-e-3
Audiowhisper-1

For the full and up-to-date list, see the OpenAI models documentation.

For the full reference of OpenAI connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to OpenAI.