Basic usage of Insomnia
Tags: javascript, tutorial, beginners
Published: 2022-09-29 11:29:59

## Insomnia  
It's an API design platform like postman. Most function, except Sync, is free.

##1.Download  
[Insomnia](https://insomnia.rest/)  

##2.Basic Usage  
(If you already know all the buttons, just skip.)

###2.1 Create new collection  

![Insomnia app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gwsavn1m9bibd1lma2ej.png)  

###2.2 Create new folder and http request

![In new collesction](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6s9amohazz50iavbmiqg.png)

###2.3 Folder settings   
Right click the folder, choose settings.  
In settings, you could change the folder name(F2 cannot) and click add description about the folder.  
All the description will show in the final export document.  

![Right click the folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xkh1vr6n2trch79smrjh.png)


![Setting image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rx4x35esh50w78poudi7.png)

###2.4 Request Area  
JSON： request body. could choose any other type.
Bearer： choose token type, add value.
Query: add fields. could use [gt] to filter data
Header: request header.
Doc: single request document

![Request area](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uryl3rqkvelgx8wm72zq.png)

###2.5 Add Environment
Click add button beside Sub Environments.  
Add environment variable in the right area.  
Caution: every field and value must be wrapped in "", including the Insomnia variable(will be mentioned later).

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

![Environment management](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/50xoqxh5dmmuxy33taci.png)

## 3 Common scenario  
Doesn't like postman, Insomnia can only use build in method or plugins.

### 3.1 Reuse a token from login request  

Scenario description:  
1) Login response body will contain a token;
2) You need the token to make other requests;


![Login image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/838noif6znzdsv3kqjvy.png)

Steps to follow:  
1) Open the environment, create a token variable.    
In value, type a 'r'(or any other character, wait a second,  Insomnia will show a list to choose from) and choose 'Response => Body Attribute'.    
2) Click the 'Response => Body Attribute', config the method in 'Edit Tag'.  
3) It used [JSONPath](https://goessner.net/articles/JsonPath/) to filter token.  
4) Refer the variable in the headers. Type 't' and it will show the variable list.

![Environment variable](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kt4iz55w1z3lnvkiwlzp.png)


![Method config](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sm2lyrak2hjztchpc3t.png)


![Request Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9bzzm05mpp2cno4aqv9d.png)

### 3.2Reuse token from cookie

Scenario description:   
1) Using cookie to send and receive token.  
As shown in the login request image.  
2) request send token prefix with bear.  

![Login request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7r3uf43vz95jmbe5bjg.png)

Steps to follow:   

1) Choose 'Bearer', type in cookie and click it to config.  
2) Choose your url and type in cookie name and then you can copy it to any request you want.

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


![Cookie manage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjb05qheucmo67m2ipmp.png)

## 4 Create Document  
I only find the way using command line, don't know if there is other. 

[Reference doc](https://dev.to/dibinjose/create-an-api-documentation-with-insomnia-plugin-55dj) 

To follow the steps, you need to download [insomnia-documenter](https://github.com/insomnia-documenter/insomnia-documenter) first.  

1. Click the collection and choose 'import/export'.  
2. Choose current project in 'Export data'.  
3. Choose 'Insomnia v4(JSON)' format in export.
4. In the export folder, use 'cmd' to open the terminal and type as below.  
(Remember to replace 'filename' to your export filename)  
`npx insomnia-documenter --config filename.json --output insomnia-final-result`  
5. 'insomnia-final-result' is your final document. Use VScode or command line to open.  


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


![Current report](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d85kr89acxba4ys875vu.png)


![Choose export request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ph9zxauwfru22idl8ugs.png) 


![Choose format](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/inpsoavu8ni907auctsf.png)


![Document in browser](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/skcojqzwyyspo2us8p30.png)


If you have better solution, please do leave comment below.  
Thank you!