DEV Community

Cover image for How to use Insomnia to test your API
Matheus Maximiliano
Matheus Maximiliano

Posted on

How to use Insomnia to test your API

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

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

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

Environment Variables

Environment Variables

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

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

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

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

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

json return

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

Top comments (3)

Collapse
 
gabrielgomeso profile image
Gabriel Gomes de Oliveira

Next time I'm testing a API I will be following this :D

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ

I like it too. Prefer it to Postman

Collapse
 
matheusmprado profile image
Matheus Maximiliano

Yes, I really like the insomnia interface, for me, is most better than postman