Pre and Post Scripts
When Scripts Run
Every collection and every API has its own Pre Script and Post Script. They run on the server, not in the browser. When an API is called, the collection's scripts and the API's scripts run together in a fixed order around the request.
- Collection Pre Script
- API Pre Script
- The request is sent
- Collection Post Script
- API Post Script

Figure 1. The Script tab of an API, with the Pre Script and Post Script tabs and the Script Help panel.
Pre Scripts
A Pre Script shapes the outgoing request. Use it to set authentication, add a header or fill in a value the request needs. Return the request object with your changes, or return nothing if nothing changed. The default starting point is below.
// request is the outgoing request: url, headers, params, body, auth
return request;
Post Scripts
A Post Script shapes the response before it reaches the CRM. Use it to reformat the response, or to catch an expired token and try again. Return the reshaped response to replace it, or return nothing to pass it through unchanged.
// response is the response body received so far
if (utils.isAuthError(utils.http.status, response)) {
// @Auth is the saved login API. It fetches a fresh token, saves it
// for next time and sends this same request again.
return await utils.refreshTokenAndRetry(@Auth, { tokenPath: "data.token" });
}
return response;
A Post Script is also where an outside system's response is reshaped into the format the CRM requires. See Response Format the CRM Requires.
The @ Mention
Typing @ anywhere in a script opens a menu with two groups: the APIs already saved in this collection, and ready made utils snippets. Picking an API inserts a readable reference such as @Auth. Behind the scenes the reference is stored as the API's numeric ID, and the readable name is only shown while you edit.
Send and the external modules use the saved scripts. After editing a Pre Script or Post Script, click Save on the API, then run Send again.
The full list of helper methods is in Script Method Reference.