# Rule Validation and Preview Endpoints

These instructions apply to the legacy Admin Console.

{% hint style="info" %}
Alerts are in early access and may not be available in your account. [Contact Contextual support](mailto:help@contextual.io) to request that Alerts be enabled for your tenant.
{% endhint %}

Both routes accept either `contextual:alert-rules:create` or `contextual:alert-rules:update`, plus every source-data read permission derived from the supplied rule. Send the full rule object as the request body, not an ID or a template-render wrapper.

## POST /rules/validate

```sh
curl --fail-with-body --silent --show-error \
  "$ALERTS_BASE_URL/rules/validate" \
  -H "Authorization: Bearer $ALERTS_TOKEN" \
  -H "x-org-id: $ALERTS_ORG_ID" \
  -H 'Content-Type: application/json' \
  --data-binary @rule.json
```

The result has `valid`, `requiredPermissions`, and `guardRails`. Each guard-rail entry has `check`, `ok`, and `detail`. Treat `valid: false` or any failed guard rail as a failed validation even when HTTP status is `200`. Schema and authorization failures can return `400` or `403` instead.

Validation is read-only and does not establish a working background connection or notification route.

## POST /rules/preview

Send the complete rule object in the request body to `POST /rules/preview?last=24h`. `last` is a positive lookback with a maximum of 24 hours; the default is `24h`. Do not use a leading minus sign. The service returns at most 12 recent complete windows of the rule’s authored duration, with a ten-second request deadline.

Read these response sections together:

| Field | Use |
| --- | --- |
| `query` | Query description and bounded-observations composition. |
| `query.coverage` | Requested/actual range, selected windows, query counts, and coverage limits. |
| `timeline` | Returned observed values and comparisons. |
| `summary` | Observation count (`points`), breach count (`breaches`), and first and last breach timestamps where present. |
| `guardRails` | Authoring and execution checks. |

Preview does not save resources or send notifications. The response reports threshold comparisons in the sampled windows. It excludes sustained-duration evaluation, alert state transitions, and the effects of silences or repeat notifications. A `wouldHaveFired` field is unavailable.

| Status | What to check |
| --- | --- |
| `400` | Rule or time-range syntax. |
| `401` / `403` | Authentication, tenant scope, or required permissions. |
| `429` | Bounded admission. Wait for the interval in `Retry-After`. |
| `501` | Unsupported history, source, window, or grouped population. A rule whose window does not fit in the lookback, such as any `24h` window, returns `preview_window_unsupported`. The service can also return this status when it cannot establish a grouped population. |
| `502` | Failed provider evidence, including malformed or truncated observations. |
| `503` | Temporary admission or deadline failure. Retry later. |

Surface these errors to the caller so it can distinguish a failed preview from a successful response with no observations.
