Microsoft.Extensions.AI - Azure OpenAI Web API Example
November 10, 2025 ยท View on GitHub
This project contains a minimal Web API that show how to use the OpenAI reference implementation in the Microsoft.Extensions.AI.OpenAI NuGet package with the Azure OpenAI service.
Prerequisites
- .NET 8 SDK
- Visual Studio or VS Code
- An Azure OpenAI Service resource with a chat completion and text embedding generation model deployments. For more details, see the Azure OpenAI resource deployment documentation.
Setup
-
In the AzureOpenAIWebAPI project directory, create a file called appsettings.local.json with the following content.
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "AI": { "AzureOpenAI": { "Endpoint": "YOUR-AZURE-OPENAI-ENDPOINT", "Chat": { "ModelId": "gpt-4o-mini" }, "Embedding": { "ModelId": "text-embedding-3-small" } } } } -
Replace the value of the
Endpointwith your Azure OpenAI API endpoint. For more details on where to find your Azure OpenAI endpoint, see the Azure OpenAI documentation.
Quick Start
Visual Studio
- Open the AzureOpenAIExamples.sln solution
- Set AzureOpenAIWebAPI as the startup project.
- Press F5.
Visual Studio Code
-
Open your terminal
-
Navigate to the AzureOpenAIWebAPI project directory
-
Run the application using
dotnet rundotnet run
Test your application
PowerShell
Chat
$response = Invoke-RestMethod -Uri 'http://localhost:5208/chat' -Method Post -Headers @{'Content-Type'='application/json'} -Body '"What is AI?"'; $response.message.contents.text
Embeddings
$response = Invoke-RestMethod -Uri 'http://localhost:5208/embedding' -Method Post -Headers @{'Content-Type'='application/json'} -Body '"What is AI?"'; $response.vector