The importance of self-contained checks
Checkly’s API Monitoring checks run independently and on different schedules, possibly even following different retry logic when failures occur. It is therefore important not to create dependencies between them, which could introduce false failures and flakiness. Checkly prevents this antipattern by having each check run fully isolated in its own sandbox. But how do you run complex API checks that have prerequisites (e.g., auth tokens, test data) that themselves require an API call or other preparation steps? That’s what setup and teardown scripts are made for:- Setup scripts run before the API check’s main request. Use them to prepare test data, fetch tokens, or configure the request.
- Teardown scripts run after the HTTP request completes. Use them to clean up test data, scrub sensitive information, or normalize responses.
Setup scripts
Setup scripts prepare everything needed before the main HTTP request runs. Common tasks include:- HTTP requests to other services (and parsing responses)
- Encrypting data payloads
- Generating unique IDs, timestamps, and date strings
request object:
| Property | Description | Type |
|---|---|---|
request.method | The HTTP request method, e.g., ‘GET’, ‘POST’ | String |
request.url | The request URL. Query parameters are appended. | String |
request.body | The request body in string format. | String |
request.headers | The request headers. | Object |
request.queryParameters | The request query parameters. | Object |
Authorization header with request.headers['Authorization'] = my_token, or set the request body with request.body = JSON.stringify({ id: 123 }).
Available libraries depend on which Runtime you have chosen. See the runtime specification for details.
Prepare test data
A self-contained check is responsible for preparing (and cleaning up) all the test data it needs. Let’s look at an example. We use Checkly to monitor Checkly! One of our checks verifies thatDELETE /v1/checks/{id} works correctly. To verify that, we need a check to actually delete. We can create a new check in the setup script, retrieve its unique id, and pass it to the main HTTP request.
Our main request hits the DELETE endpoint at https://api.checklyhq.com/v1/checks:

Authorization header set to Bearer <YOUR_CHECKLY_API_KEY>.
Now for the setup script. We use axios to create a dummy check:
SetupScript.js
POST /v1/checks endpoint used in the setup script, not the DELETE endpoint we’re testing. The Checkly check result indicates whether the error occurred in the setup script or in the main HTTP request.
Fetching dynamic test data
Sometimes you want to fetch test data from an external source—maybe the data changes often, or you want to make checks more dynamic. This example creates a new product for a webshop using dynamic data:- Use a setup script to request random device information from an external API.
- Map that data to match the target API’s schema.
- Send the data to
fakestoreapi.com.
POST endpoint at https://fakestoreapi.com/products:

SetupScript.js
Teardown scripts
Teardown scripts run after the HTTP request completes but before assertions evaluate. They have access to both therequest and response objects. Common use cases include:
- Cleaning up test data created during the check
- Scrubbing sensitive data from responses before logging
- Normalizing responses for consistent assertions
Cleaning up test data
If your setup script creates test data, use a teardown script to clean it up. This keeps your test environment clean and prevents data accumulation. Building on the earlier example where we created a dummy check to test the delete endpoint, what if we’re testing aGET endpoint instead? We’d need to clean up the dummy check after the test:
TeardownScript.js
Scrubbing sensitive data
Before response data is logged or stored, you might need to redact sensitive information for compliance reasons:TeardownScript.js
Error handling differences
Setup and teardown scripts have different error handling behavior:| Aspect | Setup Scripts | Teardown Scripts |
|---|---|---|
| On error | Check aborts immediately | Check continues |
| HTTP request | Never executes | Already completed |
| Assertions | Never run | Still evaluate |
Combined patterns
Create-then-delete workflow
For testing CRUD operations, you often need to create a resource, test operations on it, and then delete it: Setup script:SetupScript.js
GET https://api.example.com/users (tests retrieving the user)
Teardown script:
TeardownScript.js
Conditional cleanup
Sometimes you only want to clean up if the main request succeeded:TeardownScript.js
Read more
Setup and Teardown Reference
Full reference for setup and teardown scripts, including built-in variables and technical details.
Monitoring as Code
Understand monitoring as code (MaC) via our Checkly CLI.
End to end monitoring
Learn end-to-end monitoring with puppeteer and playwright to test key website flows.