Learn from experts and connect with global tech leaders at POST/CON 24. Register by March 26 to save 30%.

Learn more →
X

10 Tips for Working with Postman Variables

Avatar

This is a follow-up to our popular post about introducing variables in Postman.


Variables are a fundamental concept in programming, and in Postman, they can be your best friend. Instead of painstakingly updating individual values, use variables to cascade changes effortlessly through the rest of your code. If you’re not already using variables in Postman, get ready to have your mind blown.

screen

With Postman variables, you can:

  • Reuse values so you can keep your code DRY (Don’t Repeat Yourself)
  • Configure your setup to accommodate different users or environments
  • Extract data from responses and chain together requests in a collection
  • And more!

10 tips for working with Postman variables

      1. Use variables in the request builder: Use variables in the request builder anywhere text is used, such as the URL, URL parameters, headers, authorization, request body, and header presets. Postman uses string substitution to replace variable names enclosed in double curly braces – like {{variableName}} with its corresponding value as a global, collection, or environment variable.
        For example, you can store access credentials or endpoint paths as variables. In this way, you can easily configure your setup to accommodate different users or server environments.
        gif
      2. Dynamic variables: Generate and use dynamic variables in the request builder as unique IDs, timestamps, or random integers. Use dynamic variables with the double curly braces syntax—like {{$timestamp}}—in the request URL / headers / body.screen
      3. Autocomplete for variables: Type an open curly brace in the request builder (or type the first letter of the variable in the script sections) to bring up the autocomplete menu.screen
      4. Variable highlighting and tooltips on hover: Resolved variables are highlighted in orange, while unresolved variables are red. Hovering over the variable displays a tooltip with the variable’s current value and scope.screen
      5. Using variables in scripts: Variables are also available in the pre-request and test script sections. Since these sections are written in JavaScript, you will initialize, retrieve, and define the scope of these variables in a different manner. This is a really powerful way to extract data from a response and use this data in a subsequent request.screen
      6. Logging variables: Log variables using console.log() in a script to inspect variables in the Postman console. This is especially helpful when troubleshooting, or to communicate explicitly to your fellow collaborators (or future self) during development and debugging.screen
      7. Quick Look: Clicking on the Quick Look icon provides a preview displaying all your environment and global variables in one place. This is a good place to view a variable’s state once you send a request.screen
      8. Variable editor: From the Quick Look preview, clicking on one of the Edit links opens a modal to edit environment or global variables. Collection variables can be manually updated by editing the collection details.screen
      9. Data variables: Data variables are available when using a data file with the collection runner. You can import  the data as a CSV or JSON file, and then use the values from the data file inside HTTP requests and scripts. With data variables, you can test multiple instances of a request in one go using something like {{username} in the request builder or pm.iterationData.get("username") in the script sections.screen
      10. Variable scopes: Similar to working with variables in other programming languages, Postman resolves scopes according to a variable scope hierarchy. Using variables within specific scopes allows you to reuse values efficiently.
        Global variables are commonly used to capture ephemeral states. Collection variables are best for values that are meant to be reused within the current collection. Environment variables are frequently used across multiple server environments – like development, staging, and production. For example, an endpoint using the variable {{url}} could easily switch between a development  value of dev.myapp.com or a production value of myapp.com.

        screen

What do you think about this topic? Tell us in a comment below.

Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

22 thoughts on “10 Tips for Working with Postman Variables

  • Avatar

    How can I use variables in the Request BODY (e.g. raw JSON ?)

    • Avatar

      You can use double curly brackets also in body

      • Avatar

        That doesn’t work

        • Avatar

          It works if you include in a “” string,
          e.g. { “user_token”:”{{TOKEN}}”}

  • Avatar

    @realB12:disqus I did it like this:
    {“commentText”:”note with random number {{$randomInt}} from the postman collection posted at {{$timestamp}}”}

  • Avatar

    Can i use variable in another variable? For example:

    first: “hello”

    second: {{first}} world

    in test:
    console.log(pm.variables.get(“second”)) // Show: Hello world

  • Avatar

    My Json file in Collection runner as input data :
    [{
    “rename”: {“AssetId”: 5496,”NewName”: “API-CK9-2st”},
    “addfolderto”: {“FolderId”: 5456,”FolderName”: “API-CK1”}
    }]
    And in My Pre-Request Script:

    var envRename = pm.iterationData.get(“rename”);
    pm.environment.set(“rename”, {{envRename }});

    And in My Body, I’m trying to use directly the env. variable to replace body like :
    {{rename}}

    Then after running the script, my request body is replaced as:
    Request Body:”[object Object]”
    instead of my input json .

    Please help me to resolve this, for Huge data request, I want to have my input in the form of external JSON file and indeed to re-place the entire body per iteration.

    • Avatar

      I too see the same issue.

      My Pre-Request Script:

      var dataToPostObj = {“Obj1”: “Value”};
      pm.environment.set(“dataToPost”, dataToPostObj);

      My Body –
      {
      “Data”: {{dataToPost.Obj1}}
      }

      Here, “Request Body” comes as
      Request Body:”{ “Data”: {{dataToPost.Obj1}} }”

      Can anyone advise how i can resolve object property in the body. i.e. {{dataToPost.Obj1}} – as this doesn’t seem working or supported.

  • Avatar

    If I set an environment variable in the Prerequest or Test script, it is not properly resolved in the example response. Only variables set in pre-requests, the requests performed before, can be resolved in the example responses.
    Here is an example response using variables:
    {
    “myParam”: {{myVariable}}
    }

  • Avatar

    I wondering if there is a way in Postman to copy the value of a request URl, with all the variables evaluated when it is copied?

  • Avatar

    Sometimes we get a response as Status:401 UNAUTHORISED . I want to put this in a variable and compare with Status:200 OK. Can someone please help me in this.

    • Avatar

      Hi Sumit, Please contact our support team here, and they’ll be glad to help you!

  • Avatar

    I Need use an Array in this request(GET). But that when my Array not have data the system in the response return all the data

    https://api8.successfactors.com:443/odata/v2/Position?$format=json&$select=code,parentPosition&$filter=code in {{MyArray}}

    For example:
    I have created a Variable called “MyArray” with the following value ‘00001234’, ‘00005678’ and when I Run the request the system return the data. However when MyArray have this value ‘ ‘. The response return

    {
    “d”: {
    “results”: []
    }
    }

    Regards

    • Avatar

      Hi, Please contact our support team here, and they’ll be glad to help you!

  • Avatar

    I feel one very important upgrade needed in postman is to pass the datafile from the configured path instead of passing the file each time manually by click on “Select file”. Please bring it asap.. I know the Newman integration .. but this feature required in Postman, this will increase the value multifold..

  • Avatar

    Hello,
    I have several requests for Freshservice added to my collection and some looks like this
    api/v2/releases/[taskid]/tasks/[id], I can keep editing the whole string but is there a way to work easier with variablers from the same tab smular like keys? I can use collection variables but then I need to go to that tab to edit for each time I like to change the request and sometimes I want have same request for different values or another that would share the same collection variable.

  • Avatar

    When I think of global variables, I think of global to the application not just the workspace. I want to be able to set a variable that will work across all workspaces (application variable), or even for each workspace (user variable) that doesn’t get saved to the team workspace. This will allow for sending the correct email address or username by each member of the team when developing APIs.

  • Avatar

    Hi Joyce!I would be interested in knowing how I can create an environment variable
    , in which its value is autogenerated in the database. What do I put in the value of the environment variable?

  • Avatar

    I have added an API through an OpenAPI definition. The OpenAPI has parameters, which Postman converts to Path variables.
    I want to update/edit all Path variables with the same key to use a collection/environment variable.

    Is it possible to do this with a single action, or do I have to manually set all the Path variables to use the Collection/Environment variables?