Using authentication in a Minimal API with Swagger
Download the code example to learn how to add API key security to Minimal API endpoints
You can learn about the simplest way to add authentication to Minimal API endpoints, or watch the video.
Download the code example
You'll need to fill out the code example form. We will send you an email where you can download the code example.
Software
This is the software that will need to be installed onto your machine.
Visual Studio 2026. Version 18.0.0 or above. It will work with the free community version.
.NET 10 SDK. Version 10.0.0 or above.
The application
Open up RoundTheCode.MinApiSecurity.slnx, and run the application. The Swagger documentation will load at https://localhost:7083/swagger.
Try running the single endpoint and it will return 401 Unauthorized.
Then, click on the Authorize button in Swagger and enter the API key. The API key can be found in appsettings.Development.json.
Run the endpoint now and it should return 200 OK.
Download this code example
Enter your email and we'll send you a download link.
Related code examples
Download a .NET 10 Web API integrated with Minimal APIs and SQL Server alongside Entity Framework Core and FluentValidation.
Download an Minimal API code example that allows you to test routes, parameters, results, filters, authentication and unit testing.
Download this code example
Enter your email and we'll send you a download link.