# Route alarms to Slack

Alarms reach people through **alarm policies**, and a policy notifies **alarm channels**. This guide creates a Slack channel, adds it to a policy and proves it with a test message, using only the API. It is what an assistant acting for your organisation needs to set up alerting end to end.

## What the key needs

Four scopes, besides the same permissions on the role of the key’s owner: `channels.edit` to create the channel and send the test message, `policies.view` and `policies.edit` to read and change a policy. Add `channels.view` to read the channel back. A key made before these scopes existed holds none of them: add them to the key in the console, under **Organisation → API keys**. See [authentication and keys](https://developer.watchtower15.com/api/authentication/#scopes).

## 1\. Create the channel

Make an incoming webhook in Slack for the channel you want, and send its URL once, in the body. The URL is write-only: no answer ever returns it.

```bash
curl -sS https://rest.watchtower15.com/v1/alarm-channels \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d "{\"type\": \"slack\", \"name\": \"notifications-prod-alarms\", \"webhook_url\": \"$SLACK_WEBHOOK_URL\"}"
```

The answer is `201` with the channel: its `uuid`, `type`, `name` and `active`. Keep the `uuid`. Read the webhook URL from a protected file or your secret store, never from the command line of a shared machine or a chat.

## 2\. Send a test message

```bash
curl -sS -X POST https://rest.watchtower15.com/v1/alarm-channels/$CHANNEL_UUID/test \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)"
```

The answer is `200` with `"sent": true`, and the message arrives in the Slack channel. The call is limited to a few a minute, and it is refused, with a `422`, when the saved destination is no longer allowed (a private address, for instance).

## 3\. Add the channel to a policy

List the policies with `GET /v1/alarm-policies`, read the one you want with `GET /v1/alarm-policies/{policy_uuid}`, and send the policy back with the channel added to its `notifies`. A `notifies` list **replaces** the saved one, so send every target you want to keep, each as it was read (`target_type` and `target_uuid`), and the new one:

```bash
curl -sS -X PUT https://rest.watchtower15.com/v1/alarm-policies/$POLICY_UUID \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d "{\"notifies\": [{\"target_type\": \"destination\", \"target_uuid\": \"$CHANNEL_UUID\"}]}"
```

A policy with `all_channels` true already notifies every channel of the organisation, the new one included, and needs no change. Read the policy again to check its `notifies`.

## Send only critical alarms to a channel

A policy has two routes: `notifies` and its `all_…` switches are who it notifies for a **warning** incident, and for a critical one too while `critical_uses_warning` is `true`, which is the default. To send critical incidents somewhere else, send a `critical` object in a `PUT`: it holds the critical route’s own `all_users`, `all_channels`, `all_groups`, `all_users_channels`, `all_groups_channels` and `notifies`, in the same shapes as the top level, and it makes the policy split. Members you leave out take their defaults, and the object **replaces the whole critical route**, so send everything it should hold. This keeps the saved warning route and adds the new channel for critical incidents only:

```bash
curl -sS -X PUT https://rest.watchtower15.com/v1/alarm-policies/$POLICY_UUID \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d "{\"critical\": {\"notifies\": [{\"target_type\": \"destination\", \"target_uuid\": \"$CHANNEL_UUID\"}]}}"
```

The answer reads `critical_uses_warning: false` and the `critical` object, and its top-level `notifies` still lists only the warning route. Sending `critical_uses_warning: false` with no `critical` object starts the critical route as a copy of the warning route, which you then change.

To send critical incidents to the same people as warning ones again, send `critical_uses_warning: true`: it clears the critical route. Sending it together with a `critical` object is refused with a `422` on `critical`. A `null` in either field is read as not sent, so reading a policy and sending the answer back unchanged keeps it as it was.

## Changing and removing

`PUT /v1/alarm-channels/{channel_uuid}` renames a channel, replaces its destination or switches it off; its type does not change. `DELETE` removes it, and is refused while a policy still notifies it: remove it from the policy’s `notifies` first. See [changing resources](https://developer.watchtower15.com/api/changing-resources/) for the rules every write follows.

Source: https://developer.watchtower15.com/api/route-alarms-to-slack/
