# Scripts and tests

> Pre-request and test scripts on saved requests, Postman's pm API: where they run, what they can do, and where the values they set go.

A saved request can run JavaScript before it's sent and after the response comes back, written the way Postman's are (`pm.environment.set`, `pm.test`, `pm.expect`). Scripts from an imported Postman collection come in with their requests and run as they did there.

## Where to write them

Open a request and choose **Scripts**. Pick the level, then **Pre-request** or **Tests**:

- **Collection:** runs for every request.
- **A folder:** runs for every request in it.
- **This request:** runs for this one only.

They're saved as you type. A dot on the tab means some level has a script.

## When they run

Scripts run when you send from the **Dockito desktop app**, in the [runner](/help/api-docs/runner), and with `dockito run` in a terminal. In the browser they're shown and editable, and requests aren't sent from there.

The order is Postman's: pre-request scripts from the collection down to the request, then the send, then the test scripts in the same order. If a pre-request script fails, the request isn't sent and you see which script and which line.

## Results

After a send, the response has two more tabs:

- **Tests:** each test passed or failed, with why it failed in words.
- **Console:** what the scripts wrote with `console.log`, and each variable they set.

**Copy for AI** includes the tests and the variables set, so your AI sees what passed.

## Values a script sets

A value set with `pm.environment.set` or `pm.collectionVariables.set` goes to your own value for that variable on this device, like Postman's current value. It isn't a change to the doc, so your team doesn't see it. A value that looks like a key, token or password goes to your computer's keychain and is shown only as `••••`.

`pm.globals` lasts until the app closes.

## What scripts can do

The parts of Postman's `pm` that collections use: `pm.environment`, `pm.collectionVariables`, `pm.variables` and `pm.globals`; `pm.request` (change headers, URL, query or body before the send); `pm.response` (`code`, `status`, `headers`, `json()`, `text()`, `responseTime`); `pm.test` and `pm.expect`; `pm.sendRequest`; `pm.info`; and the older `postman.setEnvironmentVariable` and `tests["…"]` forms.

Each script runs on its own, cut off from your computer: up to 5 seconds and 32 MB, no files, and no network except `pm.sendRequest` (up to 20 calls).

## Your AI

Your AI can read every level's scripts for a request and write one, through [MCP](/help/api-docs/for-ai).
