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
  • Scripts in Postman
  • Run order of scripts
  • Reuse scripts
  • Debugging scripts
  • Test examples
  • Dynamic variables
  • Postman JavaScript APIs
Tests and scriptsWrite scripts

Use scripts to add logic and tests to Postman requests

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

Test APIs and write scripts in Postman

Next

Write pre-request scripts to add dynamic behavior in Postman

Built with

Postman’s runtime is based on Node.js. You can use this to write scripts that add dynamic behavior to requests and collections. You can use pre-request and post-response scripts to write API tests, build requests that can contain dynamic parameters, or pass data between requests.

Scripts run asynchronously. That means you can run multiple scripts without waiting for the previous script to complete. If you want scripts to run in sequence, you can use a callback function.

Scripts in Postman

You can add JavaScript code to run during two events in a flow:

  • Before a request is sent to the server, as a pre-request script under the Scripts > Pre-request tab.
  • After a response is received, as a post-response script under the Scripts > Post-response tab.
  • GraphQL requests run scripts before a query (under the Scripts > Before query tab) or after a response (under the Scripts > After response tab).
  • gRPC requests run scripts before invoke (under the Scripts > Before invoke tab), during a message (under the Scripts > On message tab) or after a response (under the Scripts > After response tab).

Postman prompts you with suggestions as you enter text. Select one to autocomplete your code.

Script autocomplete

In addition to requests, you can add pre-request and post-response scripts to a collection or folder.

Run order of scripts

In Postman, the script run order for a single request looks like this:

  • A pre-request script associated with a request runs before the request is sent.
  • A post-response script associated with a request runs after the request is sent.

Workflow for single request

For every request in a collection, scripts run in the following order:

  • A pre-request script associated with a collection runs prior to every request in the collection.
  • A pre-request script associated with a folder runs prior to every direct child request in the folder.
  • A post-response script associated with a collection runs after every request in the collection.
  • A post-response script associated with a folder runs after every direct child request in the folder.

workflow for request in collection

For every request in a collection, the scripts always runs according to the same hierarchy. Collection-level scripts run first, then folder-level scripts, and then request-level scripts. This run order applies to both pre-request and post-response scripts.

For example, the following collection is structured with a single folder and two requests within the folder.

Collection with nested folder and requests

If you created log statements in the pre-request and post-response script sections for the collection, folder, and requests, the run order is returned in the Postman Console.

Logs in the Console

How it works

The Postman Sandbox is available to you when you write your pre-request and post-response scripts. The sandbox is a JavaScript runtime environment for Postman and the Postman CLI.

Collections icon You can build workflows to test different API scenarios using a collection, and that branch and loop over a set of requests. To try out this collection template, select API scenario testing.

Reuse scripts

You can add commonly used scripts and tests to packages in your team’s Postman Package Library. This enables you to reuse internal scripts in your team’s HTTP gRPC, and GraphQL requests. Learn how to reuse scripts and tests in Postman.

You can also import external packages from npm and JSR package registries into your team’s HTTP, gRPC, and GraphQL requests.

Debugging scripts

Debugging scripts can be written under either the Pre-request tab or the Post-response tab. You can log debugging messages in the Postman Console.

Test examples

You can use post-response script examples to write your own tests for requests, folders, and collections. Post-response scripts run when Postman receives a response from the API you sent the request to. When you add tests to a folder or collection, they run after each request inside it.

Dynamic variables

Postman uses the Faker library to generate sample data, including random names, addresses, email addresses, and more. You can use these predefined variables to return different values per request. Learn how to use dynamic variables.

Postman JavaScript APIs

Postman provides JavaScript APIs that you can use in your request scripts. The pm object provides functionality for testing your request and response data.