Showcasing Postman environment settings and pre-request script examples, highlighting this week's learning focus.

Welcome to this week's TWIL session, where our resident expert Katie shares insights on Postman Environments, Variables, & Faker as well as the utility of Postman Pre-Request Scripts. Grasp the power of Postman's environment management and dynamic data generation to enhance API testing efficiency. Katie's contributions this week provide a practical walkthrough on configuring Postman to seamlessly switch API contexts and exploit its dynamic variable feature to simulate real-world data. Furthermore, learn how to leverage pre-request scripts to automate setup and testing processes, ensuring your requests are executed in the exact environment state required. Join us on a journey of micro-learning with Postman's tools that promise to refine your software development practices.

Postman Environments, Variables, & Faker

Our collections often rely on Postman's environments and environment variables (e.g., using base_url in request URLs so that requests can easily switch between APIs), but there is a lot that can be done with Postman's environments, variables, and dynamic variables (powered by Faker).

To access an environment variable in a pre-request script or test:

pm.environment.set("variable_name", "I'm set in the environment now!");
pm.environment.get("variable_name");

This can be used to set and use local variables as well (including, for example, between a pre-request script and test):

pm.variable.set("local_variable_name", "I'm specific to the request I'm in.");
pm.variable.get("local_variable_name");

Postman has dynamic variables that are evaluated in the request:

// request body
{
	"name": "{{$randomName}}",
	"email": "{{$randomEmail}}",
	"favoriteColor": "{{$randomColor}}"
}
// pre-request script / test
pm.variables.set("notes", "{{$randomLoremParagraph}}")

If you need to access the value of an evaluated dynamic variable (since otherwise each instance will be different), you can use replaceIn:

let firstName = pm.variables.replaceIn("{{$randomFirstName}}");
let lastName = pm.variables.replaceIn("{{$randomLastName}}");
let fullName = `${firstName} ${lastName}`;
// "{{$randomFirstName}} {{$randomLastName}}" would result in a full name with a 
// different first and last name than set for firstName and lastName

  • Tools
  • Postman
Katie Linero's profile picture
Katie Linero

Senior Software Engineer


Postman Pre-Request Scripts

Postman's pre-request scripts can be helpful for a variety of setup, running, and testing uses. For example, ensuring that a given user is logged in (or out) before a request is sent, creating a record specifically for a deletion request, or setting a variable for use in a request based on some API response, all without depending on any other Postman requests.

Logging In

+ Storing Tokens

pm.sendRequest({
    url: `${pm.environment.get("base_url")}/login`,
    method: "POST",
    body: {
        mode: 'raw',
        raw: JSON.stringify(
            {
	            "email": pm.environment.get("user_email"),
	            "password": pm.environment.get("password")
            }
        )
    }
}, function (err, res) {
    let response = JSON.parse(res.json());
    pm.environment.set("access_token", response.tokens.access_token);
    pm.environment.set("refresh_token", response.tokens.refresh_token);
});

Clearing Stored Tokens

pm.environment.set("access_token", "");
pm.environment.set("refresh_token", "");

Creating for Deletion

+ Setting Local Variable for Request URL

// If the request returns the deleted object, saving the creation values in local
// variables can be utilized for testing the response; it may not be useful otherwise
let thingName = "{{$randomNoun}}"
pm.variables.set("thing_name", thingName);

// Create thing for deletion
pm.sendRequest({
    url: `${pm.environment.get("base_url")}/api/things`,
    method: "POST",
    header: {
        "Authorization": `Bearer ${pm.environment.get("access_token")}`
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({ "name": thingName })
    }
}, function (err, res) {
		// Set local variable `delete_id` for use in request URL
    let response = JSON.parse(res.json());
    let thingId = response.data.id;
    pm.variables.set("delete_id", thingId);
});

Then, our delete request URL can be something like this, without needing to maintain delete_id in the environment or having worry about which records are being/have already been deleted:

{{base_url}}/api/things/{{delete_id}}

  • Tools
  • Postman
Katie Linero's profile picture
Katie Linero

Senior Software Engineer

Related Posts

Illustration of a small, determined knight in weathered medieval armor and a bucket helmet, wearing a tattered red cape, striding across barren cracked earth with sword drawn, surrounded by a swirling cloud of scattered wooden alphabet letters representing Token Guard, a GitHub Action that monitors and guards against token context bloat in AI coding agent workflows by counting the tokens in LLM instruction files committed to repositories."
February 10, 2026 • Frank Valcarcel

Token Guard: Keeping Your Agent Context Lean in CI

Token Guard is a GitHub Action that counts tokens in your agent context files and enforces limits in CI. Here’s why we check agent context into our repos, and why keeping it lean matters for team collaboration.

AWS logo centered over dark blue stylized map of Europe with concentric radar-style rings emanating from Germany, representing the AWS European Sovereign Cloud infrastructure launch for EU data sovereignty and GDPR compliance
January 26, 2026 • Frank Valcarcel

AWS Launches European Sovereign Cloud

AWS launched a physically separate cloud infrastructure in Europe with EU-only governance, zero US dependencies, and over 90 services. Here is what organizations in healthcare, finance, and government need to know about the sovereign cloud and how to evaluate it for their compliance strategy.

Let's work together

Tell us about your project and how Cuttlesoft can help. Schedule a consultation with one of our experts today.

Contact Us