Skip to main content
net8.0

OpenAI resource types for .NET Aspire.

Published
Sep 25, 2025
1y ago
Total Downloads
50.6K
50,592

Deprecated Package Version

Reason: Other, Legacy

This version is out of support and is no longer maintained. Please upgrade to the latest version. See our support policy for details: https://aka.ms/dotnet/aspire/support

Install

dotnet add package Aspire.Hosting.OpenAI --version 9.5.0-preview.1.25474.7
ProductVersions
.NET
net8.0

README

Aspire.Hosting.OpenAI library

Provides extension methods and resource definitions for a .NET 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 .NET 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-mini
  • gpt-4o
  • gpt-4-turbo
  • gpt-3.5-turbo
  • text-embedding-3-small
  • text-embedding-3-large
  • dall-e-3
  • whisper-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.

Additional documentation

Feedback & contributing

https://github.com/dotnet/aspire

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

This version is marked prerelease. Validate its API and upgrade path before production use.NuGet marks this package as deprecated. Review the publisher's migration guidance before adopting it.Compatibility metadata lists 1 target framework; confirm the exact target used by your application.The published metadata declares 24 direct dependency entries across framework groups. Inspect the resolved dependency graph for your target framework.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.