# Minimal APIs: Fast, Lean, and Powerful in ASP.NET Core

Sometimes you don’t need the full MVC setup — you just want to expose a few endpoints quickly and cleanly. That’s where **Minimal APIs** shine in ASP.NET Core.

In this post, we’ll explore how to create Minimal APIs that are fast, lean, and production-ready.

---

## 🚀 What Are Minimal APIs?

Minimal APIs allow you to define endpoints **without controllers or attributes**. You write route handlers directly in `Program.cs` using clean, expressive syntax.

Perfect for:
- Microservices
- Internal tools
- Prototypes
- Lightweight APIs

---

## 🛠️ Creating a Minimal API Project

```bash
dotnet new web -n MinimalApiDemo
cd MinimalApiDemo
dotnet run
```

This template has no controllers — just a single `Program.cs`.

---

## 🧱 Basic Example

```csharp
var app = WebApplication.Create(args);

app.MapGet("/", () => "Hello World!");

app.MapGet("/api/products", () =>
{
    var products = new[] {
        new { Id = 1, Name = "Apple" },
        new { Id = 2, Name = "Orange" }
    };
    return Results.Ok(products);
});

app.Run();
```

✅ No controller classes. Just functions.

---

## 📦 Dependency Injection Still Works

```csharp
app.MapGet("/config", (IConfiguration config) =>
{
    var appName = config["AppName"];
    return Results.Ok(new { appName });
});
```

You can inject any service registered in the DI container.

---

## 🔐 Add Authentication & Authorization

```csharp
app.MapGet("/secure", [Authorize] () => "Secure data");
```

Minimal APIs fully support `[Authorize]`, `Swagger`, CORS, and all major ASP.NET Core features.

---

## 🧩 Grouping Routes (Optional)

For better structure, group endpoints:

```csharp
var products = app.MapGroup("/api/products");

products.MapGet("/", GetAllProducts);
products.MapPost("/", CreateProduct);
```

---

## 🧪 Swagger for Minimal APIs

Add these in `Program.cs`:

```csharp
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

app.UseSwagger();
app.UseSwaggerUI();
```

It works just like regular APIs!

---

## ⚠️ When Not to Use Minimal APIs

- When your app grows and needs organized controllers/services
- When using lots of filters or custom attributes
- When you want a strong separation of concerns

Minimal APIs are great, but don't replace MVC entirely.

---

## ✅ Up Next

Next: background tasks and scheduled jobs using `IHostedService` and Hangfire.

➡️ [Background Tasks & Hosted Services →](./aspnetcore-background-tasks)

Small APIs. Big power. Let’s go! 🔥

