# Price alerts and notifications

Create price alerts that fire once when a market's price crosses a level, and read them in the notification center.

A price alert watches one market's price and fires once when the price crosses a level you choose. A fired alert is delivered to the notification center, where the account reads it with its other notifications.

- **[PriceAlertService](https://testnet.polyester.com/docs/api-docs/connect/alerts.v1.PriceAlertService/CreateAlert)** (`alerts.v1`) creates, lists, updates, and cancels alerts.
- **[NotificationService](https://testnet.polyester.com/docs/api-docs/connect/notifications.v1.NotificationService/ListNotifications)** (`notifications.v1`) lists notifications, counts unread ones, and marks them read or unread.

Both are ConnectRPC only, with no REST route.

## SDKs

- **TypeScript:** [`client.priceAlerts`](https://testnet.polyester.com/docs/sdk/typescript/reference/price-alerts) and [`client.notifications`](https://testnet.polyester.com/docs/sdk/typescript/reference/notifications).
- **Python:** [`client.alerts`](https://testnet.polyester.com/docs/sdk/python/reference/alerts).
- **Go:** [`client.Alerts`](https://testnet.polyester.com/docs/sdk/go/reference/alerts).
- **Rust:** [`client.alerts`](https://testnet.polyester.com/docs/sdk/rust/reference/alerts).

The Python, Go and Rust SDKs cover price alerts; read the notification center with TypeScript or ConnectRPC.

```python
alert = await client.alerts.create(
    symbol="BTC-USDT",
    direction="above",
    price="70000",
    channels=["in_app"],
    idempotency_key="btc-70k-alert",
)
print(alert.alert_id, alert.status, alert.armed, alert.reference_price)
```

## Access

Alerts and notifications belong to the Main Account. A session and the Main Account's API keys see the same alerts.

- **Price alerts** accept a session, or a Main Account API key whose policy grants `READ_PRICE_ALERTS` to list alerts and `MANAGE_PRICE_ALERTS` to create, update, or cancel them. Neither permission implies the other. A key without the permission is refused with `PERMISSION_DENIED` and `AUTH_API_KEY_ACCESS_DENIED`; a Subaccount key is refused with `PERMISSION_DENIED` and `SUBACCOUNT_KEY_REFUSED`. See [API key policies](https://testnet.polyester.com/docs/developer-docs/account/api-key-policies#price-alerts-main-account).
- **Notifications** accept a session, or a Main Account API key whose policy grants `READ_NOTIFICATIONS` for `ListNotifications`, `GetNotification`, and `GetNotificationCounts`. Marking notifications read or unread takes a session only; an API key is refused with `PERMISSION_DENIED` and `AUTH_SESSION_KIND_NOT_ALLOWED`. A Subaccount key is refused with `PERMISSION_DENIED` and `AUTH_API_KEY_ACCESS_DENIED`.

## How an alert fires

- **Condition.** `ABOVE` fires when the price rises to or above the alert price; `BELOW` fires when it falls to or below it.
- **Price source.** An alert watches the market's last traded price (`LAST_PRICE`) or its index price (`INDEX_PRICE`). Leave the source out to use the index price where the market has one, and the last traded price otherwise; the returned alert states which. A market without an index price refuses `INDEX_PRICE` with `PRICE_SOURCE_UNSUPPORTED`.
- **Crossing.** Creating an alert records the current price as `referencePriceTicks`. An alert only fires on a crossing it observes, so one created already past its level starts with `armed` false and waits for the price to move back and cross again.
- **Once.** An alert fires at most once and becomes `TRIGGERED`. Late prices and a feed that stops updating never fire it.
- **No expiry.** Alerts do not expire and do not repeat. An `ACTIVE` alert waits until it fires or you cancel it.

The status is `ACTIVE`, `TRIGGERED`, or `CANCELLED`.

## Create, update, and cancel

- **[`CreateAlert`](https://testnet.polyester.com/docs/api-docs/connect/alerts.v1.PriceAlertService/CreateAlert)** takes the market, direction, price, channels, an optional price source, and an optional `idempotencyKey`. A retry with the same key and request returns the alert the first request created.
- **[`UpdateAlert`](https://testnet.polyester.com/docs/api-docs/connect/alerts.v1.PriceAlertService/UpdateAlert)** changes the direction, price, or channels named in `updateMask`, with the `revision` from your last read as `expectedRevision`. A new direction or price records a new reference price. Updating a `TRIGGERED` alert sets it again, even with an empty mask. A replay with the same `idempotencyKey` returns outcome `REPLAYED` and changes nothing, so a retried update never sets the alert again a second time.
- **[`CancelAlert`](https://testnet.polyester.com/docs/api-docs/connect/alerts.v1.PriceAlertService/CancelAlert)** cancels an `ACTIVE` alert, which then never fires. Cancelling it again returns it unchanged. A `TRIGGERED` alert is refused with `ALERT_TRIGGERED`; a `CANCELLED` alert can't be updated (`ALERT_CANCELLED`).
- **[`ListAlerts`](https://testnet.polyester.com/docs/api-docs/connect/alerts.v1.PriceAlertService/ListAlerts)** returns alerts newest first, filtered by status and market, up to 200 per page. Reuse `nextPageToken` with the same filters.

A Main Account holds at most 100 active alerts. Creating another, or setting a fired alert again, is refused with `ALERT_LIMIT_REACHED`; cancel an alert first. A `SERVICE_UNAVAILABLE` on a create, update, or cancel leaves the outcome unknown: send it again with the same `idempotencyKey`, or list the alerts before retrying. See [Error codes](https://testnet.polyester.com/docs/developer-docs/error-handling/error-codes#price-alerts).

## Delivery

`channels` names one or both of `IN_APP` and `TELEGRAM`. `IN_APP` delivers the fired alert to the notification center as a `PRICE_ALERT` notification. `TELEGRAM` sends a direct message to the Telegram account linked to the user's account. An alert delivered only by Telegram appears in the notification center only when its Telegram message can't be delivered, for example after Telegram was unlinked.

Telegram delivery is available on Testnet. [Link Telegram](https://testnet.polyester.com/docs/developer-docs/getting-started/other-apis#telegram-linking) before creating or updating an alert that keeps `TELEGRAM` among its channels. Without a linked account, the request fails with `FAILED_PRECONDITION` and `TELEGRAM_NOT_LINKED`. If Telegram delivery is disabled, the same requests return `TELEGRAM_UNAVAILABLE`. To change an alert without Telegram, set its channels to `IN_APP`.

## Notification center

[`ListNotifications`](https://testnet.polyester.com/docs/api-docs/connect/notifications.v1.NotificationService/ListNotifications) returns the account's notifications newest first: 25 per page by default, at most 100. Filter by category (`ALERTS` or `SECURITY`), read state, and creation time. `nextPageToken` is an exclusive cursor bound to the account and the filters, so reuse it only with the same filters.

Each notification has a `type`, its `category`, a plain-text `title` and `text`, and a typed detail. Exactly one detail is set, the one its type names. Show the title and text for a type your client does not recognize.

| `type`                    | Category   | Reports                                                                                                                                                                                                                |
| ------------------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `PRICE_ALERT`             | `ALERTS`   | A price alert fired: detail `priceAlert` with the alert ID, market, level, and the price that fired it.                                                                                                                |
| `TELEGRAM_APPROVAL`       | `SECURITY` | An assistant action was approved from the linked Telegram account: detail `telegramApproval` with the action, the account it acted on, and its key parameters.                                                         |
| `TELEGRAM_LINKED`         | `SECURITY` | A Telegram account was linked to the account: detail `telegramLink` with `linkedAt`. The linked Telegram account also receives it.                                                                                     |
| `ADMIN_KEY_ACTION`        | `SECURITY` | An [admin API key](https://testnet.polyester.com/docs/developer-docs/authentication-security/admin-api-keys) created, updated, or revoked an API key, or set an API key or Subaccount policy: detail `adminKeyAction`. |
| `BROKER_ALLOWLIST_CHANGE` | `SECURITY` | A QuickSwap broker vault payout address was requested to be added or removed, or the change was applied or not: detail `brokerAllowlist`, sent to the broker's Owner and Finance members.                              |

A security notification the user does not recognize can mean someone else acts on the account: for a Telegram type, unlink Telegram and review the account's sessions; for `ADMIN_KEY_ACTION`, revoke the admin key and review the account's API keys.

- **Read one** with [`GetNotification`](https://testnet.polyester.com/docs/api-docs/connect/notifications.v1.NotificationService/GetNotification).
- **Count** total and unread notifications, by type, with [`GetNotificationCounts`](https://testnet.polyester.com/docs/api-docs/connect/notifications.v1.NotificationService/GetNotificationCounts).
- **Mark read or unread** with [`MarkNotificationsRead`](https://testnet.polyester.com/docs/api-docs/connect/notifications.v1.NotificationService/MarkNotificationsRead) and [`MarkNotificationsUnread`](https://testnet.polyester.com/docs/api-docs/connect/notifications.v1.NotificationService/MarkNotificationsUnread): 1 to 100 IDs per call. Either every ID is the account's and each is marked, or nothing changes and the call fails with `NOTIFICATION_NOT_FOUND`. `readAt` is set while a notification is read.
- **Mark all read** with [`MarkAllNotificationsRead`](https://testnet.polyester.com/docs/api-docs/connect/notifications.v1.NotificationService/MarkAllNotificationsRead), optionally by category. Pass the newest notification you have shown as `throughNotificationId`, so one that arrives later stays unread.

There is no streaming channel for notifications. Poll `GetNotificationCounts` for new unread notifications, and list when the count changes.

### Dust conversion approvals

`TelegramApprovalAction.CONVERT_DUST` identifies approval to convert dust into one Asset within Trading. It appears as `telegramApproval.action` on a `TELEGRAM_APPROVAL` notification, rather than as a separate notification type. See [Dust conversion](https://testnet.polyester.com/docs/developer-docs/account/dust-conversion).

## Rate limits

Alert creates, updates, and cancels, and read-state changes, spend from the `ACCOUNT_ADMIN` pool. See [Rate limits](https://testnet.polyester.com/docs/developer-docs/shared-concepts/rate-limits#authentication-and-account-management).
