Open In App

Difference between global and local variables in Postman

Last Updated : 26 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Postman is a powerful API development tool which offers a feature known as environment variables. These variables are used for efficient testing and development of APIs by allowing users to manage dynamic values across requests easily.

Difference between global and local variables in Postman

Variables enable you to store and reuse values in Postman. By storing a value as a variable, you can reference it throughout your collections, environments, requests, and test scripts. Variables help you work efficiently, collaborate with teammates, and set up dynamic workflows. Let’s see the difference between global and local variables in Postman

Global Variables

Local Variables

These are independent of environments and function outside the environment. Users are not required to create an environment for global variables. Through global variables, we can access data between collections, test scripts, requests, and environments.

These are temporary variables that will only function inside the environment they were created in. When you change the environment, the variable will stop its access, and an error will occur.

Global variables are accessible across different requests within a collection. They persist throughout the entire collection run.

Local variables are specific to a particular request. They exist only for the duration of that request and are not accessible outside of it.

You define global variables at the collection level or in the collection’s pre-request scripts. These variables are available to all requests in that collection.

Local variables are typically set in the pre-request script of a specific request or dynamically during the request execution.

Useful for storing data that needs to be shared across multiple requests, such as authentication tokens or common configuration settings.

Ideal for the temporary storage of data relevant only to a particular request, helping maintain request-specific context without affecting other parts of the collection.

Steps to create Global Variable

Step 1: After downloading and installing the Postman, open the software. Add a new Collection and give it a name like “GFG”.

postman2

Step 2: Select the environment quick look icon Environment quick look icon in the workbench as shown. Next to Globals , select Edit (or Add if no variables have been added yet) .

Step 3: Add a variable named geek and give it an initial value of GeeksforGeeks as shown below

z164

Step 5: When you hover, on the name of your collection, 3 dots will appear. Click on those 3 dots, and then click on “Add new request”

z198

Step 6: Now You can simply paste the API in the space provided and you can call the global variable by {{geek}} , then click on Send. Output will be shown in the body with the status code.

Example: In this example, we will create a global variable, id with a initial value=1. We will call API like:

https://jsonplaceholder.typicode.com/posts/?userId={{id}}

Recording-2023-12-07-at-003311Steps to create Local Variable

Step 1: After downloading and installing the Postman, open the software. Add a new Collection and give it a name like “GFG”.

postman2

Step 2: Create a environment ; “GFG“, using the + button in Environments tab as shown below

z176

Step 3: Create a local variable ; ‘var’ and add a initial value as 1

z170

Step 4: Go to your collection and select the environment ; “GFG”

z175

Step 5: Now You can simply paste the API in the space provided and you can call the local variable by {{var}} , then click on Send. Output will be shown in the body with the status code.

API Used

https://jsonplaceholder.typicode.com/posts/?userId={{var}}
the

Output:

Recording-2023-12-07-at-003311



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads