# Dynamic monitor creation - Checkly Docs

> Create monitors dynamically with the Checkly CLI by looping over lists of targets in TypeScript or JavaScript, managing checks at scale without duplication.

Source: https://www.checklyhq.com/docs/constructs/dynamic-monitor-creation/

---

Back to top
- Similar checks from a list of targets
- Separate groups for prod and pre-prod

Getting Started

# Dynamic monitor creation

Create monitors dynamically with the Checkly CLI by looping over lists of targets in TypeScript or JavaScript, managing checks at scale without duplication.

The [Checkly CLI](https://www.checklyhq.com/docs/cli/overview/) enables you to code your entire monitoring setup taking full advantage of the flexibility of TypeScript/JavaScript. Reusing language constructs that you are already familiar with, you will be able to create a MaC setup that neatly fits your unique use cases and workflows.
This page shows a few examples.

## Similar checks from a list of targets

Iterating through lists of target URLs is an easy way to manage checks at scale while avoiding code duplication.
__checks__/api.check.ts

```
import { ApiCheck, AssertionBuilder } from 'checkly/constructs'

const publicResources = ['/public-stats', '/v1/runtimes']

for (const publicResource of publicResources) {
new ApiCheck(`public-resource_${publicResource}`, {
name: `Public Resource ${publicResource}`,
request: {
url: `https://api.checkly.com${publicResource}`,
method: 'GET',
followRedirects: true,
skipSSL: false,
assertions: [ AssertionBuilder.statusCode().equals(200) ]
}
})
}
```

Asynchronous operations are supported by exporting an async function from your check files, too.
__checks__/api.check.ts

```
import { ApiCheck, AssertionBuilder } from 'checkly/constructs'
import { getPublicResources } from './helpers'

// an exported async function to signal that
// this check file performs asynchronous operations
export default async function createApiChecks() {
const publicResources = await getPublicResources();

for (const publicResource of publicResources) {
new ApiCheck(`public-resource_${publicResource}`, {
name: `Public Resource ${publicResource}`,
request: {
url: `https://api.checkly.com${publicResource}`,
method: 'GET',
followRedirects: true,
skipSSL: false,
assertions: [ AssertionBuilder.statusCode().equals(200) ]
}
})
}
}
```

## Separate groups for prod and pre-prod

Iterating through target environments (like `preview` and `production`) linked to [`Group`](https://www.checklyhq.com/docs/constructs/check-group-v2) resources allows you to reuse existing `Check` definitions.
__checks__/browser.check.ts

```
import fs from 'fs'
import { BrowserCheck } from 'checkly/constructs'
import { groupProd, groupPreview } from './groups.check'

// This reads a directory and extracts all file paths containing '.spec.ts'
const files = fs.readdirSync('__checks__/')
const specFiles = files.filter((filename) => {
return filename.includes('.spec.ts')
})

// This is the list of environments and their matching group; it can be extended easily
const environments = [
{ name: 'preview', group: groupPreview },
{ name: 'production', group: groupProd },
]

// Here we create a new browser check for each environment x testspec combination
// Checks are added to the right groups - the group will set the right env variable for the target URL
environments.forEach((environment) => {
for (const specFile of specFiles) {
new BrowserCheck(`${specFile}${environment.name}`, {
name: `${specFile} [${environment.name}]`,
tags: [`${environment.name}`],
group: environment.group,
code: {
entrypoint: specFile,
},
})
}
})
```

You can handle potential differences between target environments via group-level [environment variables](https://www.checklyhq.com/docs/platform/variables/), which are made available to all checks within a group.
__checks__/group.check.ts

```
import { CheckGroupV2 } from 'checkly/constructs'
import { smsChannel, emailChannel } from '../alert-channels'
const alertChannels = [smsChannel, emailChannel]

export const groupPreview = new CheckGroupV2('group-browser-preview', {
name: 'WebShop - Preview',
activated: true,
muted: false,
runtimeId: '2025.04',
locations: ['us-east-1', 'eu-west-1'],
tags: ['mac', 'preview'],
// You can use group-level environment vars to point each group's checks to the right target URL
environmentVariables: [ { key: 'TARGET_URL', value: 'https://preview.mywebsite.com' }],
apiCheckDefaults: {},
concurrency: 100,
alertEscalationPolicy: 'global',
alertChannels
})

export const groupProd = new CheckGroupV2('group-browser-prod', {
name: 'WebShop - Production',
activated: true,
muted: false,
runtimeId: '2025.04',
locations: ['us-east-1', 'eu-west-1'],
tags: ['mac', 'production'],
// You can use group-level environment vars to point each group's checks to the right target URL
environmentVariables: [ { key: 'TARGET_URL', value: 'https://www.mywebsite.com' }],
apiCheckDefaults: {},
concurrency: 100,
alertEscalationPolicy: 'global',
alertChannels
})
```

Was this page helpful?

[Suggest edits](https://github.com/checkly/docs/edit/main/constructs/dynamic-monitor-creation.mdx)[Raise issue](https://github.com/checkly/docs/issues/new?title=Issue%20on%20docs&body=Path:%20/constructs/dynamic-monitor-creation)

[Create Checks Automatically from Files Previous](https://www.checklyhq.com/docs/constructs/including-checks)[UrlMonitor Construct Next](https://www.checklyhq.com/docs/constructs/url-monitor)

[x](https://x.com/checklyhq)[github](https://github.com/checkly)[linkedin](https://linkedin.com/company/checkly)

[Powered by This documentation is built and hosted on Mintlify, a developer documentation platform](https://www.mintlify.com/?utm_campaign=poweredBy&utm_medium=referral&utm_source=checkly-422f444a)
