client.priceAlerts manages the caller's price alerts. Alerts belong to the root account, not a
trading account: a session and the root account's API keys see the same alerts. No call changes
balances, orders, or account settings. Errors carry detail: { service: "alerts", code }.
Tree-shaken builds reach the same service with priceAlertsService(core) from @polyester/sdk/services/price-alerts.
API keys need read-price-alerts for list and manage-price-alerts for create, update, and cancel. A key whose policy lacks the permission gets PermissionError with detail.code === "AUTH_API_KEY_ACCESS_DENIED". Both are main-account permissions: sub-account
policies reject them, and a key scoped to a subaccount is refused with PermissionError and detail.code === "SUBACCOUNT_KEY_REFUSED".
An alert fires at most once, and only on a crossing it observes. It records the price when it is
created or set again as its referencePrice. An alert whose condition already holds at that price
has armed: false and first waits for the price to move to the other side: a below alert created
while the price is already below fires only after the price rises above it and falls back. Prices
that arrive late, or a feed that stops updating, never fire an alert. update sets a fired alert
again.
Methods
create(input, options?)
Creates an active alert and returns it, with the price it watches and its referencePrice.
const symbolId = client.catalog.market.requireSymbolIdByPairSymbol("BTC-USDT");
const alert = await client.priceAlerts.create({
symbolId,
direction: "above",
price: "90000",
channels: ["in_app"],
idempotencyKey: crypto.randomUUID(),
});
console.log(alert.alertId, alert.priceSource, alert.referencePrice, alert.armed);index_price on a market without an index price throws PreconditionFailedError with detail.code === "PRICE_SOURCE_UNSUPPORTED"; use last_price. A market with no current price yet
throws a retryable ServiceUnavailableError (PRICE_UNAVAILABLE).
"telegram" delivers the fired alert as a direct message to the Telegram account linked to the
caller's account. Asking for it without a linked Telegram account throws PreconditionFailedError with detail.code === "TELEGRAM_NOT_LINKED", and while Telegram delivery is disabled, with detail.code === "TELEGRAM_UNAVAILABLE". Neither is retryable: link Telegram through social verification, or ask for "in_app" only. update throws them too when the alert keeps "telegram" among its channels; set channels: ["in_app"] to change such an alert.
list(input?, options?)
Returns { alerts, nextPageToken }, newest first. Pass the previous nextPageToken as pageToken with the same filters; an empty nextPageToken means there are no more.
let pageToken = "";
do {
const page = await client.priceAlerts.list({ statuses: ["active"], limit: 200, pageToken });
for (const alert of page.alerts) {
console.log(alert.alertId, alert.symbolId, alert.direction, alert.price);
}
pageToken = page.nextPageToken;
} while (pageToken !== "");update(input, options?)
Changes an alert's direction, price, or channels and returns { alert, outcome }. Omitted
fields stay as they are, and channels replaces the whole list. A new direction or price records a
new reference price. Updating a triggered alert sets it again, active with a new reference
price, even with no field given. An update that changes nothing on an active alert returns it
unchanged.
const { alert: updated, outcome } = await client.priceAlerts.update({
alertId: alert.alertId,
expectedRevision: alert.revision,
price: "95000",
});outcome says what the call did:
A replayed update never sets an alert again, however late it comes. An alert keeps at most 1,000
update keys: a new key beyond that throws PreconditionFailedError with detail.code === "ALERT_UPDATE_LIMIT_REACHED", while updates without a key and repeats of a stored
key still work.
A stale expectedRevision throws RevisionConflictError: read the alert again and retry. A
canceled alert throws PreconditionFailedError with detail.code === "ALERT_CANCELLED".
cancel(input, options?)
Cancels an active alert by alertId and returns it. Canceling a canceled alert returns it
unchanged. A triggered alert already fired, so there is nothing to cancel: it throws PreconditionFailedError with detail.code === "ALERT_TRIGGERED". Update it to set it again.
const canceled = await client.priceAlerts.cancel({ alertId: alert.alertId });The PriceAlert shape
import type { PriceAlert } from "@polyester/sdk";
interface PriceAlert {
alertId: string;
symbolId: number;
direction: "above" | "below" | "unspecified";
priceSource: "last_price" | "index_price" | "unspecified"; // the price it watches
price: string; // alert price, a decimal string
referencePrice: string; // price when created or last set again; it fires on a crossing from here
armed: boolean; // false while an alert created past its price waits for the price to move back
status: "active" | "triggered" | "canceled" | "unspecified";
channels: ("in_app" | "telegram" | "unspecified")[];
revision: string; // pass as expectedRevision to update
createdAt: number | null; // epoch ms
updatedAt: number | null; // epoch ms, the caller's last change
triggeredAt: number | null; // epoch ms, set only once the alert fired
triggeredPrice: string | null; // price that fired it; null unless triggered
}active waits for its crossing, triggered fired once and stopped, and canceled never fires
again.
Errors
Invalid input, such as a zero price or a repeated channel, throws ValidationError before the
request is sent. The service's refusals:
Related
- Notifications for the
price_alertnotices of fired alerts. - API keys for the
read-price-alertsandmanage-price-alertspermissions. - Catalog for resolving
symbolId. - Errors for the error type reference.