# Add a notification channel.

> Koidog first asks Notify who the token belongs to (`GET /auth/status`): a token Notify refuses, or one without notifications:send, interactions:create and interactions:read, is refused here (`422 notify_token_refused`) and nothing is saved.
> https://koi.dog/docs/api/notifications/create-notification-channel/

`POST /api/v1/notifications/channels` · Scopes: `config`, `approve`

If Notify cannot be reached the channel is saved `failing` and retried. Needs `approve` as well as `config`, because whoever answers prompts on that Notify account decides approvals. Only what happens after the channel is made is sent; older pending items are in the digest. The timeline records the key ID, action and outcome.

## Headers.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | `string` | no | Retry the same request with the same key. [Retry rules](https://koi.dog/docs/api/#retries). |

## Body.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `kind` | `enum` | yes | `notify` sends to a phone through Notify. |
| `name` | `string` | yes | A name to tell channels apart, such as the phone's. 1 to 80 characters. |
| `token` | `string` | yes | Your Notify agent token, with the scopes notifications:send, interactions:create and interactions:read (devices:read is optional: it names the phone that answered). 8 to 2000 characters. |
| `send` | `array` | no | What to send: `approvals`, `questions`, `crew_finished`, `crew_failed`, `budget`, `digest`. Any of `approvals`, `questions`, `crew_finished`, `crew_failed`, `budget`, `digest`. 1 to 6 items. Default `["approvals","questions","crew_failed","digest"]`. |
| `digestMinutes` | `15–10080` | no | The digest is sent at most once in this many minutes, and only when something needs you. Default `1440`. |
| `maxPerHour` | `1–120` | no | At most this many messages and prompts an hour (the digest not counted). Past it, what needs you waits for the digest and the dashboard Inbox. Default `20`. |
| `enabled` | `boolean` | no | False saves the channel switched off: nothing is sent until it is switched on. Default `true`. |

## Response.

201 Created · JSON object

- **422 validation_failed**: Each schema error appears in `details`.

## Request.

```sh
curl -X POST https://api.koi.dog/api/v1/notifications/channels \
    -H "Authorization: Bearer $KOIDOG_API_KEY" \
    -H "Idempotency-Key: $(uuidgen)" \
    -H "Content-Type: application/json" \
    -d '{
      "kind": "notify",
      "name": "My phone",
      "token": "<your Notify agent token>",
      "send": [
        "approvals",
        "questions",
        "crew_failed",
        "digest"
      ]
    }'
```

```js
const res = await fetch("https://api.koi.dog/api/v1/notifications/channels", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.KOIDOG_API_KEY}`,
    "Idempotency-Key": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    kind: "notify",
    name: "My phone",
    token: "<your Notify agent token>",
    send: [
      "approvals",
      "questions",
      "crew_failed",
      "digest",
    ],
  }),
});
console.log(await res.json());
```

```python
import os
import uuid

import requests

r = requests.post(
    "https://api.koi.dog/api/v1/notifications/channels",
    headers={
        "Authorization": f"Bearer {os.environ['KOIDOG_API_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
        "kind": "notify",
        "name": "My phone",
        "token": "<your Notify agent token>",
        "send": [
            "approvals",
            "questions",
            "crew_failed",
            "digest",
        ],
    },
)
r.raise_for_status()
print(r.json())
```

## Response example.

```json
{
  "id": "string",
  "kind": "notify",
  "name": "string",
  "send": [],
  "digestMinutes": 0,
  "maxPerHour": 0,
  "state": "active",
  "stateReason": "string",
  "tokenPrefix": "string",
  "consecutiveFailures": 0,
  "lastSuccessAt": "string",
  "lastFailureAt": "string",
  "lastDigestAt": "string",
  "pendingSends": 0,
  …
}
```
