So far, your tests run AFTER the response. But what about code that runs BEFORE the request? That's pre-request scripts. They let you set variables, generate dynamic data, compute timestamps — anything you need before the request fires.
// Generate a unique email using timestamp
const timestamp = Date.now();
const uniqueEmail = `testuser_${timestamp}@mailinator.com`;
pm.environment.set("userEmail", uniqueEmail);
// Now use {{userEmail}} in your request body:
// {
// "email": "{{userEmail}}",
// "password": "Test@1234"
// }// Random number between 1 and 1000
const randomId = Math.floor(Math.random() * 1000) + 1;
pm.environment.set("randomUserId", randomId);
// Current date in ISO format
const today = new Date().toISOString().split("T")[0];
pm.environment.set("currentDate", today);
// Random string for unique names
const chars = "abcdefghijklmnopqrstuvwxyz";
let randomName = "";
for (let i = 0; i < 8; i++) {
randomName += chars.charAt(Math.floor(Math.random() * chars.length));
}
pm.environment.set("randomName", randomName);
// UUID-like unique identifier
const uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c === "x" ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
pm.environment.set("requestId", uuid);Postman has built-in dynamic variables you can use directly in request fields without any script: {{$timestamp}}, {{$randomInt}}, {{$guid}}, {{$randomEmail}}, {{$randomFirstName}}. But pre-request scripts give you full control over the format.
| Approach | Pros | Cons |
|---|---|---|
| {{$randomEmail}} | Zero code, instant | Can't control format or domain |
| Pre-request script | Full control, any format | Need to write JavaScript |
| {{$timestamp}} | Quick, unique enough | Just a number, not human-readable |
| Date.now() in script | Can format any way you want | Slightly more work |
// Pre-request: Check if token exists and is not expired
const token = pm.environment.get("authToken");
const tokenExpiry = pm.environment.get("tokenExpiry");
const now = Date.now();
if (!token || now > tokenExpiry) {
console.log("Token missing or expired. Login request will refresh it.");
// You'd typically call a login endpoint here using pm.sendRequest
// (covered in the next lesson)
}Pre-request scripts can set environment and collection variables, but NOT global variables in the free version of Postman. Use pm.environment.set() for most cases. Also, pre-request scripts cannot access pm.response — the response doesn't exist yet.
Q: What are pre-request scripts in Postman? Give an example.
A: Pre-request scripts are JavaScript code that runs before a request is sent. I use them to generate dynamic test data — like unique emails using timestamps, random IDs, or formatted dates. For example, I generate a unique email with Date.now() and set it as an environment variable using pm.environment.set("email", "user_" + Date.now() + "@test.com"). The request body then uses {{email}} to inject the value. This ensures every test run creates unique data and avoids conflicts.
Key Point: Pre-request scripts run BEFORE the request. Use them to generate dynamic data, set variables, and prepare test inputs. Post-response scripts run AFTER — that's where your assertions go.