Use scripts to add logic and tests to Postman requests

Postman's runtime is based on Node.js and enables you to 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, pass data between requests, and more.

Pre-request and post-response scripts execute asynchronously. That means you can execute multiple scripts without waiting for the previous script to complete. If you want scripts to execute in sequence, you can use a callback function.

Scripts in Postman

You can add JavaScript code to execute during two events in the 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.

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

Script autocomplete

You can add pre-request and post-response scripts to a collection, a folder, a request within a collection, or a request not saved to a collection.

Execution order of scripts

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

  • A pre-request script associated with a request will execute before the request is sent
  • A post-response script associated with a request will execute after the request is sent
Workflow for single request

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

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

For every request in a collection, the scripts will always run according to the same hierarchy. Collection-level scripts (if any) will run first, then folder-level scripts (if any), and then request-level scripts (if any). Note that this order of execution applies to both pre-request and post-response scripts.

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

Console log statement

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

Logs in the Console

How it works

The Postman Sandbox is a JavaScript execution environment that's available to you while writing pre-request and post-response scripts for requests (both in Postman and Newman). Whatever code you write in these sections is executed in this sandbox.

Collections icon You can build conditional workflows using a collection, and branch and loop over a set of requests. To try out this collection template, select Conditional workflow.

Reuse scripts

You can add commonly-used scripts and tests to packages in your team's Package Library. This enables you to reuse scripts in the Pre-request and Post-response tabs in your team's HTTP requests, collections, and folders. Learn how to reuse scripts and tests in Postman.

Debugging scripts

Debugging scripts can be written under either the Pre-request tab or the Post-response tab, with helpful messages logged 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 execute when Postman receives a response from the API you sent the request to. When you add tests to a folder or collection, they execute after each request inside it.

Dynamic variables

Postman uses the Faker library to generate sample data, including random names, addresses, email addresses, and much more. You can use these predefined variables multiple times 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.

Last modified: 2021/01/27