Skip to main content
Before scripts run immediately before Postchi sends a request. You receive the request object and can mutate it freely — whatever state it’s in when the script finishes is what gets sent.

File naming

Place a file named <request-name>.before.js in the same folder as your request file:
For a script that applies to every request in a folder, create before.js in that folder:
The folder-level before.js runs first, then the request-level <name>.before.js. This means the request-level script always sees changes made by the folder-level script.

Available context

Postchi injects these variables into every before script:
VariableTypeDescription
requestobjectThe request about to be sent. Mutating this object changes what Postchi sends.
envobjectAll active environment variables as string key-value pairs. Read-only.
fetchfunctionThe global fetch function.

The request object

PropertyTypeDescription
request.methodstringHTTP method, e.g. "GET"
request.urlstringFull URL including any template variables already resolved
request.headersobjectHeaders as { name: value } pairs
request.bodystring | nullRequest body as a string, or null

Examples

Add a dynamic header

Build the URL from environment variables

Modify a JSON body

Add a Bearer token from the environment

Notes

Before scripts cannot cancel a request — they can only transform it. If your script throws an error, Postchi surfaces the error and does not send the request.
Put shared authentication logic in a folder-level before.js so you only write it once and it applies to every request in that folder automatically.