Building Your First Minimal Web API in .NET 9

π§ Introduction
ASP.NET Core in .NET 9 makes it easier than ever to build fast and lightweight REST APIs using Minimal APIs. These are ideal for small apps, microservices, and learning the ropes.
This tutorial walks you step by step through creating a simple API that manages a to-do list.
π§° Prerequisites
.NET 9 SDK
A code editor like Visual Studio Code
Basic C# knowledge
π οΈ Step 1: Create a New Project
Open your terminal and run:
dotnet new webapi -n TodoApi -minimal
cd TodoApi
This creates a Minimal API project with a basic HTTP request pipeline and no MVC boilerplate.
Whatβs inside the project?
Program.cs
: where the whole app is defined.appsettings.json
: configuration file.No controllers, no views β just pure endpoint mapping.
π Step 2: Add a Todo
Record Model
Create a new file named Todo.cs
and paste this:
namespace TodoApi;
public record Todo(int Id, string Title, bool IsDone);
record
is perfect for immutable, lightweight data containers. It automatically generates constructor,ToString()
, equality methods, etc.
π Step 3: Define Endpoints in Program.cs
Open Program.cs
and replace the contents with this:
using TodoApi;
var builder = WebApplication.CreateBuilder(args);
// Register API explorer and Swagger for documentation
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApiDocument(); // .NET 9 feature
var app = builder.Build();
// In-memory list to store todos
var todos = new List<Todo>();
// Get all todos
app.MapGet("/todos", () => todos);
// Get a specific todo
app.MapGet("/todos/{id:int}", (int id) =>
{
var todo = todos.FirstOrDefault(t => t.Id == id);
return todo is not null ? Results.Ok(todo) : Results.NotFound();
});
// Add a new todo
app.MapPost("/todos", (Todo todo) =>
{
var newTodo = todo with { Id = todos.Count + 1 };
todos.Add(newTodo);
return Results.Created($"/todos/{newTodo.Id}", newTodo);
});
// Start the app
app.Run();
π Key Concepts
MapGet
andMapPost
are routing methods.Results.Ok()
,Results.NotFound()
, etc. return HTTP responses.The todo list is in-memory β no database for now.
This approach is clean, short, and great for beginners.
π Step 4: Run & Test the API
Run the project:
dotnet run
You should see something like:
Now listening on: https://localhost:5001
π Test your API
Try hitting the endpoints:
curl https://localhost:5001/todos
You can also test via the built-in Swagger UI by visiting:
https://localhost:5001/swagger
This gives you an interactive interface to explore your API. π§ͺ
βοΈ Step 5: Publish with Ahead-of-Time Compilation (Optional)
Want to make it even faster for deployment?
.NET 9 supports native AOT publishing:
dotnet publish -c Release -p:PublishAot=true
This compiles your app down to a native binary β startup time is nearly instant.
π§ Bonus Tips for Beginners
β This app does not persist data. Restarting the app wipes the todo list.
β Try using Postman or Thunder Client (VS Code extension) for testing.
π¦ You can integrate with EF Core and SQLite in the next step (see Article 2!).
π Conclusion
Youβve just created your first REST API using .NET 9 and C# Minimal APIs! π
This pattern is beginner-friendly, fast, and scalable.
Subscribe to my newsletter
Read articles from Dev In Real Life directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Dev In Real Life
Dev In Real Life
Developer at heart. I love building with .NET, Java, and playing in the clouds (AWS & Azure) βοΈ Sharing my real dev life, one project at a time. #DevInRealLife