# Price rules `price`

- Section: Webhooks (TTF API reference)
- Source: https://ttf.bot/developers/docs/#webhook-price
- The whole reference as one text: https://ttf.bot/developers/docs/llms-full.txt

`POST` to your endpoint, with the header `X-TTF-Event: price`.

Rule types: `price_cross`, `pct_move`

Sent when a `price_cross`, `pct_move` rule fired on a `price` event.

## Delivery rules

TTF sends an HTTPS POST with a JSON body to your endpoint when a rule fires (and when you press Test in the dashboard). Answer any 2xx within 10 seconds (queue the work on your side); anything else, a timeout or a redirect is retried after 1 min, 5 min, 30 min, 2 h and 12 h. Delivery is AT LEAST ONCE: dedupe on X-TTF-Delivery (= body.id). Deliveries are not ordered: order by created_ms / event.ts. Verify X-TTF-Signature over the RAW body before you trust it. Deliveries come from changing proxy addresses: never allowlist IPs, verify the signature.

## Headers

| Header | Type | Meaning | Example |
| --- | --- | --- | --- |
| `X-TTF-Event` | string | The event type of the body's `event`: price, trending, wallet, signal or ping. | `price` |
| `X-TTF-Event-Id` | string | The event id (a ULID). One event can fire several of your rules: each is its own delivery. | `01JDZ8Q7X3M4N5P6Q7R8S9T0PA` |
| `X-TTF-Delivery` | string | The delivery id (a ULID) = body.id, THE SAME on every retry and re-send: drop duplicates on it. | `01JDR4Q7Z2N8M5K3X9V6B4C2HA` |
| `X-TTF-Attempt` | integer | 1 for the first try, then 2, 3, ... (at most 6 attempts over about 15 hours). | `1` |
| `X-TTF-Timestamp` | integer | Unix SECONDS when this attempt was signed (every retry is signed afresh). Refuse it when more than 300 s away from your clock (replay window). | `1795000000` |
| `X-TTF-Signature` | string | v1=\<hex>: HMAC-SHA256(key = your endpoint secret "whsec_...", message = X-TTF-Timestamp + "." + the raw body bytes), lower-case hex. During the 24 h after a secret rotation: "v1=\<new>,v1=\<old>". Compare in constant time; accept if any value matches. | `v1=0000000000000000000000000000000000000000000000000000000000000000` |
| `User-Agent` | string | Always TTF-Webhooks/1.0. | `TTF-Webhooks/1.0` |

## Signature

| Field | Value |
| --- | --- |
| algorithm | HMAC-SHA256 |
| format | v1=\<64 lower-case hex>[,v1=\<hex>] |
| header | X-TTF-Signature |
| rotation | for 24 h after a secret rotation the header carries two values (new secret first): accept if any matches |
| secret | the endpoint secret, the whole whsec_... string as UTF-8 |
| signed payload | \<X-TTF-Timestamp>.\<raw request body bytes> |
| timestamp header | X-TTF-Timestamp |
| tolerance s | 300 |

