# Create a monitor

> Starts watching one page, or with `scope: "site"` every page reachable from `url` on its exact host.

Source: https://docs.spicrawl.com/api-reference/monitors/monitors-create

## POST /v1/monitors

Operation ID: `monitorsCreate`. API key scope: `monitors`.

Starts watching one page, or with `scope: "site"` every page reachable from `url` on its exact host. The first
successful check records a baseline and reports no change. Later checks
report a change when the watched content differs from the last accepted version, after `confirm_checks`
checks in a row agree on the new version.

What is compared is the main content of the page, or the text of every element matched by `selector`. The
content is normalised first (runs of whitespace collapse, lines are trimmed, empty lines drop), so layout
churn does not count as a change.

`interval_seconds` defaults to the plan minimum and must be at least that. The interval adapts between the
plan minimum and the value you set: it shortens while the page changes and lengthens while it does not. See
`current_interval_seconds`.

Each check bills the same credits as the scrape that performed it: 1 credit for a plain fetch, more with
`js_render`. Checks are billed to the API key that created the monitor, and that key must stay valid.

`webhook_endpoint_id` must name an endpoint of this organization. Notifications go to it as
`monitor.changed` and `monitor.failed`, if those events are in its `events`.

A site monitor (`scope: "site"`) needs `max_pages`. Each run discovers the site's pages from `robots.txt`,
its sitemaps or the start page's links, fetches the changed ones and sends one `monitor.changed` digest for
the run. `confirm_checks` is ignored for site monitors. Each fetched page, each sitemap file and `robots.txt`
is a plain scrape call and is billed as one (a 404 is not charged); changing `selector` or `js_render` clears
the stored pages, so the next run is a new baseline; a page skipped by its sitemap `lastmod` costs nothing. The Monitors section of the API reference has
the rules.

Plan limits: the number of monitors and the minimum interval depend on the plan. See the Monitors section
of the API reference.

### Example

```bash
curl -X POST "https://api.spicrawl.com/v1/monitors" \
  -H "Authorization: Bearer $SPICRAWL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url":"https://example.com/pricing","name":"pricing-page","interval_seconds":3600,"webhook_endpoint_id":"0190f3a2-7c1e-7d4b-9a2f-3b8e5c6d7e80"}'
```

### Request body

`application/json`, required.

| Field | Type | Required | Description |
|---|---|---|---|
| `url` | string (uri) | yes |  |
| `name` | string | no |  |
| `selector` | string | no | CSS selector. Omit to watch the main content. |
| `interval_seconds` | integer | no | Between the plan minimum and 2,592,000 (30 days). Defaults to the plan minimum. |
| `webhook_endpoint_id` | string | no |  |
| `confirm_checks` | integer; default `1` | no |  |
| `js_render` | boolean; default `false` | no |  |
| `scope` | string: `page`, `site`; default `"page"` | no | `site` watches every page reachable from `url` on its exact host. Requires `max_pages`. |
| `max_pages` | integer | no | Site only, and required for it. The most pages a run considers. Capped by your plan (free 25, starter 250, pro and other paid plans 2,000, enterprise and internal 10,000). |
| `include_paths` | array of string | no | Site only. Path patterns a page must match to be kept. See the Monitors section of the API reference. |
| `exclude_paths` | array of string | no | Site only. Path patterns that drop a page. |
| `max_credits_per_run` | integer | no | Site only. Optional cap on the credits one run may spend. |

Example `mainContent`: Watch the main content of a page once an hour

```json
{
  "url": "https://example.com/pricing",
  "name": "pricing-page",
  "interval_seconds": 3600,
  "webhook_endpoint_id": "0190f3a2-7c1e-7d4b-9a2f-3b8e5c6d7e80"
}
```

Example `selector`: Watch one element, confirmed on two checks

```json
{
  "url": "https://example.com/status",
  "selector": "#status-banner",
  "confirm_checks": 2
}
```

Example `jsRender`: Watch a client-rendered page

```json
{
  "url": "https://app.example.com/stock",
  "js_render": true
}
```

