Introduction to Pre-request Scripts
What Is a Pre-request Script?
A pre-request script is a snippet of JavaScript that runs immediately before a request is sent. It's commonly used to set variables, generate timestamps, or compute values needed by the request.
Where to Add One
Every request, folder, and collection has a Pre-request Script tab. Scripts at the collection or folder level run for every request inside them, which is useful for shared setup logic.
Common Use Cases
| Use Case | Example |
|---|---|
| Dynamic timestamps | Set a variable to the current date/time |
| Generating tokens | Compute a signature before sending |
| Randomized test data | Generate a random email or ID |
Pre-request scripts rely on the built-in pm object, which is covered in more detail next.
To write meaningful pre-request scripts and tests, you'll need to understand the pm object and how JavaScript works inside Postman.
Ready to master Postman API Testing Course?
Join Uncodemy's hands-on training and build real-world API testing skills with expert mentors.