DEV Community

Cover image for Testing API calls in Insomnia
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Testing API calls in Insomnia

So far, we setup an API in Laravel, and a great way to explore your API is making use of API Clients.

Some clients you can consider:

It all does pretty much the same, so it really comes down to which ever you like best.
I recently moved from Postman to Insomnia.

So I'll be showing you how to use Insomnia.

Authentication with our API

The first step is to authenticate with our API.
As you may have seen in our API documentation article, we need to make a post to the login endpoint.

Let's open up Insomnia and add a new request.

Insomnia add new request

Give the request a good name, let's say Login.
On the right side, change the method to POST and the type to Multipart.

Insomnia request builder

Once you've added the request, it shows up the detail view.
Fill out the endpoint you are wanting to test.

In our case: http://localhost:8084/api/login.
In the form, section add two fields: email and password.

POST request in insomnia

As you can see on the right-hand side, we get our response.
This gives us an access_token we can use for further calls.

Let's add the me request next. This will be a GET request to the /me endpoint.

Only now do we have to open up the Bearer section and add the token we just received on the login call.

Insomnia token request

And this way, we can make authenticated requests to our API.

Managing environments

A cool thing most API clients can do is the managing environment.
You often have your own local environment, a staging/testing, and a production environment.

In Insomnia, it's super easy to add these different variables so we can easily switch between them.

Click the No Environment button on the top left corner and click manage environments.

Insomnia environments

Here you will find a Base environment that will have variables that will work for every single one.
But we'll go ahead and add two new ones: a Production and a local one will only use a URL for now.

Adding environments in Insomnia

Now, if we go back to our request, we can change our URL to {{url}}; this will make it a dynamic variable.

Variable environment

This makes it super easy to switch between our environments without having to change the URL all the time.

Automating our access token

Another thing we can automate is the access_token, it will expire, and we then manually need to do the login call again and re-set it.

But we can create another environment variable for this.

Go to the local environment and add an "access_token", start typing response, and show Body Attributes.

You can then click this section and open up the tag details.

We will set the tag to request the login call and filter the \$.access_token, since that is the variable name.
We can then set a trigger behavior to resend when it's expired.

It will then automatically fetch a new token when needed.

Set access_token dynamic

This newly created access token we can then set on our other request in the token field.

Open the me call, and in the Token field, type _.access_token.

Dynamic access token

And that's it. We now have an easy way to switch environments and even automatically set the access_token!

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (5)

Collapse
 
tlylt profile image
Liu Yongliang

Interesting post. Recently I have been building (with other team members) a simple desktop application in Java for API testing. Not yet near the standards & functionalities of the major API clients out there but it has been really fun:) imposter-dev.tk/

Collapse
 
liyasthomas profile image
Liyas Thomas

If you'd like to try an online API development / testing platform - we're building Hoppscotch.io.

GitHub:

GitHub logo hoppscotch / hoppscotch

👽 Open source API development ecosystem. https://hoppscotch.io

Collapse
 
dailydevtips1 profile image
Chris Bongers

Oh nice, i've heard about this before!

Collapse
 
tanzimibthesam profile image
Tanzim Ibthesam

Is it faster than postman

Collapse
 
dailydevtips1 profile image
Chris Bongers

Don't think it makes much of a difference, same thing to me.
But don't find either slow actually?