Skip to main content
Setup and teardown scripts execute arbitrary JavaScript/TypeScript code before and after an API check’s HTTP request. Both script types have access to environment variables, runtime objects like request and response, and popular npm packages like moment, axios, and lodash. To get the most power out of API checks with setup and teardown scripts, we advise using the Checkly CLI. You can also use them via the web UI.
Setup and teardown scripts have a maximum execution time of 10 seconds each.
Check the video below for a quick overview of using setup and teardown scripts through the web UI and with the CLI.

How it works

Setup and teardown scripts run at specific points in the API check lifecycle. Understanding this flow helps you use them effectively. Execution order:
  1. Setup scripts run first and can modify the request before it’s sent.
  2. The HTTP request executes.
  3. Teardown scripts run and can access or modify the response.
  4. Assertions evaluate against the (possibly modified) response.

Setup scripts

Setup scripts execute before the HTTP request. Use them to prepare test data, configure request parameters, and handle authentication. You have access to built-in variables to modify the HTTP request and a set of libraries available in each runtime.

Modifying the request

You can modify any property of the request object:

Authentication example

A common task for setup scripts is fetching or signing session tokens. You can centralize this logic and reuse it across API checks.
  1. Create an API check and reference the setup script (setup.ts) using the entrypoint property.
  2. Encapsulate authentication logic in a separate auth-client.ts file.
  3. In the setup.ts file, import the auth client and update the request object.
Your folder structure:
The API check performs a GET on an authenticated endpoint:
api.check.ts
The setup script uses the auth client and sets the Authorization header:
setup.ts
The auth client reads a static environment variable for authentication:
common/auth-client.ts
Benefits of this structure:
  • Reuse authentication logic across multiple API checks
  • Test authentication logic separately from Checkly-specific code
Check our setup script examples for OAuth2, HMAC signing, JWT tokens, and other auth methods.

Error handling in setup scripts

If a setup script throws an error or times out, the check aborts immediately. The HTTP request never executes, and assertions don’t run.

Teardown scripts

Teardown scripts run after the HTTP request completes but before assertions evaluate. They have access to both the request and response objects. Common use cases:
  • Cleaning up test data created during the check
  • Scrubbing sensitive data from responses before logging
  • Normalizing response data for consistent assertions
  • Extracting and storing values for subsequent checks

Accessing the response

The response object contains the full HTTP response:

Modifying the response

You can modify response properties before assertions run. This is useful for scrubbing sensitive data or normalizing inconsistent API responses.

Cleaning up test data

If your setup script creates test data, use the teardown script to clean it up:

Error handling in teardown scripts

Unlike setup scripts, teardown errors do not abort the check. The check continues and assertions still evaluate. However, if an error occurs, it may mark the check as failed depending on the error type.

Built-in variables

Inside each script, you have access to specific data structures from the API check lifecycle.

Environment variables

You have access to all environment variables configured in your account. You can create, read, update, and delete attributes, but mutations only persist for the duration of a single check run. The current data center location is exposed as the region code in the REGION constant, e.g., eu-west-1 or us-east-1.
In setup scripts, the modified environment object is used for the subsequent HTTP request. In teardown scripts, modifications are only available for the remainder of that script’s execution. More about using environment variables

Request

Request properties are exposed as a standard JavaScript object. This object is available in both setup and teardown scripts.

Response

Response properties are exposed as a standard JavaScript object. These are only available in teardown scripts.

Built-in runtime variables

The setup and teardown runtime exposes specific environment variables, in addition to generic runtime variables like process.env.CHECK_NAME.

Setup & teardown specific variables

Generic runtime variables

Technical reference

Included libraries

All setup and teardown scripts run in a sandboxed environment. You do not have full access to the Node.js standard library or arbitrary npm packages. Check our runtimes documentation for a full specification of included modules.

Limitations

  • Setup and teardown scripts are implicitly wrapped in an async function. You can always use await statements.
  • You cannot use nested callbacks as there is no way to determine the callback function. Always use await statements.
  • You need to include modules and libraries explicitly, e.g., const moment = require('moment').
  • You can pass a maximum of 256KB of data to and from the check’s main request.

Using setup and teardown scripts via the UI

When using the Checkly web UI, you can use setup and teardown scripts in two modes: 1. Inline scripts Write JS/TS code directly in the editor. Best for quick, one-off pieces of code. setup and teardown script for http requests 2. Snippets Use snippets to share code across multiple checks. This approach is similar to working with the CLI but less flexible. reusable code snippets Select snippets from the dropdown in the setup and teardown section of your API check. setup and teardown script for http requests with snippets You can also import snippets from within your setup or teardown scripts. See the snippet documentation for details.

Using environment variables via Handlebars

If your API check’s request body relies on data from a setup script, use environment variables to pass it.
Access environment variables in the request body with {{MY_VALUE}} notation. Checkly API check editor showing a JSON request body that uses the MY_VALUE environment variable. For more examples, check our setup script examples.