***

title: Use scripts to add logic and tests to Postman requests
slug: docs/tests-and-scripts/write-scripts/intro-to-scripts
max-toc-depth: 2
ux: need to replace logs-in-console-v11-v3.jpg
topictype: concept
---------------------

For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://learning.postman.com/llms.txt. For full content including API reference and SDK examples, see https://learning.postman.com/llms-full.txt.

Postman's runtime is based on [Node.js](https://nodejs.org/en/about). 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](/docs/tests-and-scripts/write-scripts/pre-request-scripts/) under the **Scripts > Pre-request** tab.
* After a response is received, as a [post-response script](/docs/tests-and-scripts/write-scripts/test-scripts/) 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](https://assets.postman.com/postman-docs/v12/test-script-autocomplete.png)

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](https://assets.postman.com/postman-docs/v11/req-resp-v11.jpg)

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](https://assets.postman.com/postman-docs/v11/execOrder-v11.jpg)

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.

<img alt="Collection with nested folder and requests" src="https://assets.postman.com/postman-docs/v11/console-log-statement-v11-2-v4.jpg" />

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](/docs/sending-requests/response-data/troubleshooting-api-requests/).

![Logs in the Console](https://assets.postman.com/postman-docs/v11/logs-in-console-v11-v3.jpg)

### How it works

The [Postman Sandbox](/docs/tests-and-scripts/write-scripts/postman-sandbox-reference/overview/) 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](/docs/postman-cli/postman-cli-overview/).

<Info class="iconless-callout">
  <img alt="Collections icon" src="https://assets.postman.com/postman-docs/Collections.png#icon" width="24px" /> 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](https://www.postman.com/templates/collections/api-scenario-testing/).
</Info>

## 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](/docs/tests-and-scripts/write-scripts/packages/package-library/).

You can also [import external packages](/docs/tests-and-scripts/write-scripts/packages/external-package-registries/) 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](/docs/sending-requests/response-data/troubleshooting-api-requests/).

## Test examples

You can use post-response [script examples](/docs/tests-and-scripts/write-scripts/test-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](https://www.npmjs.com/package/@faker-js/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](/docs/tests-and-scripts/write-scripts/variables-list/).

## Postman JavaScript APIs

Postman provides [JavaScript APIs](/docs/tests-and-scripts/write-scripts/postman-sandbox-reference/overview/) that you can use in your request scripts. The `pm` object provides functionality for testing your request and response data.