Skip to main content
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:
For a script that applies to every request in a folder, create after.js in that folder:
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.

Available context

Postchi injects these variables into every after script:
VariableTypeDescription
requestobjectThe final request that was sent (method, url, headers, body).
responseobjectThe response received.
envobjectAll active environment variables as string key-value pairs. Read-only.
fetchfunctionThe global fetch function.
setEnvironmentVariable(key, value)functionSaves a variable to the active environment in environments.cenv.
setSecret(key, value)functionSaves a secret to the active environment in secrets.cenv.

The response object

PropertyTypeDescription
response.statusnumberHTTP status code, e.g. 200
response.headersobjectResponse headers as { name: value } pairs
response.bodystring | nullResponse body as a string, or null

Examples

Capture an auth token

Save a refresh token as a secret

Log response details for debugging

Notes

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