4 Steps to Automatically Generate API Documentation for ASP.NET Core Projects
Tags: productivity, aspnetcore, swagger, beginners
Published: 2019-05-29 10:49:36

---
title: 4 Steps to Automatically Generate API Documentation for ASP.NET Core Projects
published: true
tags: productivity,ASP.NET Core,Swagger,beginners
canonical_url: https://blog.syncfusion.com/post/automatically-generate-api-docs-for-asp-net-core.aspx
cover_image: https://thepracticaldev.s3.amazonaws.com/i/jecf2m6to6xrggmuair8.png
---

Have you ever wondered whether generating API documentation automatically is possible while writing the code? Yes, this is possible. For developers, writing documentation is a painful part of the process. [Swagger](https://swagger.io/), also known as [OpenAPI](https://www.openapis.org/), solves this problem by generating useful documentation and help pages for web APIs. It not only generates read-only help pages but ones that are interactive as well, which can even be used for testing APIs.

I will walk you through the steps needed to use document generation capability for your API. We’ll be using the **Swashbuckle** NuGet package to add document generation ability to our ASP.Net Core project.

First, we install a NuGet package in our project. The NuGet package used here is [Swashbuckle.AspNetCore](https://www.nuget.org/packages/Swashbuckle.AspNetCore).

In Visual Studio, go to **Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution**. Search for the package named **Swashbuckle.AspNetCore** and install it in your project.

<figure>
  <img src="https://blog.syncfusion.com/wp-content/uploads/2019/05/Installing-Swashbuckle.AspNetCore-nuget-package-1.png" alt="Installing Swashbuckle.AspNetCore NuGet package" style="width:100%">
  <figcaption>Installing Swashbuckle.AspNetCore NuGet package</figcaption>
</figure>

The next few steps will show you how to configure Swagger by adding a few lines of code in your project **Startup.cs** file.

**Step 1:** Include the Swagger namespace.

<figure>
  <img src="https://blog.syncfusion.com/wp-content/uploads/2019/05/Including-swagger-namespace-1.png" alt="Including Swagger namespace" style="width:100%">
  <figcaption>Including Swagger namespace</figcaption>
</figure>

**Step 2:** Then, register the Swagger service in the **ConfigureServices** method.

<figure>
  <img id=example1 src="https://blog.syncfusion.com/wp-content/uploads/2019/05/Configuring-Swagger-service-1.png" alt="Configuring Swagger service" style="width:100%">
  <figcaption>Configuring Swagger service</figcaption>
</figure>

**Step 3:** Enable Swagger middleware in the **Configure** method to configure [Swagger UI](https://swagger.io/tools/swagger-ui/). That’s it, the configuration is now over.

<figure>
  <img src="https://blog.syncfusion.com/wp-content/uploads/2019/05/Configuring-Swagger-UI-1.png" alt="Configuring Swagger UI" style="width:100%">
  <figcaption>Configuring Swagger UI</figcaption>
</figure>

**Step 4:** Now run your API in a browser and navigate to your API base URL. In my case, it is localhost:44314. You will see an interactive docs page for your API up and running in no time without you writing even a single line for the document.

![The swagger wizard](https://blog.syncfusion.com/wp-content/uploads/2019/05/Swagger-Wizard-1.png)Check out this working sample project [here](https://github.com/SyncfusionExamples/api-docs-swagger-sample).

## Conclusion

So, get started and add docs to your existing API following these simple steps. Using this, you can even create version-based docs for your API. Also, be sure to add authorization to your API docs to prevent outsiders from misusing them.

Additional reference: [https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetcore-2.2](https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?view=aspnetcore-2.2)

The post [4 Steps to Automatically Generate API Documentation for ASP.NET Core Projects](https://blog.syncfusion.com/post/automatically-generate-api-docs-for-asp-net-core.aspx) appeared first on [Syncfusion Blogs](https://blog.syncfusion.com).