> ## 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.

# After scripts

> Extract values from a response and save them to environment variables or secrets.

After scripts run once Postchi receives a response. They're the right place to capture tokens, IDs, or any other value you want to reuse in later requests.

## File naming

Place a file named `<request-name>.after.js` next to your request file:

```
requests/
  auth/
    login.chttp
    login.after.js    ← runs after login.chttp
```

For a script that applies to every request in a folder, create `after.js` in that folder:

```
requests/
  auth/
    after.js          ← runs after every request in auth/ and subfolders
    login.chttp
    refresh.chttp
```

<Note>
  The request-level `<name>.after.js` runs first, then the folder-level `after.js`. Both scripts can call `setEnvironmentVariable` and `setSecret` — all mutations from both are applied together.
</Note>

## Available context

Postchi injects these variables into every after script:

| Variable                             | Type     | Description                                                            |
| ------------------------------------ | -------- | ---------------------------------------------------------------------- |
| `request`                            | object   | The final request that was sent (method, url, headers, body).          |
| `response`                           | object   | The response received.                                                 |
| `env`                                | object   | All active environment variables as string key-value pairs. Read-only. |
| `fetch`                              | function | The global `fetch` function.                                           |
| `setEnvironmentVariable(key, value)` | function | Saves a variable to the active environment in `environments.cenv`.     |
| `setSecret(key, value)`              | function | Saves a secret to the active environment in `secrets.cenv`.            |

### The `response` object

| Property           | Type           | Description                                 |
| ------------------ | -------------- | ------------------------------------------- |
| `response.status`  | number         | HTTP status code, e.g. `200`                |
| `response.headers` | object         | Response headers as `{ name: value }` pairs |
| `response.body`    | string \| null | Response body as a string, or `null`        |

## Examples

### Capture an auth token

```javascript theme={null}
// After a login request, save the token to the environment
const data = JSON.parse(response.body);
if (response.status === 200 && data.token) {
  setEnvironmentVariable('api_token', data.token);
}
```

### Save a refresh token as a secret

```javascript theme={null}
const data = JSON.parse(response.body);
setSecret('refresh_token', data.refresh_token);
setEnvironmentVariable('access_token', data.access_token);
```

### Log response details for debugging

```javascript theme={null}
console.log('Response status:', response.status);
console.log('User ID:', JSON.parse(response.body).id);
```

## Notes

<Warning>
  If an after script throws an error, Postchi shows the error message in the response view but still displays the response. Your mutations up to the point of the error are not applied.
</Warning>

<Tip>
  Use a folder-level `after.js` to log or assert a common shape across all responses in a folder — useful for catching unexpected errors during development.
</Tip>
