Variables in POSTMAN

Variables are used to store a part of the URL in POSTMAN. Using this concept of storing a part of the URL in a variable and using it in multiple places can save the time of typing a lengthy URL. This technique can also be used to identify a URL very easily in different parts of the POSTMAN software.

In this section, we shall create a variable, use a variable, and refer a variable. Variables are used when a part of different URLs is a common text.

For example, two requests which are inside the collection- Collection01 have the first website link in common; just the request end-points are different. Therefore, the first part can be stored in a variable so that, the variable can be used when the website URL is required.

To open the variable section and click the three dots beside the collection name, and then a drop-down list will appear. Select edit from the list.

postman-edit-variables

After you select edit, you should see as below:

postman-variables

Now, click on the variable tab and you will be asked to enter the key, which is the variable name(anything you want). Now, copy the first part of the URL which is common to both of the requests.

In our case, one URL is: https://reqres.in/api/users/2

And another URL is: https://reqres.in/api/users?/page=2

Therefore, you can see that the URL part https://reqres.in is common to both the above URLs. Therefore, we shall try to store this part of the URL in a variable, and try substituting it inside the POSTMAN software and see whether it works.

To do so, click on the Variables and then name the key(the variable) and then edit the part of the URL into the URL bar. And then select the Update button.

postman-variable-edit

After you have named and updated your variable, go to the URL bar and replace the part of the URL with the variable you declared.

In our case, the variable is the url and so the syntax contains URL between a pair of braces {{ }}

postman-variable-replacement

After successful replacement of the variable, you are ready to go. Now, send the request again and it should work the same way it worked earlier.

postman-send-button-check

Check the output now:

postman-variable-replacement-working

Variables in Environment

Variables can be set in POSTMAN Environment directly. It means when we are required to change variables we can directly change the environment. This is useful when we work with different request URLs which has a common part in it.

Variables in POSTMAN Environment can be created in two different ways

  • Adding a variable just by clicking into Add button, which will work for the environment inside which it is created.
  • Global Variable, which can be created irrespective of environments. This type of variable can be used and applied in different environments.
Environment Variable:

To create Environment Variable, go to Manage Environment.

postman-manage-environment

Then click into the Add button.

postman-variable-add-button

After you click Add you will get an area to input the variable name, value, etc.

postman-variable-adding

In my case my VARIABLE is url01. The INITIAL VALUE is http://reqres.in/ And the CURRENT VALUE will automatically take the same as that of the INITIAL VALUE.

Now, before adding the variable and values, enter a suitable environment name and then click Add.

postman-environment-naming

Now, when you Add your variable, it is added successfully. Now, go to the request URL bar and replace the part of the URL with the variable name. In my case, the URL part I want to replace is: http://reqres.in/. The variable which must be placed inside a double brace. For example, {{url01}} in the URL.

postman-variables-replacement

And then select the Environment as new-environment. I have an API request called - new01. Now, go click the Send button.

postman-send-button-with-environment

Now, the request should work well and the response should be visible below in the software.

Response from the server :

{
    "page": 1,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [
        {
            "id": 1,
            "email": "george.bluth@reqres.in",
            "first_name": "George",
            "last_name": "Bluth",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/calebogden/128.jpg"
        },
        {
            "id": 2,
            "email": "janet.weaver@reqres.in",
            "first_name": "Janet",
            "last_name": "Weaver",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg"
        },
        {
            "id": 3,
            "email": "emma.wong@reqres.in",
            "first_name": "Emma",
            "last_name": "Wong",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg"
        },
        {
            "id": 4,
            "email": "eve.holt@reqres.in",
            "first_name": "Eve",
            "last_name": "Holt",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
        },
        {
            "id": 5,
            "email": "charles.morris@reqres.in",
            "first_name": "Charles",
            "last_name": "Morris",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
        },
        {
            "id": 6,
            "email": "tracey.ramos@reqres.in",
            "first_name": "Tracey",
            "last_name": "Ramos",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
        }
    ]
}

So, we have seen that how we can set variables in the environment and still the URL runs a similar way.

Appropriate environment(in which the variables are set) should be selected before targetting to send the request. An irrelevant environment will produce an error.

Global Variables

In the previous section we have seen how can we set an environment variable and when the request has to be run, we select the required environment and then send the request to get the response.

However, when variables are set in the environment level, then the variable can only be used when the request is within the environment. But it is also possible to set variables globally, where the variable can be used in any of the environments.

Global Variables are used when we create different environments with different requests inside them. But all the request contains a part of the URL which is common to all.

To create Global variables, go to the Environment Manager and click Globals.

postman-global-variable-option

Then you will get an area to create global variables and set value for them.

I shall create a variable for the URL part- https://reqres.in and will name the variable as cvalue(which I indicate common value, as this URL part is going to be used in different requests, you can use any name you want).

postman-global-variable-creation

Now, save it.

postman-save-global-variable

Now, create any request which requires the URL part- https://reqres.in And then append the required end-point and send the request.

For example, I shall visit the link: https://reqres.in and then select a GET request. I shall Select the List User, which is GET type and copy the request end-point.

postman-get-method-example

Now, go to the URL bar of the request and use the variable cvalue in place of https://reqres.in and then append the request end-point.

postman-set-global-variable

Now, select any environment of your choice(does not matter which one you choose), the request will work for all the environment. This is the use of a Global Variable.

Let us select any environment and send the request. First, I shall select the environment myenv.

postman-select-any-environment-one

Then send the request.

Secondly, I select a different environment called: new-environment.

postman-select-any-environment-two

Now again send the request.

In both cases, I have got the same response. It means, that the global variable is in action and is working the same for different environments.

Response :

{
    "page": 2,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [
        {
            "id": 7,
            "email": "michael.lawson@reqres.in",
            "first_name": "Michael",
            "last_name": "Lawson",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/follettkyle/128.jpg"
        },
        {
            "id": 8,
            "email": "lindsay.ferguson@reqres.in",
            "first_name": "Lindsay",
            "last_name": "Ferguson",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/araa3185/128.jpg"
        },
        {
            "id": 9,
            "email": "tobias.funke@reqres.in",
            "first_name": "Tobias",
            "last_name": "Funke",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/vivekprvr/128.jpg"
        },
        {
            "id": 10,
            "email": "byron.fields@reqres.in",
            "first_name": "Byron",
            "last_name": "Fields",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/russoedu/128.jpg"
        },
        {
            "id": 11,
            "email": "george.edwards@reqres.in",
            "first_name": "George",
            "last_name": "Edwards",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/mrmoiree/128.jpg"
        },
        {
            "id": 12,
            "email": "rachel.howell@reqres.in",
            "first_name": "Rachel",
            "last_name": "Howell",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/hebertialmeida/128.jpg"
        }
    ]
}

Environment Variable Vs Global Variable

  • Environment Variable is used when a change in Environment requires a change in the entire URL. On the other hand, Global variable is used when all the requests in all the environments have a common part in their URLs.
  • An Environment variable can also help in changing the credentials, for example in the case of retrieving data when a particular attribute is matched. On the other hand, the Global variable cannot be used to change the credentials, they will act the same irrespective of change of environments.
  • The variable set in one Environment will not interfere with the other variables in the Environment. On the other hand, the change in the Global Variable, the change in the variable value will affect the entire Request URLs.
  • Environment variables can have the same value in different environments. Other the other hand, Global variables cannot have multiple values for one variable name.
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Subscribe to See Videos

Subscribe to my Youtube channel for new videos : Subscribe Now