How to automatically set the access token for authenticated requests in Insomnia
Tags: tutorial, insomnia, testing
Published: 2020-12-06 04:33:53

---
title: How to automatically set the access token for authenticated requests in Insomnia
published: true
date: 2020-12-04 00:00:00 UTC
tags: tutorial, insomnia, testing
canonical_url: https://www.ankursheel.com/blog/automatically-set-access-token-authenticated-requests-insomnia
description: Making your GraphQl endpoint accessible only to authorised user is a common way of securing your website. However, it does make it difficult to use Insomnia to test your requests. How do you make this lets tedious? Let's find out
cover_image: https://www.ankursheel.com/static/609cf4e8ef1447581bb0e46b8aa1cdd9/5a190/request_success.png
---

## Problem

The Graphql endpoint for the API is accessible only to authorised users. Although it is required from a security perspective, it makes testing the API through [Insomnia](https://insomnia.rest/) a little tedious. What if we could automatically insert the bearer token every time we made a request using Insomnia?

## Solution

Let’s start by creating a new environment. Although not entirely necessary, it makes authenticating on different environments easy. _Note: You can give the environment better names such as Dev, Staging, Production etc_

![Creating a new environment](https://www.ankursheel.com/static/7663571681a67ce3efc37a0ecb24d85e/5a190/new_environment.png "Creating a new environment")<figcaption>Creating a new environment</figcaption>

Now, let’s add some environment variables specific to that environment

- baseUrl : The base URL for all the endpoints
- email: The email used to login
- password: The password used to login

![Adding environment variables](https://www.ankursheel.com/static/8bd983c89b546c828f2b23b0184d5e87/5a190/environment_variables.png "Adding environment variables")<figcaption>Adding environment variables</figcaption>

Let’s make insomnia use the environment.

![Use the environment](https://www.ankursheel.com/static/465008dc2b1ea74191ce86dcb774dee8/5a190/use_environment.png "Use the environment")<figcaption>Use the environment</figcaption>

Assuming that the endpoint to get the access token is `LoginUser` add a new post request called `GetAccessToken`.

_Remember the environment variables from earlier. We can use them so that no matter what environment we are on, everything will just work._

![Get token](https://www.ankursheel.com/static/7c9f178cff005492cf7eb57d1feb5775/5a190/get_token.png "Get token")<figcaption>Get token</figcaption>

Let’s add a new Graphql query and try to send the request. We get a  **401 Unauthorized**  error because we haven’t passed in the bearer token.

![Graphql request without bearer token](https://www.ankursheel.com/static/4fb719a5d9950c06a1f228486e2a334c/5a190/unauthorised.png "Graphql request without bearer token")<figcaption>Graphql request without bearer token</figcaption>

To set the bearer token, we can click on the  **Bearer**  tab and enter _Response ⇒ Body Attribute_ for the token.

![Set the bearer token](https://www.ankursheel.com/static/4c33a7d2d3a70e3689c7506c8bd828a8/5a190/set_token.png "Set the bearer token")<figcaption>Set the bearer token</figcaption>

To edit the tag, click on it.

![Edit the tag](https://www.ankursheel.com/static/943cb219ad45c371e8c4d1bca8d8e273/5a190/edit_tag.png "Edit the tag")<figcaption>Edit the tag</figcaption>

In the  **Edit Tag**  screen select _Post GetAccessToken_ for the request

![Set the request on the tag screen](https://www.ankursheel.com/static/9852454d75cbe2e053a2af3c2180611a/5a190/set_tag_request.png "Set the request on the tag screen")<figcaption>Set the request on the tag screen</figcaption>

Set the trigger behaviour to  **Always**  so that the request is made every time and you get a new token

![Set the trigger behavior](https://www.ankursheel.com/static/e100eedd94055535163962e05a4b92a6/5a190/trigger.png "Set the trigger behavior")<figcaption>Set the trigger behavior</figcaption>

For the filter, enter `$.accessToken.value`. The value will be dependent on the response that you get from _GetAccessToken_. If you have set it up correctly, you should see a token in the **Live Preview**.

![Set the filter](https://www.ankursheel.com/static/4267c5df96ed5261709222aa4be8310f/5a190/filter.png "Set the filter")<figcaption>Set the filter</figcaption>

Now, if you try to resend the request, you should get back a  **200 OK**  response.

![Send a successful request](https://www.ankursheel.com/static/609cf4e8ef1447581bb0e46b8aa1cdd9/5a190/request_success.png "Send a successful request")<figcaption>Send a successful request</figcaption>

## Conclusion

By automatically generating access tokens every time we make a Graphql request, we can easily test our API using Insomnia