# Subscribe a browser to push

Register a browser's push subscription, so alerts arrive as notifications.

POST `/api/v1/notifications/push`

**cURL**

```shell
curl -X POST 'https://www.prufy.dev/api/v1/notifications/push' \
  -b cookies.txt \
  -H 'content-type: application/json' \
  -d '{
  "endpoint": "https://fcm.googleapis.com/fcm/send/dQw4w9WgXcQ:APA91bHPRgkF3JUikC4ENAHEeMrd41Zxv3hVZjC9KtT8OvPVGJ",
  "keys": {
    "p256dh": "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM",
    "auth": "tBHItJI5svbpez7KI4CCXg"
  }
}'
```

**Node.js**

```javascript
// `cookie` is the session cookie that Create a claim returned.
const response = await fetch("https://www.prufy.dev/api/v1/notifications/push", {
  method: "POST",
  headers: { "content-type": "application/json", cookie },
  body: JSON.stringify({
    endpoint: "https://fcm.googleapis.com/fcm/send/dQw4w9WgXcQ:APA91bHPRgkF3JUikC4ENAHEeMrd41Zxv3hVZjC9KtT8OvPVGJ",
    keys: {
      p256dh: "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM",
      auth: "tBHItJI5svbpez7KI4CCXg",
    },
  }),
})
const data = await response.json()
```

**Python**

```python
import requests

# `session` is the requests.Session() that created the claim.
response = session.post(
    "https://www.prufy.dev/api/v1/notifications/push",
    json={
        "endpoint": "https://fcm.googleapis.com/fcm/send/dQw4w9WgXcQ:APA91bHPRgkF3JUikC4ENAHEeMrd41Zxv3hVZjC9KtT8OvPVGJ",
        "keys": {
            "p256dh": "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM",
            "auth": "tBHItJI5svbpez7KI4CCXg",
        },
    },
)
data = response.json()
```

Response 200 OK

```json
{
  "email": {
    "address": "owner@yourcompany.com",
    "verified": true
  },
  "pushDevices": 2,
  "capabilities": {
    "email": true,
    "pushPublicKey": "BEl62iUYgUivxIkv69yViEuiBIa-Ib9-SkvMeAtA3LFgDzkrxZJjSgSnfckjBJuBkr3qBUYIHBQFLXYp5Nksh8U"
  }
}
```

The body is what `PushManager.subscribe()` returns, made with the key in `capabilities.pushPublicKey`. Only endpoints at the push services browsers actually use are accepted.

Subscribing the same endpoint twice keeps one subscription.

## Authentication

Send the session cookie. Without it the answer is `unauthorized`. [How sessions work](https://www.prufy.dev/docs/authentication.md).

## Body

JSON, with `content-type: application/json`.

- `endpoint` URL required

  The push service's address for this browser.

  - At most 2,000 characters
- `keys` object required

  The subscription's keys, as the browser gives them.

  - `p256dh` string

    The browser's public key.

    - At most 256 characters
  - `auth` string

    The browser's authentication secret.

    - At most 128 characters

## Response

`200 OK`, with every field below present. Times are ISO 8601 strings in UTC.

- `email` object | null

  The alert address. Null when none is set.

  - `address` string

    Where alert emails go.
  - `verified` boolean

    False until the confirmation link has been used.
- `pushDevices` integer

  How many browsers are subscribed to push.
- `capabilities` object

  Which channels this server can use.

  - `email` boolean

    Whether this server can send email at all.
  - `pushPublicKey` string | null

    The key a browser subscribes with. Null when push is off.

## Errors

| Status | Kind | When |
| --- | --- | --- |
| 400 | `bad_request` | The body doesn't match the shape below, or the endpoint isn't a push service we recognise. |
| 401 | `unauthorized` | The request has no session cookie, or one we don't recognise. |
| 503 | `push_unavailable` | This server has no push keys set up. |

Any route can also answer `500` with `internal_error`. Every error has the same shape, described in [Errors](https://www.prufy.dev/docs/errors.md).

---

Every page of these docs, as a list: https://www.prufy.dev/llms.txt
