← Back to Chapters

Pre-request Scripts in Postman

? Pre-request Scripts in Postman

? Quick Overview

Pre-request Scripts in Postman run before an API request is sent. They are written in JavaScript and are commonly used to generate dynamic data, set environment variables, create authentication tokens, and prepare request headers or parameters.

? Key Concepts

  • Executed before every request
  • Written using JavaScript
  • Can access environment, globals, and collection variables
  • Used for authentication, timestamps, random data
  • Helps automate API testing workflows

? Syntax / Theory

Postman provides a pm API object to interact with variables, requests, and utilities. Pre-request scripts are scoped at request, folder, or collection level.

  • pm.environment – access environment variables
  • pm.globals – access global variables
  • pm.variables – dynamic variable resolution
  • pm.request – modify request details

? Code Example(s)

? View Code Example
// Generate a random user ID before request
const userId = Math.floor(Math.random() * 10000);
pm.environment.set("user_id", userId);
? View Code Example
// Set current timestamp for request usage
const timestamp = new Date().toISOString();
pm.environment.set("current_time", timestamp);
? View Code Example
// Add dynamic authorization header
const token = pm.environment.get("auth_token");
pm.request.headers.add({
key: "Authorization",
value: "Bearer " + token
});

? Live Output / Explanation

What Happens?

  • A random user ID is generated before sending the request
  • Current timestamp is stored for logging or validation
  • Authorization header is attached dynamically

These values are available during the request execution.

? Interactive Example

Use the following logic to simulate dynamic data generation in every request:

? View Code Example
// Create a fake email for testing APIs
const email = "user" + Math.floor(Math.random() * 1000) + "@test.com";
pm.environment.set("email", email);

? Use Cases

  • Generating OAuth or JWT tokens
  • Creating random test users
  • Setting timestamps and signatures
  • Updating request headers dynamically
  • Chaining dependent API calls

? Interactive Simulator

Click "Run Script" below to simulate how Postman updates Environment Variables before sending a request.

Pre-request Script (Input)

// 1. Generate Random ID
let rID = Math.floor(Math.random() * 9999);

// 2. Get Timestamp
let ts = new Date().toLocaleTimeString();

// 3. Set Variables
pm.environment.set("order_id", rID);
pm.environment.set("req_time", ts);

Environment Variables (Result)

  • Variables will appear here...

✅ Tips & Best Practices

  • Keep scripts lightweight and readable
  • Reuse logic at collection level when possible
  • Use environment variables instead of hardcoding
  • Log values using console.log() for debugging

? Try It Yourself

  1. Create a random order ID before request
  2. Store today’s date in an environment variable
  3. Automatically attach a custom header
  4. Generate a dynamic username on every call