Introduction
Artificial Intelligence is no longer limited to chatbots. Modern applications expose AI capabilities through REST APIs that can summarize documents, answer questions, generate content, classify text, analyze sentiment, and automate business workflows.
ASP.NET Core provides an excellent foundation for building scalable APIs, while Azure OpenAI delivers enterprise-grade access to powerful language models. Together, they enable .NET developers to create secure, high-performance AI services that integrate seamlessly with existing applications.
In this article, you'll build the foundation of an AI-powered API using ASP.NET Core and Azure OpenAI while exploring the production practices required for enterprise deployments.
Why Build AI APIs?
Instead of embedding AI directly into every application, exposing AI capabilities through APIs offers several advantages:
Centralized AI services
Reusable business logic
Easier maintenance
Better security
Independent scaling
Consistent AI behavior across applications
This architecture allows web applications, mobile apps, desktop software, and other services to consume the same AI capabilities.
Solution Architecture
A typical AI-powered API consists of the following components:
| Component | Responsibility |
|---|---|
| Client Application | Sends API requests |
| ASP.NET Core Web API | Processes requests |
| Azure OpenAI | Generates AI responses |
| Optional Database | Stores prompts or history |
| Azure Monitor | Collects logs and metrics |
The API receives a request, forwards it to Azure OpenAI, and returns the generated response to the client.
Configuring Azure OpenAI
Install the Azure OpenAI SDK.
dotnet add package Azure.AI.OpenAI
Create the Azure OpenAI client.
using Azure;
using Azure.AI.OpenAI;
var client = new AzureOpenAIClient(
new Uri(builder.Configuration["AzureOpenAI:Endpoint"]),
new AzureKeyCredential(builder.Configuration["AzureOpenAI:ApiKey"]));
The client manages communication with your Azure OpenAI deployment and should be reused throughout the application.
Creating an AI Endpoint
Create a simple API endpoint that accepts a prompt and returns the AI-generated response.
app.MapPost("/api/chat", async (
ChatRequest request,
ChatClient chatClient) =>
{
var response = await chatClient.CompleteChatAsync(
request.Prompt);
return Results.Ok(new
{
Response = response.Content[0].Text
});
});
This minimal endpoint demonstrates how easily AI capabilities can be exposed through an ASP.NET Core Web API.
Enhancing the API
As your application grows, AI endpoints can support additional capabilities such as:
Content summarization
Question answering
Text classification
Sentiment analysis
Code generation
Document analysis
Retrieval-Augmented Generation (RAG)
Keeping each endpoint focused on a single responsibility makes the API easier to maintain and test.
Production Considerations
Dependency Injection
Register Azure OpenAI services through ASP.NET Core's dependency injection container.
This avoids repeatedly creating client instances and centralizes application configuration.
Inject AI services into controllers or minimal APIs rather than creating them manually.
Configuration
Store AI configuration in appsettings.json.

Join the conversation! Your thoughts help the community grow.