Creating a Swagger Specs Document
Tags: documentation, tutorial, api, swagger
Published: 2023-01-21 20:44:53

## What is Swagger?

Swagger is a documentation tool that will help you describe the structure of your API.  Swagger does this by reading a JSON file which contains a detail description of your entire API.  The specification asks you to include information like:

- What operations will your API support?
- What are your API's parameters and what does it return?
- Does your API need some authorization?
- Terms & conditions, contact information, license to use the API, etc.

There are two ways to create your Swagger spec for your API.

1. Generate your specs manually.  Personally I like this approach better just because it helps you design your API before writing any code.
2. Have the specs generated automatically from annotations on your source codes. Check [Swagger docs](https://swagger.io/open-source-integrations/) for a list of tools that let you generate Swagger from your code.

## Creating Swagger specs

To create your Swagger specs, first go to [swagger.io](https://swagger.io) and sign in

![Swagger home page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nijdcqy2bytpjtw5rz3r.png)

So lets start by creating our first Swagger specs doc

![Create new](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7g9a2y7fmfjuyvtx58o1.png)

For the specification we leave the OpenAPI 3.0.x value, and for the template we change it to OAuth 2.0 - Password and finally give our specs a name, we will call it New_API and hit on the Create API button.

Once the create specs finishes, we will get the following screen:


![Start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/diagdp4f6qmir1ws4zmb.png)

All of the docs in the [swagger.io](https://swagger.io) must be written in YAML format, which I think it is easier to work with.

## Understanding Each Section

### The info Section

![Info](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88t3zgi3c1g6uclthw7r.png)

Here we define some basic information about our API like its name, version and description

### The paths Section

![Paths](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ez2qq4yw83zmqw6wejqo.png)

Here we will be defining all our routes and methods that our API will listen to, giving information such as what type of data we will be expecting to receive and return as all of the status codes we will be working with.

### The components Section

![Components](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7lltthk65d1mulyx3418.png)

Here we will be defining the schemas of the data that we are going to be receiving and returning

## Create our route

For visual organization in our documentation, we first need to create a tags section where we will group each of the routes

![Tags](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rboteqid1kf7smmram1o.png)

You can add as many tags as necessary, just remember that with YAML files the indentation is important

Next inside the path section we start creating our routes, first we define the endpoint we are going to work with, and for each endpoint we need to define what methods will it allow (GET, POST, PUT, PATCH, DELETE)

When we write the description with a multiple line format we are allowed to use MarkDown language to format to format the text.  

![Route](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6a8ftppydmay579z47ml.png)

Congratulations!!! We've just created our first endpoint, now we need to model the data we will be receiving.

At the end you will have your documentation looking as the following:

![API 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zplu5x7kbrssuft0je0q.png)

![API 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a18uxvnvd68u028cgev2.png)

We will continue with the Swagger specs definition in a later post