1. Introduction
In every modern development project, API documentation plays a crucial role in helping developers understand and integrate backend services efficiently. However, maintaining accurate, up-to-date API documentation is often one of the most neglected tasks in software projects.
Developers frequently make changes to API endpoints, models, or parameters, but forget to update the Swagger descriptions or XML comments — resulting in outdated and confusing documentation.
To solve this problem, we can now use Artificial Intelligence (AI) to automatically generate or update Swagger descriptions based on the actual code, business context, and naming conventions.
This article explains how to integrate AI-based documentation generation into your ASP.NET Core Web API project using OpenAI or Azure OpenAI along with Swagger (Swashbuckle). You’ll learn how to automatically generate endpoint summaries, parameter details, and model descriptions — all in a clean and developer-friendly way.
2. Why Automate API Documentation?
Manually maintaining Swagger documentation is time-consuming and error-prone. The main challenges include:
Developers forget to update Swagger descriptions after changing code.
Descriptions are inconsistent across multiple APIs.
No standard tone or style in documentation.
Lack of clarity for external developers integrating the API.
AI-powered documentation addresses these problems by:
Generating contextual and natural language explanations.
Keeping descriptions synchronized with the actual source code.
Saving time for developers.
Providing documentation in a consistent, easy-to-read tone.
3. Workflow Overview
Let’s visualize how AI can automatically enhance Swagger documentation in an ASP.NET Core API project.
Flowchart: AI-Generated Swagger Workflow
Developer updates API code
|
v
Swagger Middleware scans all Controllers and Actions
|
v
AI Engine analyzes code and generates summaries (via OpenAI API)
|
v
Swagger JSON enriched with AI-generated descriptions
|
v
Updated Swagger UI with smart documentation
This flow ensures that every time you build or deploy your API, AI reviews your code and updates the Swagger documentation automatically.
4. Project Setup
Step 1: Create a new ASP.NET Core Web API Project
dotnet new webapi -n SmartApiDocs
cd SmartApiDocs
Step 2: Add Swagger Support
Install the Swashbuckle.AspNetCore package:
dotnet add package Swashbuckle.AspNetCore
Step 3: Add OpenAI SDK (or Azure OpenAI SDK)
For example, using the OpenAI .NET client:
dotnet add package OpenAI
Or, if you’re using Azure OpenAI:
dotnet add package Azure.AI.OpenAI
5. Configuring Swagger in ASP.NET Core
In Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
This sets up basic Swagger documentation. When you run the project, you can access Swagger UI at /swagger.
6. The Challenge: Missing Descriptions
A standard Swagger file generated by Swashbuckle may look like this:
{"paths": {
"/api/customer/{id}": {
"get": {
"summary": "",
"description": "",
"parameters": [
{ "name": "id", "description": "" }
]
}
}}}As you can see, all the important descriptions are missing. This is where we’ll use AI to fill the gaps.
7. Integrating AI for Auto-Generated Descriptions
Step 1: Create an AI Helper Service
Create a new class AIService.cs:
using OpenAI_API;
public class AIService
{
private readonly OpenAIAPI _api;
public AIService(string apiKey)
{
_api = new OpenAIAPI(apiKey);
}
public async Task<string> GenerateDescriptionAsync(string endpointName, string parameters)
{
var prompt = $"Write a short API documentation summary for an endpoint named '{endpointName}' with parameters {parameters}. " +
"Explain in a professional tone suitable for Swagger documentation.";
var response = await _api.Completions.GetCompletion(prompt);
return response.ToString();
}
}
This service uses OpenAI to generate contextual documentation text based on endpoint names and parameters.
Step 2: Modify Swagger Generation
We’ll hook into Swagger generation to enrich endpoints automatically.
Create a new file AIDescriptionDocumentFilter.cs:
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
public class AIDescriptionDocumentFilter : IDocumentFilter
{
private readonly AIService _aiService;
public AIDescriptionDocumentFilter(AIService aiService)
{
_aiService = aiService;
}
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
foreach (var path in swaggerDoc.Paths)
{
foreach (var operation in path.Value.Operations)
{
if (string.IsNullOrEmpty(operation.Value.Description))
{
var endpointName = path.Key;
var parameters = string.Join(", ", operation.Value.Parameters.Select(p => p.Name));
var description = _aiService.GenerateDescriptionAsync(endpointName, parameters).Result;
operation.Value.Description = description;
operation.Value.Summary = description.Length > 100 ? description.Substring(0, 100) + "..." : description;
}
}
}
}
}

Join the conversation! Your thoughts help the community grow.