With generative AI's explosive growth, developers are no longer constrained to static business logic. The ability to build, summarize, explain, and even dynamically generate code or SQL queries has been added to applications. Generative AI APIs (such as Hugging Face, Azure OpenAI, or OpenAI) with.NET 8 can be combined to create intelligent microservices that can generate and comprehend natural language. Learn how to create a microservice driven by generative AI in this tutorial by utilizing the OpenAI GPT API and.NET 8 Minimal APIs.

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); }"}

Step 6. Containerize with Docker (Optional)
To make it cloud-ready, create a Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "GenerativeAIMicroservice.dll"]

Then run:
docker build -t generative-ai-service .
docker run -p 8080:80 generative-ai-service

Step 7. Real-World Use Cases

ScenarioDescription

Code Assistant

Generate code snippets based on developer prompts

Chatbot Backend

Provide intelligent responses in chat systems

SQL Generator

Convert text prompts into database queries

Content Creation

Auto-generate text, descriptions, and blogs

AI Documentation Service

Summarize and document APIs automatically

Architecture Overview

Client App (UI)

Generative AI Microservice (.NET 8)

OpenAI / Azure OpenAI API

AI-Generated Response

This architecture makes the AI layer modular, secure, and reusable across multiple projects.

Security Considerations
Do not hardcode API keys — use:

dotnet user-secrets set "OpenAIKey" "sk-your-key"

and retrieve it via:

builder.Configuration["OpenAIKey"]

  1. Limit tokens and rate of calls
  2. Sanitize user inputs before sending to AI API
  3. External References

Conclusion
By integrating Generative AI APIs into a .NET 8 microservice, you can bring AI-driven intelligence into any system — whether for content creation, coding automation, or chatbot applications. This architecture is modular, scalable, and ready for enterprise deployment, bridging traditional software engineering with next-generation AI development.