Introduction
Artificial Intelligence is becoming a major part of modern software applications. Many platforms now include AI-powered capabilities such as chatbots, automated content generation, intelligent customer support systems, and AI coding assistants.
Companies increasingly want AI features integrated directly into their .NET applications. Because of this demand, developers who know how to integrate AI into ASP.NET Core applications are becoming more valuable in the job market.
In this guide, we will build a simple ASP.NET Core Web API that connects to the OpenAI API. The API will accept a user question and return an AI-generated response.
Why This Topic Is Trending
Artificial Intelligence is being used in many areas of modern software development.
Examples include:
AI Chatbots
AI Content Generators
AI Customer Support Systems
AI Code Assistants
Many companies want these capabilities inside their applications. Learning how to integrate AI into ASP.NET Core applications helps developers build smarter software systems and stay aligned with modern technology trends.
What We Will Build
In this tutorial, we will create a simple AI-powered ASP.NET Core application.
The application will:
Create an ASP.NET Core Web API
Connect the API to OpenAI
Send a question to the AI service
Receive and return the AI response
The implementation is designed to be simple and beginner-friendly.
Step 1: Create an ASP.NET Core Web API
Open Visual Studio and create a new project.
Select:
ASP.NET Core Web API
Click Next, configure the project settings, and then click Create.
This will generate a basic Web API project structure.
Step 2: Install Required Package
Open the NuGet Package Manager and install the following package:
OpenAI
You can also install it using the Package Manager Console.
Install-Package OpenAI
This package allows your .NET application to communicate with the OpenAI API.
Step 3: Get an OpenAI API Key
To connect your application with OpenAI, you must generate an API key.
Steps:
Create an account on the OpenAI website
Generate a new API key
Copy the generated key
Important: Keep your API key private and never expose it publicly in source code repositories.
Step 4: Add API Key in appsettings.json
Add your API key to the configuration file.
{
"OpenAI": {
"ApiKey": "YOUR_API_KEY_HERE"
}
}
Using configuration files helps keep sensitive information separate from application code.
Step 5: Create AI Service
Create a folder named Services in your project.
Inside that folder, create a new file named AIService.cs.
using OpenAI;
using OpenAI.Chat;
using Microsoft.Extensions.Configuration;
public class AIService
{
private readonly string _apiKey;
public AIService(IConfiguration configuration)
{
_apiKey = configuration["OpenAI:ApiKey"];
}
public async Task<string> AskAI(string question)
{
var client = new OpenAIClient(_apiKey);
var response = await client.ChatEndpoint.GetCompletionAsync(new ChatRequest()
{
Model = "gpt-3.5-turbo",
Messages = new List<ChatMessage>
{
new ChatMessage(ChatRole.User, question)
}
});
return response.FirstChoice.Message.Content;
}
}
Explanation
OpenAIClient connects the application to the AI service.
ChatRequest sends a message to the AI model.
Model specifies which AI model should generate the response.
ChatMessage contains the user's question.
response.FirstChoice.Message.Content returns the generated AI answer.
Step 6: Register Service in Program.cs
Register the service inside your application's dependency injection container.
builder.Services.AddSingleton<AIService>();
This allows the AI service to be injected into controllers.
Step 7: Create Controller
Create a controller named AIController.cs.
using Microsoft.AspNetCore.Mvc;
[ApiController]
[Route("api/[controller]")]
public class AIController : ControllerBase
{
private readonly AIService _aiService;
public AIController(AIService aiService)
{
_aiService = aiService;
}
[HttpGet]
public async Task<IActionResult> Ask(string question)
{
var result = await _aiService.AskAI(question);
return Ok(result);
}
}
This controller receives a question from the user and forwards it to the AI service.
Step 8: Test the API
Run the application and test the endpoint using a browser or Postman.
Example request:
GET https://localhost:5001/api/ai?question=What is ASP.NET Core?
Output Example
The API will return a response similar to this:
ASP.NET Core is a cross-platform, open-source framework for building modern web applications and APIs using .NET.
Your ASP.NET Core application is now capable of generating AI-powered responses.
Real-World Use Cases
This type of integration can be used to build many AI-powered applications.
Examples include:
AI chatbots
Automated email reply systems
Content generation tools
Resume analysis platforms
Customer support assistants
AI-based code helper tools
These systems help automate workflows and improve productivity.
Why This Skill Is Valuable
Many organizations want to integrate AI capabilities into their applications.
Key industry demands include:
AI integration
Intelligent automation
Smart software systems
Developers who understand both .NET development and AI integration are becoming increasingly valuable in modern software development teams.
Conclusion
In this article, we built a simple ASP.NET Core Web API that integrates with the OpenAI API. The application sends a question to the AI model and returns the generated response.
By following these steps, developers can begin adding AI capabilities to their applications and build smarter software systems.
Join the conversation! Your thoughts help the community grow.