How to use Postman tests to populate variables
Tags: postman, tests, api, webdev
Published: 2021-09-15 14:58:01

I've been using [Postman](https://www.postman.com/) for years, and I do love that product, but I'm aware that sometimes I just use a fraction of the features available on it, and today I found almost by accident a super handy one.

## The problem

I recently build some APIs, and I'm using Postman to test them, I've created a collection and created all the requests on it and grouped in folders.
I use Postman variables a lot to populate paths, data to send, etc...

Also my APIs use JWT token to authenticate my requests to it, and of course I used a variable on all the requests, as you can see here:

![Screenshot 2021-09-15 at 15.41.58](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4h9ut8pefiuvgw24mrbo.png)
 
And it works great. The only problem for me, is that the token expires after some time, I don't use JWT token with long life (and you shouldn't), so every time it expires I have to call the login endpoint, authenticate, get the token, copy it, and paste it in the variables.

And if there's something I hate, is repetitive work.

## The solution

So I started to google to see if there were any way to automate this, and I found my answer on the "[Scripting in Postman documentation](https://learning.postman.com/docs/writing-scripts/intro-to-scripts/)".

What I found is that you can write JavaScript code to write tests against your endpoints, which is by itself extremely useful, and something I'll start to do very soon, but I also found out that you can use the `pm` object available in the tests editor, to also **write variables programmatically!**

So I edited my login endpoint, selected the "Tests" option (see image), and started to write my script.
![Screenshot 2021-09-15 at 15.49.43](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/825u9jhm6ntn5emna6mi.png)

Here the script, which is just plain JavaScript:

```javascript
const { token } = pm.response.json();
// Write to collection variables
pm.collectionVariables.set('jwt_token', token);

pm.test('It should contain the token', () => {
    pm.expect(token).to.be.a('string');
});
```

So once you run the endpoint via postman, the tests are executed, then I read the response data, and get the `token` variable from it, then simply use that variable to populate my collection variables, and that's it!

Now once I run that endpoint, all other endpoints, as long I use that variable, they will work with the correct JWT token.

Just marvellous!

Also in my example I use the collection variables, but you can do the same for global, environment and local variables as well, here some examples:

```javascript
// Global
pm.globals.set("variable_key", "variable_value");

// Enviroment
pm.environment.set("variable_key", "variable_value");

// Collection
pm.collectionVariables.set("variable_key", "variable_value");

// Local variables
pm.variables.set("variable_key", "variable_value");
```

You can also get or unset those variables, for example:

```javascript
// get a variable value
const myVar = pm.variables.get("variable_key");

// unset a variable
pm.variables.unset("variable_key");
```

For more info you can check the [Postman related docs](https://learning.postman.com/docs/sending-requests/variables/#defining-variables-in-scripts)