Copy-paste verification code in Python and Node.js: [Verify the signature](https://ttf.bot/developers/docs/md/webhook-signatures.md).

## Body

One body per fired rule. `always` = in every body; other fields are left out when unknown. Text inside events comes from third parties: escape it before you show it.

### `WebhookPriceCross`

A `price_cross` rule fired.

- `created_ms` (integer, always): When the rule fired. Unix time in milliseconds.
- `detail` (object, always): Why a price_cross rule fired.
  - `change_pct` (number, always): How far the price is past the threshold. Percent (5.2 = 5.2 %).
  - `side` (string, always): above = the price rose to / past it, below = it fell to / past it. One of: `above`, `below`.
  - `threshold_usd` (string, always): Your threshold price in US dollars. Decimal string (never a float, no exponent).
  - `price_usd` (string): The price that crossed it, in US dollars. Decimal string (never a float, no exponent).
- `event` (object, always): A price update of a token your rule watches.
  - `chain` (string, always): Chain id. Absent for a signal about no chain.
  - `data` (object, always): A price update of a token your rule watches. The event data (the keys of this type; unknown = absent).
    - `liquidity_usd` (integer): Liquidity of the pool the price comes from. Whole US dollars (integer).
    - `name` (string): Token name (third-party text: escape it).
    - `pair` (string): The pair (pool) the price comes from.
    - `price_usd` (string): Price of one token in US dollars. Absent only for an absurd price (more than 64 characters). Decimal string (never a float, no exponent).
    - `symbol` (string): Ticker (third-party text: escape it).
  - `id` (string, always): Event id: a ULID (26 characters, sorts by time). One event can fire several rules.
  - `token` (string, always): Token address (one canonical form per chain: EVM lower-case 0x + 40 hex). Absent for a signal about no token.
  - `ts` (integer, always): When the event happened. Unix time in milliseconds.
  - `type` (string, always): The event type.
  - `v` (integer, always): Envelope version (1).
- `id` (string, always): Delivery id (a ULID) = the X-TTF-Delivery header.
- `reason` (string, always): Why it fired. One of: `crossed`, `already_past`.
- `rule` (object, always): The rule that fired.
  - `id` (string, always): Rule id (whr_ + 24 hex).
  - `kind` (string, always): The rule type.
- `type` (string, always): rule.fired

### `WebhookPctMove`

A `pct_move` rule fired.

- `created_ms` (integer, always): When the rule fired. Unix time in milliseconds.
- `detail` (object, always): Why a pct_move rule fired.
  - `base_ms` (integer, always): When that start price was seen. Unix time in milliseconds.
  - `change_pct` (number, always): The move seen over the window. Percent (5.2 = 5.2 %).
  - `threshold_pct` (number, always): Your move threshold (negative = a drop). Percent (5.2 = 5.2 %).
  - `window` (string, always): Your window. One of: `1m`, `5m`, `15m`, `1h`, `4h`, `24h`.
  - `base_price_usd` (string): The price at the start of the window, in US dollars. Decimal string (never a float, no exponent).
  - `price_usd` (string): The price now, in US dollars. Decimal string (never a float, no exponent).
- `event` (object, always): A price update of a token your rule watches.
  - `chain` (string, always): Chain id. Absent for a signal about no chain.
  - `data` (object, always): A price update of a token your rule watches. The event data (the keys of this type; unknown = absent).
    - `liquidity_usd` (integer): Liquidity of the pool the price comes from. Whole US dollars (integer).
    - `name` (string): Token name (third-party text: escape it).
    - `pair` (string): The pair (pool) the price comes from.
    - `price_usd` (string): Price of one token in US dollars. Absent only for an absurd price (more than 64 characters). Decimal string (never a float, no exponent).
    - `symbol` (string): Ticker (third-party text: escape it).
  - `id` (string, always): Event id: a ULID (26 characters, sorts by time). One event can fire several rules.
  - `token` (string, always): Token address (one canonical form per chain: EVM lower-case 0x + 40 hex). Absent for a signal about no token.
  - `ts` (integer, always): When the event happened. Unix time in milliseconds.
  - `type` (string, always): The event type.
  - `v` (integer, always): Envelope version (1).
- `id` (string, always): Delivery id (a ULID) = the X-TTF-Delivery header.
- `reason` (string, always): Why it fired. One of: `moved`.
- `rule` (object, always): The rule that fired.
  - `id` (string, always): Rule id (whr_ + 24 hex).
  - `kind` (string, always): The rule type.
- `type` (string, always): rule.fired

## Examples

`pct_move`: pct_move delivery

```json
{
  "created_ms": 1795000000000,
  "detail": {
    "base_ms": 1794996500000,
    "base_price_usd": "0.0000086",
    "change_pct": 6.98,
    "price_usd": "0.0000092",
    "threshold_pct": 5,
    "window": "1h"
  },
  "event": {
    "chain": "eth",
    "data": {
      "price_usd": "0.0000092"
    },
    "id": "01JDZ8Q7X3M4N5P6Q7R8S9T0PA",
    "token": "0x6982508145454ce325ddbe47a25d4ec3d2311933",
    "ts": 1795000000000,
    "type": "price",
    "v": 1
  },
  "id": "01JDR4Q7Z2N8M5K3X9V6B4C2HA",
  "reason": "moved",
  "rule": {
    "id": "whr_6f1c0a9e2b7d4c3a1e0f9b8d",
    "kind": "pct_move"
  },
  "type": "rule.fired"
}
```

`price_cross`: price_cross delivery

```json
{
  "created_ms": 1795000000000,
  "detail": {
    "change_pct": 2.22,
    "price_usd": "0.0000092",
    "side": "above",
    "threshold_usd": "0.000009"
  },
  "event": {
    "chain": "eth",
    "data": {
      "price_usd": "0.0000092"
    },
    "id": "01JDZ8Q7X3M4N5P6Q7R8S9T0PA",
    "token": "0x6982508145454ce325ddbe47a25d4ec3d2311933",
    "ts": 1795000000000,
    "type": "price",
    "v": 1
  },
  "id": "01JDR4Q7Z2N8M5K3X9V6B4C2HA",
  "reason": "crossed",
  "rule": {
    "id": "whr_6f1c0a9e2b7d4c3a1e0f9b8d",
    "kind": "price_cross"
  },
  "type": "rule.fired"
}
```

## Your answer

- `2XX`: Delivered: any 2xx. The first 1 KB of your answer shows in the delivery log.
- `default`: Any other answer (or a timeout, a TLS / connection failure, a redirect) is a failed attempt and is retried.

## JSON Schema

Validate bodies in your own code with the published schemas (draft 2020-12):

- [events/price.json](https://ttf.bot/developers/docs/schemas/events/price.json)
- [webhook.json](https://ttf.bot/developers/docs/schemas/webhook.json)
