OK
HomeBlogResourcesAboutContactSponsor Start Reading →
.NET

How to Build an AI Chatbot API in ASP.NET Core with Clean Architecture

SEO keywords: AI chatbot API, ASP.NET Core AI, .NET AI integration, C# chatbot, Clean Architecture Web API.

AI chatbots are becoming a normal feature in modern web applications, but a production chatbot should not be built as a quick controller that directly calls an AI provider. A better approach is to build an ASP.NET Core Web API with clean boundaries, provider-neutral services, logging, validation, and deployment-ready configuration.

Why Use Clean Architecture for an AI Chatbot?

Clean Architecture keeps your chatbot workflow testable and flexible. Your API should depend on an application service, not directly on OpenAI, Azure OpenAI, Amazon Bedrock, or any specific model vendor. This makes it easier to change providers later without rewriting controllers.

Recommended Project Structure

src/
  CodeWithChai.Api/
    Program.cs
    Endpoints/ChatEndpoints.cs
  CodeWithChai.Application/
    Chat/IChatAssistant.cs
    Chat/ChatAssistant.cs
  CodeWithChai.Infrastructure/
    Ai/IAiClient.cs
    Ai/HttpAiClient.cs
  CodeWithChai.Domain/
    ChatAnswer.cs

Create the Chat Endpoint

The endpoint should be thin. It receives the request, validates basic input, delegates work to the application layer, and returns the response.

app.MapPost("/api/chat", async (
    ChatRequest request,
    IChatAssistant assistant,
    CancellationToken cancellationToken) =>
{
    if (string.IsNullOrWhiteSpace(request.Message))
    {
        return Results.BadRequest("Message is required.");
    }

    var response = await assistant.ReplyAsync(
        request.Message,
        cancellationToken);

    return Results.Ok(response);
});

public sealed record ChatRequest(string Message);

Build a Provider-Neutral AI Service

The application layer should describe what your system does. The infrastructure layer should decide how the external AI call is made.

public interface IChatAssistant
{
    Task<ChatAnswer> ReplyAsync(
        string message,
        CancellationToken cancellationToken);
}

public sealed class ChatAssistant : IChatAssistant
{
    private readonly IAiClient _aiClient;

    public ChatAssistant(IAiClient aiClient)
    {
        _aiClient = aiClient;
    }

    public async Task<ChatAnswer> ReplyAsync(
        string message,
        CancellationToken cancellationToken)
    {
        var prompt = $"Answer clearly for a .NET developer: {message}";
        var answer = await _aiClient.GenerateAsync(prompt, cancellationToken);

        return new ChatAnswer(answer, DateTimeOffset.UtcNow);
    }
}

Production Checklist

  • Store API keys in environment variables or a secret manager.
  • Add request timeouts and retry policies.
  • Log failures without storing sensitive prompts.
  • Add rate limits for public endpoints.
  • Write integration tests around the application service.
  • Use Docker for consistent deployment.

Final Thoughts

A strong AI chatbot API is not just a model call. It is a maintainable ASP.NET Core system with clean architecture, security, observability, and provider flexibility. This approach helps your .NET application stay ready for future AI changes.

Finished reading. Nice work.

Related Articles