[Postman] The variables
Tags: postman, api, cheatsheet, javascript
Published: 2024-03-23 11:22:47

    From advanced to expert 
    For developers and testers.  
             ---------          
    Some great tutorials to get all the tips of Postman :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51cb5xogk1qcj76fl6so.png)

_**Number 2 : Let's continue the tutorials with the variables.**_
In the previous tutorial we written some javascript in the test section to get data from the console. Postman offer a storage feature for this data.

**_That means :_**
- we can use getter and setter inside postman
- we can reuse data for a request chain for e2e tests and automation
- We can secure bearer token as secret using the variables

For this tutorial we will use the free **_API of national french repository of adresses_** (--> [documentation - FR](https://guides.etalab.gouv.fr/apis-geo/1-api-adresse.html))

Create a new request and type this call :
```
https://api-adresse.data.gouv.fr/search/?q=8+rue+port
```
Feel free to enter the adresse of your choice ;) 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qleysgul4k9tva6cd491.png)

Click the send button to submit the request and enjoy the 5 results.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozmr00szz68qgt9i7yzd.png)

Alright, it's time to play with variables !

**_Firstly_** we will _iterate on the results_ to get each adresse name : 
```
let adresses = pm.response.json()
adresses.features.forEach(adresse => console.log(adresse.properties.label))
```
Submit the call and enjoy the results in the dev console :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ynl8p7fr4t2q2p9ri6i.png)

Each adresse label is properly retrieved.
**_Secondly_**, we will store the results in a **_collection variable._**

```
let adresses = pm.response.json()
// Setter
pm.collectionVariables.set("adresse_label_variable", adresses.features[0].properties.label);
```
Super nice ! And we add a getter :
```
// Getter
let firstAdresse = pm.collectionVariables.get("adresse_label_variable");
console.log('stored_variable', firstAdresse);
```

Should be like this :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rju9jagampehfic7mnpa.png)

Well done ! We used **_setter_** and **_getter_** to store the first result and reuse it in a console.log

**_Good to know :_** collectionVariables is not the only way to store data.

- CollectionVariable = Manage data into the collection only
- EnvironmentVariables = You can reuse data from one collection to another
- GlobalVariables = Use it everywhere.

> 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6y2wpvx61v1v9rb59okg.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8e74ypkraerrkabw838b.png)
([Official Postman Documentation](https://learning.postman.com/docs/sending-requests/variables/#defining-collection-variables))

You can find the environment on the right corner at the top :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68q5l64mvz37992kwyss.png)

**_To see all your collectionVariables_** go there :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4nwqjuaiwpqqi3r13x4.png)

To see the other variable types see here :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5h08f57bscrx8im3ne5o.png)

_In the next tutorial_ we will learn to use the 
**_environmentVariables_** and how to store secret data


See all the official **_postman documentation_** [here](https://learning.postman.com/docs/sending-requests/variables/)

---

Hope it helps ! 
**_Daphné Hervé (API, monitoring and QA automation lover)_**