Skip to content

Setup and teardown scripts

A setup script runs once just before a check and a teardown script runs once just after it. Use them to fetch a token, prepare data before the check, or clean up what the check created. They work on API checks, multistep checks and browser checks. DNS and TCP monitors do not run them.

To add one, open the check editor, find Run setup or tear down scripts?, click View Editor and use the Setup Script and Teardown Script tabs. Scripts are JavaScript and run in an isolated sandbox on every run, in every region, with their console output captured.

  • API check: the setup script gets request; the teardown script gets request and response (response.statusCode, response.body as text, response.headers). The request object in a setup script is a copy for your script to read. Changing it does not change the request LoadFocus sends, so use setup to do work beforehand (for example, create a record the check needs), and use the teardown script to clean up after the response.
  • Multistep check: the setup script gets ctx. Anything you store in ctx.vars can be used in every step as {{name}}. The teardown script gets ctx and the run result.
  • Browser check: the setup script runs before the browser starts and fills ctx.vars for the flow; the teardown script runs after it.

Inside a script you can require:

  • got and axios: make HTTP calls, for example to exchange credentials for an OAuth token. Calls to private, loopback and link-local addresses are blocked.
  • crypto: hashes and HMAC signatures.
  • @faker-js/faker: realistic unique test data.
  • url and path: helpers for building URLs and paths.
  • Snippets: shared code saved once and loaded with require("./snippets/name"). See Reusable snippets.

console.log output is shown under the script's logs.

Example: fetch an OAuth token in a multistep setup

Section titled “Example: fetch an OAuth token in a multistep setup”
const got = require('got');
const tokenResponse = await got.post('https://auth.example.com/oauth/token', {
json: {
grant_type: 'client_credentials',
client_id: '${{CLIENT_ID}}',
client_secret: '${{CLIENT_SECRET}}',
},
}).json();
// Make the token available to every step as {{token}}
ctx.vars.token = tokenResponse.access_token;

In each step, set the header value to Bearer {{token}}.

Example: clean up in an API check teardown

Section titled “Example: clean up in an API check teardown”
const got = require('got');
// If the request created a resource, delete it so the monitor leaves no trace
if (response.statusCode === 201 && response.body) {
const created = JSON.parse(response.body);
await got.delete('https://api.example.com/items/' + created.id, {
headers: { Authorization: 'Bearer ${{API_TOKEN}}' },
});
}

Reference a stored secret or variable with ${{NAME}} inside a script. LoadFocus replaces it with the value just before the script runs, so credentials never sit in plain text in the script. A reference without the dollar sign, such as {{NAME}}, is not replaced in a script. See Variables and secrets.

In the script editor, RUN SCRIPT runs the script against the real endpoint and shows the result. Open VIEW LOGS to see what the script printed. A test run counts as one run towards your monthly usage.

  • Each script is limited to 100 KB.
  • If the setup script fails, the check is not sent and the run is marked failed. That is usually what you want: no valid token means no valid check.
  • A teardown failure is reported in the logs but does not change the result of the run.