# Metrics Enrollment

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 %}

Manage the connection used for background metrics evaluation. This API does not return background connection credentials. All routes use the normal tenant authentication boundary.

| Method and route | Alert permission | Additional requirements |
| --- | --- | --- |
| `GET /metrics-enrollment` | `contextual:alert-rules:read` | Read status and caller capabilities. |
| `POST /metrics-enrollment` | `contextual:alert-rules:create` | Provisioning permissions; idempotency key. |
| `POST /metrics-enrollment/reconnect` | `contextual:alert-rules:create` | Provisioning permissions; idempotency key and current hash. |
| `PUT /metrics-enrollment/notifications` | `contextual:alert-rules:update` | Idempotency key, hash, and destination read permission when selecting a destination. |
| `POST /metrics-enrollment/cleanup` | `contextual:alert-rules:update` | Cleanup permissions; idempotency key and hash. |

Setup/reconnect provisioning requires `contextual:clients:read`, `contextual:clients:create`, `contextual:clients:delete`, `contextual:client-grants:read`, `contextual:client-grants:create`, `contextual:client-grants:delete`, `contextual:client-secrets:read`, and `contextual:resource-servers:read`. Use the status response's `setupCapability` and `cleanupCapability` to identify missing permissions for the caller and operation.

## Status

`GET /metrics-enrollment` returns readiness, `hash`, health and failure information, verification/access timestamps, caller capabilities, notification settings, cleanup state, and the current or last operation where present.

Inspect `readiness`, `health`, `operation.state`, `cleanupPending`, and `notificationStatus` separately. A `200` mutation response can still describe a pending or failed operation. Readiness tracks provisioning, health tracks access to metrics, and cleanup can remain pending while the active connection works.

## Setup and Reconnect

Both accept an empty object or optional notifications:

```json
{
  "notifications": {
    "destinationId": "DESTINATION_ID",
    "notifyOnRecovery": false
  }
}
```

Omitting notifications on reconnect preserves existing settings. Include `Idempotency-Key` containing 1–200 characters on every mutation. Setup permits `x-if-hash-match`; reconnect, notification updates, and cleanup require it.

For a **new** operation, use a new idempotency key and the latest status hash. For a **retry of the same operation**, reuse the same key, exact input, and original hash. Do not refresh the hash while retaining the old operation key. When status supplies `operation.retry`, use its recorded `idempotencyKey`, `ifHashMatch`, and `input` to resume the operation.

After setup, refresh status until it reports the actual outcome. Reconnect verifies a replacement before switching the active connection. Old-resource cleanup may still need completion.

## Health Notification Settings

PUT accepts `destinationId` and `notifyOnRecovery`. To explicitly detach external health warnings:

```json
{ "detach": true, "notifyOnRecovery": false }
```

Use a fresh operation key and status hash. Do not interpret omission of a destination as consent to detach it. These health warnings operate independently of ordinary rule subscriptions and silences.

## Cleanup

Choose an ID returned in `cleanupOperationIds` and send:

```json
{ "operationId": "CLEANUP_OPERATION_ID" }
```

Cleanup retires resources for that operation while preserving the active connection. Re-read status for remaining IDs and the result. It is not an enrollment-delete endpoint.
