Introduction
Minimal API using .NET and C# offer a fast, lightweight, and modern approach to building HTTP APIs using minimal configuration and boilerplate code. Introduced in .NET 6, Minimal APIs have become increasingly popular for micro services, cloud-native applications, and rapid prototyping due to their simplicity and performance. In this blog, you’ll learn how to build a step-by-step Minimal API using .NET and C#, set up endpoints, and run your first lightweight API project.
Prerequisites for Building a Minimal API in .NET 6
Before you begin, make sure you have:
- .NET SDK installed (version 6 or later)
- A code editor (Like Visual Studio, VS Code, or JetBrains Rider)
- Basic knowledge of C#, REST APIs, and HTTP methods
Creating a Minimal API Using .NET 6 and C# Project Setup
- Create a New Project
Open your terminal or command prompt and create a new Minimal API project using the following command:
dotnet new web -n MinimalApiDemo
Basically, this command creates a new web project named MinimalApiDemo.
- Navigate to the Project Directory
cd MinimalApiDemo
- Open the Project in Your Editor
In the next step, open the project in your favorite code editor to explore and work with the files.
Defining the Model in a Minimal API
Let’s start by creating a simple model for a Todo item. In your project folder, create a new file called TodoItem.cs and add the code below:
public class TodoItem
{
public int Id { get; set; }
public string? Title { get; set; }
public bool IsCompleted { get; set; }
}
Setting Up API Endpoints Using .NET 6 and C#
Now, let’s define the API endpoints that will handle various HTTP requests. To do this, open the Program.cs file and replace or update the existing code with the example shown below:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
List<TodoItem> todos = new();
app.MapGet("/todos", () => todos);
app.MapGet("/todos/{id}", (int id) =>
todos.FirstOrDefault(t => t.Id == id)
);
app.MapPost("/todos", (TodoItem todo) =>
{
todo.Id = todos.Count + 1;
todos.Add(todo);
return Results.Created($"/todos/{todo.Id}", todo);
});
app.MapPut("/todos/{id}", (int id, TodoItem updatedTodo) =>
{
var todo = todos.FirstOrDefault(t => t.Id == id);
if (todo is null) return Results.NotFound();
todo.Title = updatedTodo.Title;
todo.IsCompleted = updatedTodo.IsCompleted;
return Results.Ok(todo);
});
app.MapDelete("/todos/{id}", (int id) =>
{
var todo = todos.FirstOrDefault(t => t.Id == id);
if (todo is null) return Results.NotFound();
todos.Remove(todo);
return Results.NoContent();
});
app.Run();
Explanation of the Code
Dependency Injection
We’re using a singleton list to store TodoItem objects, providing a simple in-memory storage mechanism for demonstration purposes.
API Endpoints
- GET /todos → Retrieve all todo items
- GET /todos/{id} → Get a todo item by ID
- POST /todos → Create a new todo item
- PUT /todos/{id} → Update a todo item
- DELETE /todos/{id} → Delete a todo item
Running the API
Basically, to run the API, execute the following command in your terminal:
dotnet run
Once the application is running, your API will be accessible at http://localhost:5000. You can test the endpoints using tools like Postman or cURL.
Running and Testing the Minimal API
Example Requests
- Create a new Todo item using a POST request
curl -X POST http://localhost:5000/todos -H "Content-type: application/json" -d '{"title": "Learn Minimal APIs", "isCompleted": false}'
- Next, we’ll retrieve all the Todo items using a GET request
curl -X GET http://localhost:5000/todos/1 -H "Content-type: application/json" -d '{"title": "Learn Minimal APIs", "isCompleted": true}'
- Modify an Existing Todo Item
curl -X PUT http://localhost:5000/todos/1 -H "Content-type: application/json" -d '{"title": "Learn Minimal APIs", "isCompleted": true}'
- Remove a Todo Item
curl -X DELETE http://localhost:5000/todos/1
Conclusion
Minimal API using .NET provide a clean, fast, and efficient way to build small-scale services without the overhead of traditional controllers. They are perfect for microservices, cloud-native apps, prototyping, and scenarios where performance and simplicity matter.
With just a few lines of C# code, you can create fully functional endpoints, test them quickly, and scale from simple demos to production-ready services.
Whether you’re a beginner or an experienced .NET developer, Minimal API are a powerful addition to your development toolkit.