How to configure Swagger in a .net core API from scratch

As a developer I often use some code from other projects. This is a pretty convenient and productive way to deal with issues or things to tackle. However, sometimes there is a case when you have to setup something from scratch and you want to do it step by step without copying solution from other projects. The reasoning is that in other projects there are customisations which you don’t want to use and you don’t remember if they are necessary for the functionality you crave for. This is my case when it comes to Swagger as it is something that you setup once and forget.

The problem arise when you have to create an empty project, setup the mentioned functionality, and in other projects you have plentiful of customisations you don’t want to use. I had to configure Swagger lately and I didn’t remember which parts are necessary and which weren’t. That’s why I created this post, to firstly share a bit of my knowledge, and secondly to have my own reference point.

To resolve this issue, firstly have your .net core api created.

Then, install nuget package Swashbuckle.AspNetCore.SwaggerGen either by package manager console using Install-Package Swashbuckle.AspNetCore.SwaggerGen or by using nuget packages manager or by dotnet add package Swashbuckle.AspNetCore.SwaggerGen.

Now, go to Startup.cs file and in method ConfigureServices add this code:

services.AddSwaggerGen(cfg => 
cfg.SwaggerDoc("api", new OpenApiInfo {Title = ApiName, Version = ServiceVersion});

Afterwards, in method Configure add the following line:


And voila, if you go to your service endpoint and add /swagger/api/swagger.json to the url, you’ll see the perfectly formatted open api definition for your service!

I believe that’s not enough, and you’d like to have a graphical interface for swagger. The rest of this post will handle that for you 🙂

Install nuget package (again, either by Package Manager Console or its UI equivalent) named Swashbuckle.AspNetCore (it has its own dependency to Swashbuckle.AspNetCore.SwaggerGen so in the future you could start with this package).

Now go to Startup.cs and go to method Configure add this line

app.UseSwaggerUI(opt => opt.SwaggerEndpoint("/swagger/api/swagger.json", $"My Api"));

And you’re done, all is good now and you can use Swagger UI from you app url. Simply go to your API and add /swagger where the beautiful UI lays!


If you start with Swashbuckle.AspNetCore you’ll still have to add all the mentioned pieces of code!

Have fun, you’re good to go! 🙂

Leave a Comment