Integrate Generative AI capabilities into your microservices using modern .NET architecture
🧠 Introduction
With the rapid rise of Generative AI, developers are no longer limited to static business logic. Applications can now create, summarize, explain, and even generate code or SQL queries dynamically.
By combining .NET 8 and Generative AI APIs (like OpenAI, Azure OpenAI, or Hugging Face), you can build intelligent microservices capable of natural language understanding and generation.
In this article, you’ll learn to build a Generative AI-powered microservice using .NET 8 Minimal APIs and OpenAI GPT API.
⚙️ Prerequisites
Before we begin, ensure you have the following setup:
✅ Tools Required
.NET 8 SDK
Visual Studio 2022 / VS Code
OpenAI API key → https://platform.openai.com
(Optional) Docker for microservice containerization
🧩 Project Setup
Step 1. Create a New .NET 8 Web API Project
In your terminal or command prompt:
dotnet new webapi -n GenerativeAIMicroservice
cd GenerativeAIMicroservice
Step 2. Clean Up the Template
Remove default controllers and WeatherForecast examples.
We’ll use a Minimal API style for simplicity.
Step 3. Add Dependencies
Install the following NuGet packages:
dotnet add package OpenAI_API
dotnet add package Newtonsoft.Json
These allow communication with the OpenAI GPT API and handle JSON serialization.
🧱 Step 4. Create the AI Service
Create a new file: Services/AiService.cs
using OpenAI_API;
using System.Threading.Tasks;
namespace GenerativeAIMicroservice.Services
{
public class AiService
{
private readonly OpenAIAPI _api;
public AiService(string apiKey)
{
_api = new OpenAIAPI(apiKey);
}
public async Task<string> GenerateTextAsync(string prompt)
{
var result = await _api.Completions.GetCompletion(prompt);
return result;
}
}
}
This service will handle all Generative API communication.
🌐 Step 5. Create the Minimal API Endpoint
In your Program.cs file, add:
using GenerativeAIMicroservice.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(new AiService("sk-your-openai-api-key")); // Replace with your key
var app = builder.Build();
app.MapPost("/api/generate", async (AiService aiService, PromptRequest request) =>
{
if (string.IsNullOrEmpty(request.Prompt))
return Results.BadRequest("Prompt is required.");
var response = await aiService.GenerateTextAsync(request.Prompt);
return Results.Ok(new { Output = response });
});
app.Run();
record PromptRequest(string Prompt);
🧠 Example Request
You can now test your Generative API microservice using Postman or curl.
POST Request
URL:
https://localhost:5001/api/generate
Body (JSON):
{"Prompt": "Write a C# function to reverse a string"}Example Response
{"Output": "public string ReverseString(string s) { char[] arr = s.ToCharArray(); Array.Reverse(arr); return new string(arr); }"}
Join the conversation! Your thoughts help the community grow.