Example `wholeSite`: Watch the blog and docs of a site once a day

```json
{
  "url": "https://example.com",
  "scope": "site",
  "max_pages": 250,
  "include_paths": [
    "/blog/*",
    "/docs/*"
  ],
  "exclude_paths": [
    "/blog/drafts/*"
  ],
  "max_credits_per_run": 500,
  "interval_seconds": 86400
}
```

### Responses

#### 201

The monitor. It is checked for the first time when `next_check_at` comes around.

Headers: `X-Request-Id`.

`application/json`.

| Field | Type | Required | Description |
|---|---|---|---|
| `id` | string | yes |  |
| `name` | string | yes | Defaults to the host of `url`. |
| `url` | string (uri) | yes |  |
| `selector` | string \| null | yes | CSS selector. Every element it matches is watched, as its text, in document order. Null watches the main content of the page. |
| `js_render` | boolean | yes | Render the page in a browser before reading it. Costs more credits; see the Monitors section of the API reference. |
| `interval_seconds` | integer | yes | The interval you asked for. Defaults to the plan minimum. |
| `current_interval_seconds` | integer | yes | The interval in force now. It moves between the plan minimum and `interval_seconds` as the page changes or stays put. |
| `confirm_checks` | integer | yes | A change must be seen on this many checks in a row before it is reported. 1 reports the first change. |
| `webhook_endpoint_id` | string \| null | yes | The endpoint that receives `monitor.changed` and `monitor.failed`. Null when none is set. |
| `enabled` | boolean | yes | False once the monitor was disabled, by repeated failures, by running out of credits, or by `enabled: false`. |
| `disabled_reason` | string \| null | yes | Why the monitor is disabled, when it is. Null while enabled. |
| `last_error` | string \| null | yes | A short description of the most recent failed check. Cleared by a successful check and by re-enabling. |
| `last_checked_at` | string \| null (date-time) | yes |  |
| `last_changed_at` | string \| null (date-time) | yes |  |
| `next_check_at` | string \| null (date-time) | yes |  |
| `consecutive_failures` | integer | yes | Failed checks in a row. A success resets it. The monitor disables itself when it reaches the limit. |
| `created_at` | string (date-time) | yes |  |
| `updated_at` | string (date-time) | yes |  |
| `scope` | string: `page`, `site` | yes | `page` watches one URL. `site` watches every page the start URL leads to on its exact host. Set at create; it cannot be changed. |
| `max_pages` | integer \| null | yes | Site monitors only. The most pages a run considers. Null for page monitors. |
| `include_paths` | array of string | yes | Site monitors only. Path patterns a page must match to be kept. Empty keeps every path. |
| `exclude_paths` | array of string | yes | Site monitors only. Path patterns that drop a page. A page that matches one is dropped even when it matches `include_paths`. The start URL is exempt from both lists. |
| `max_credits_per_run` | integer \| null | yes | Site monitors only. A run stops fetching when the next fetch would exceed this many credits, discovery included. A fetch that costs more than expected can overrun it by about one fetch. Null for no cap. |
| `pages_tracked` | integer \| null | yes | Site monitors only. Pages the monitor currently tracks, not counting removed ones. Null for page monitors. |
| `last_run` |  | yes | Site monitors only. Counts of the most recent run. Null until a run has finished, and for page monitors. |

#### 400

The request is invalid. Not retryable; fix the request using `code` and `diagnostics.hint`.

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

#### 401

Missing, invalid, revoked or expired key. Not retryable with the same key.

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

#### 403

The key lacks the scope this route requires (`ERR::AUTH::INSUFFICIENT_SCOPE`), or the action is not permitted.

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

#### 404

`webhook_endpoint_id` names no endpoint of this organization.

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

#### 409

Conflicts with the resource's current state, for example a session in use (`ERR::SESSION::BUSY`, retryable).

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

#### 500

Internal error. Retryable when `retryable` is true.

Headers: `X-Request-Id`.

`application/problem+json` (`Problem` schema).

Full OpenAPI spec: https://docs.spicrawl.com/openapi.yaml
