For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Postman
PricingEnterprise
Contact SalesSign InSign Up for Free
HomeDocs
HomeDocs
      • Overview
        • Overview
        • Write pre-request scripts
        • Write tests
        • Script examples
        • Dynamic variables
        • Troubleshoot test errors
Postman API Platform

Product

  • Postman Overview
  • Enterprise
  • Spec Hub
  • Flows
  • Agent Mode
  • API Catalog
  • Fern
  • Postman CLI
  • Integrations
  • Workspaces
  • Plans and pricing

API Network

  • App Security
  • Artificial Intelligence
  • Communication
  • Data Analytics
  • Database
  • Developer Productivity
  • DevOps
  • Ecommerce
  • eSignature
  • Financial Services
  • Payments
  • Travel

Resources

  • Postman Docs
  • Academy
  • Community
  • Templates
  • Intergalactic
  • Videos
  • MCP Servers

Legal and Security

  • Legal Terms Hub
  • Terms of Service
  • Postman Product Terms
  • Security
  • Website Terms of Use

Company

  • About
  • Careers and culture
  • Contact us
  • Partner program
  • Customer stories
  • Student programs
  • Press and media
Twitter iconLinkedIn iconGithub iconYouTube iconInstagram iconDiscord icon
Download Postman
Privacy Policy

© 2026 Postman, Inc.

On this page
  • Pre-request scripting example
  • Scripting before your request runs
  • Add documentation to pre-request scripts
  • Reuse pre-request scripts
  • Next steps
Tests and scriptsWrite scripts

Write pre-request scripts to add dynamic behavior in Postman

||View as Markdown|
Was this page helpful?
Previous

Use scripts to add logic and tests to Postman requests

Next

Write scripts to test API response data in Postman

Built with

You can use pre-request scripts in Postman to run JavaScript before a request runs. By including code in the Pre-request tab for a request, collection, or folder, you can carry out pre-processing such as setting variable values, parameters, headers, and body data. You can also use pre-request scripts to debug code, for example, by logging output to the Postman Console.

Pre-request scripting example

The following is an example of using pre-request scripts:

  • You have a series of requests in a collection and are running them in a sequence, such as when using the Collection Runner.
  • The second request is dependent on a value returned from the first request.
  • The value needs to be processed before you pass it to the second request.
  • The first request sets the data value from a response field to a variable in its post-response script.
  • The second request retrieves the value and processes it in its pre-request script, then sets the processed value to a variable. This variable is then referenced in the second request, for example, in its parameters.

Scripting before your request runs

To include code you want to run before Postman sends a request, do the following:

  1. In the sidebar, click Items icon Items, then expand Collections.
  2. Open the request, then select the Scripts tab.
  3. Click the Pre-request tab.
  4. Enter the JavaScript you need to process before the request runs, then click Save icon Save.
  5. Click Send to send the request. The code runs before Postman sends the request to the API.

To make your code more readable, click Pretty icon Beautify in the lower right of the code editor.

Add documentation to pre-request scripts

Postman supports JSDoc for documenting JavaScript functions in your pre-request scripts. Documentation added to your functions using JSDoc display in a popup window when you call your functions. You can use the official JSDoc documentation to learn how to add documentation to your pre-request scripts.

The following example has documentation for the logger function using JSDoc. The documentation explains what the function does, and defines what the data parameter is used for and that it accepts a string data type.

1/**
2 * This function prints a string to the Postman Console.
3 * @param {string} data - The text to print to the Postman Console.
4 */
5function logger (data) {
6 console.log(`Logging information to the console, ${data}`)
7}

Reuse pre-request scripts

You can add pre-request scripts to entire collections and folders within collections. In both cases, your pre-request script will run before every request in the collection or direct child request in the folder. This enables you to define commonly used pre-processing or debugging steps you need to run for multiple requests.

You can define a pre-request script when you first create a collection or folder, or at any time after that. You can also store pre-request scripts in the Postman Package Library. This lets you maintain commonly used scripts in a single location, share them with your team, and reuse them in your internal workspaces.

To add pre-request scripts to a collection or folder, do the following:

  1. In the sidebar, click Items icon Items, then expand Collections.
  2. Select a collection or folder.
  3. Click the Scripts tab.
  4. Click the Pre-request tab. Enter code that will run before every request in the collection or direct child request in the folder.

Next steps

After learning the basics of writing pre-request scripts, you can extend your scripts:

  • To learn more about how to use the pm object, visit the Postman JavaScript reference.
  • For more information about storing and reusing commonly used scripts, learn about the package library in Postman.