> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getpostchi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Scripts

> Automate request setup, response handling, and multi-step workflows with plain JavaScript.

Postchi lets you attach JavaScript scripts to your requests and run standalone scripts on demand. Scripts run in a sandboxed async function — you don't need `import` statements or TypeScript. Postchi injects the context you need directly.

There are three types of scripts:

<CardGroup cols={3}>
  <Card title="Before scripts" icon="arrow-right" href="/scripts/before-scripts">
    Run before a request is sent. Mutate the request's method, URL, headers, or body.
  </Card>

  <Card title="After scripts" icon="arrow-left" href="/scripts/after-scripts">
    Run after a response is received. Extract values into environment variables or secrets.
  </Card>

  <Card title="Actions" icon="bolt" href="/scripts/quick-actions">
    Standalone scripts you run on demand. Execute requests, set variables, and chain workflows.
  </Card>
</CardGroup>

## Script types

| Type                 | File name                          | When it runs                                       |
| -------------------- | ---------------------------------- | -------------------------------------------------- |
| Before script        | `<request-name>.before.js`         | Before the request is sent                         |
| Folder before script | `before.js` (in any folder)        | Before every request in that folder and subfolders |
| After script         | `<request-name>.after.js`          | After the response is received                     |
| Folder after script  | `after.js` (in any folder)         | After every request in that folder and subfolders  |
| Action               | `<name>.action.js` (in `actions/`) | On demand, from the actions panel                  |

## Execution order

When you send a request, Postchi runs scripts in this order:

<Steps>
  <Step title="Folder before script">
    If a `before.js` exists in an ancestor folder, it runs first and can modify the request.
  </Step>

  <Step title="Request before script">
    If a `<request-name>.before.js` exists next to the request file, it runs next with the (already-modified) request.
  </Step>

  <Step title="Request is sent">
    Postchi sends the final request.
  </Step>

  <Step title="Request after script">
    If a `<request-name>.after.js` exists, it runs first with the response.
  </Step>

  <Step title="Folder after script">
    If an `after.js` exists in an ancestor folder, it runs last with the same response.
  </Step>
</Steps>

<Note>
  Both the folder-level and request-level after scripts share the same mutation state — all `setEnvironmentVariable` and `setSecret` calls from both scripts are applied together.
</Note>

## How scripts run

Scripts are plain JavaScript wrapped in an async function. Postchi injects variables into the function scope, so you can use them directly without declaring them:

```javascript theme={null}
// 'request', 'env', and 'fetch' are all available — no imports needed
request.headers['X-Request-Time'] = new Date().toISOString();
```

Scripts can use `await` anywhere since they run inside an async context.
