Testing API calls in Insomnia

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Hello!I have suffered from the problem of insomnia very often before. I was looking for a good remedy for my problems a very long time. I searched a lot but still found a cool drug Zopiclone They showed themselves remarkably after just a few days of admission. Come here EU Meds's sale page and buy the best cure for insomnia! They are really a legit company using doctors and real pharmacies.
Thanks Richard!
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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.
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.

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

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.

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.

And this way, we can make authenticated requests to our API.
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.

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.

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

This makes it super easy to switch between our environments without having to change the URL all the time.
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.

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.

And that's it. We now have an easy way to switch environments and even automatically set the access_token!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter