How to use Insomnia to test your API
Tags: beginners, backend, insomnia, api
Published: 2022-01-18 02:13:36

## What is Insomnia? 
If you are developing a set of APIs, Insomnia is a great software to test your REST or GraphQL APIs in terms of functionality, performance, besides a well simple alternative.

The goal of this article is to share how to get the best of it, show some of the tips and tricks that you can use.

**Insomnia** is available on Linux, Windows and macOS, in this [link](https://insomnia.rest/products/insomnia)


## Get Organized
When you work with a lot a projects, you need staying organized for that you can do maintainability your projects. So, Insomnia have two ways to do start, in first, we have a **Request Collection**, and inside, we have **folders**.

![Dashboard with Request Collections](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjmebkuxyqr6ihagvsft.jpg)


## Request Collection or Workspaces
Request collections or Workspace are the first item that need created to start a test your new project or application or even microservice.

In your dashboard, you can view all your Request Collections, and open for you start work.

Besides as well is possible can create environments variables for each collection, like a base url, keys, whatever you need reuse.


## Folders
The folders are items for organization inside your collection to organize your requests.

You can use the folder for organize your requests for product, or group requests of the same domain, or differences paths.

As well folders will continue to have access to the environments variables of your collection.

![Folders in Collection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ocb6pnl59wf7bqe55phz.jpg)


## Environment Variables
![Environment Variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k5jm0l23thuqsqretqhv.jpg)

You can configure your collection a base environments or a sub environments, and switch to a different environment with ease. 

This environments are place to store data as a JSON object that you can modify in whatever moment.

## Create a requests
Inside your collection, you can create your endpoints and choice the type of request you need, `POST`, `GET`, `PUT`, `DELETE` whatever you want. 

![Create a request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6z6gsb5we8foiygl9q0.jpg)

After you created, you need place into a endpoint for testing your API. In this point its possible use a base environments, like a base url for your endpoint.

## Start use

So, suppose we have an API that let us manage a collection of Clients, and we have a tree basic endpoints:

- `POST /client/create` to create a new client;

- `POST /client/auth` to get a access token;

- `GET /client/` to get a data client logged;

**For `POST` route**, we need fill the information's for sign up using a JSON structure.

![POST client create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zvf6zv2ejy0zyowrd68e.jpg)

> The endpoint use a baseUrl environment.

This API is responsible to generate a unique identifier at the creation of a client like a uuid, so after we create our client, and have successful in our request, we need get the data of this client.

Now, after create your client user we need a access token to use Bearer Authenticate for get a client data.

So call your Auth router to get a token authenticate.

After use the Auth, we have this return.

![Auth route](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wznk5141r9vzzx1a6fm1.jpg)

So, finally, open the get client route, and go to second tab _Auth_, and select the "Bearer Token" or whatever you use to authenticate.

In input text token, press `Ctrl+space` (in Windows), and select `response => body attribute`.

## Edit Tag

Edit the tag filter to set path and identifier attribute `$.token`, and adapt the trigger to you need, equal the image below .

![Edit Tag](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g94j7hmel5q57fp3i61b.jpg)

And is it! Now you can get the data client object!

![json return](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54ew1xjobupdbltgw24f.jpg)

## Closing thoughts
Personally, I find Insomnia very easy to work with, we have seen some of the features Insomnia to make us more productive. I hope this article made your development more easy too. 
There are a lot of possibilities to explore. if you want to go further, you can check out [Insomnia documentation](https://docs.insomnia.rest/)