> ## Agent Instructions
>
> Base URL: https://api.anysite.io
> Authentication: send the `access-token` header. Do NOT use `Authorization: Bearer`.
> Full endpoint catalog: https://app.anysite.io/docs
# Monitors API and webhook

Get each run’s new results at your own address by webhook, check its signature, and read monitors with an API key or from an MCP client.

Goal: get what your monitors find into your own system, pushed to your address after each run or read
with an API key whenever you want. Neither costs anything.

> ACTION FOR THE HUMAN: open https://app.anysite.io/monitors (Open Monitors).

## Steps

1. **Set the webhook.** Open the monitor, click **Edit**, go to the last step and fill in **Webhook
   URL** with your receiver’s `https://` address. Click **Save changes**. Every monitor type takes it,
   **People under post links** included. Saving only the webhook charges nothing.
2. **Copy the secret.** On the monitor’s **Settings** tab, **Webhook secret** shows the start of the
   secret; click **Copy** and store it in your receiver.
3. **Check every call’s signature** (below) before you use its body.
4. **Drop repeats by `id`.** Every post, change, person and item carries its `id`. The same item can come
   again, by the webhook and by the API; keep the first one.
5. **Read what you missed with the API** (below). The webhook makes one attempt per run and is not
   sent again; a run whose call failed reads **Webhook failed · …** in **Run history**, and its results
   are in the news feed.
6. **Or ask an MCP client.** The Anysite MCP server lists your monitors and reads a monitor’s news
   after a cursor: [Monitor tools](/docs/mcp/overview).

To stop the calls, clear **Webhook URL** and save. **Regenerate secret** on **Settings** makes a new
secret; the old one stops working at once, so update your receiver.

## The webhook call

After each run that found something, one `POST` goes to your address, beside the results e-mail:

| Header | Value |
|---|---|
| `Content-Type` | `application/json` |
| `User-Agent` | `Anysite-Webhook/1.0` |
| `X-Webhook-Signature` | `sha256=` and the hex HMAC-SHA256 of the raw body, keyed with the secret |

```json
{
  "monitor_id": "<monitor id>",
  "runs": [
    {
      "id": "<run id>",
      "status": "done",
      "result_count": 2,
      "person_count": 1,
      "started_at": 1764000000,
      "finished_at": 1764000300,
      "posts": [],
      "changes": [],
      "people": [],
      "items": []
    }
  ],
  "has_more": false,
  "next_cursor": "<cursor>"
}
```

`posts`, `changes`, `people` and `items` hold the same items as the monitor’s tabs. `items` is a
Custom flow’s, as its **Items** tab shows them, and is empty on every other type: `kind` is `item` for
a new item or `item_change` for a watched field’s change, `fields` the fields you chose to show (each
with its `step`, `path` and `value`), and `change`, on an `item_change`, the field’s `path`, `old` and
`new` value and the `threshold` it crossed (`null` when the watch has none); `null` on a new item. A
Custom flow’s run:

```json
{
  "id": "<run id>",
  "status": "done",
  "result_count": 2,
  "person_count": 0,
  "started_at": 1764000000,
  "finished_at": 1764000300,
  "posts": [],
  "changes": [],
  "people": [],
  "items": [
    {
      "id": "<item id>",
      "kind": "item",
      "item_url": "https://jp.mercari.com/item/m2",
      "title": "Switch 2",
      "fields": [
        { "step": 2, "path": "name", "value": "Switch 2" },
        { "step": 2, "path": "price", "value": 30002 }
      ],
      "change": null,
      "is_new": true,
      "found_at": 1764000120
    },
    {
      "id": "<item id>",
      "kind": "item_change",
      "item_url": "https://jp.mercari.com/item/m1",
      "title": "Switch 1",
      "fields": [
        { "step": 2, "path": "name", "value": "Switch 1" },
        { "step": 2, "path": "price", "value": 27500 }
      ],
      "change": { "path": "price", "old": 30001, "new": 27500, "threshold": null },
      "is_new": true,
      "found_at": 1764000180
    }
  ]
}
```

A call carries at most 100
items of its run; when `has_more` is `true`, read the rest from the news feed with
`after=<next_cursor>`. Times are Unix seconds. A redirect is not followed, and a call with no answer in
10 seconds counts as failed.

Check the signature over the bytes you received, never over re-encoded JSON:

```python
import hashlib, hmac

def is_signed(secret: str, raw_body: bytes, header: str) -> bool:
    expected = "sha256=" + hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, header)
```

## Reading monitors with an API key

Send your API key in the `access-token` header, as for every Anysite API call. Results are kept for 30
days.

List your monitors with their ids:

```bash
curl https://api.anysite.io/monitors \
  -H "access-token: YOUR_API_KEY"
```

> ACTION FOR THE HUMAN: copy your API key at https://app.anysite.io/billing — an agent cannot obtain it.

Read a monitor’s news: its finished runs that found something, oldest first, each with its new posts,
changes and people, or a Custom flow’s items.

```bash
curl "https://api.anysite.io/monitors/<monitor id>/news?limit=100" \
  -H "access-token: YOUR_API_KEY"
```

> ACTION FOR THE HUMAN: copy your API key at https://app.anysite.io/billing — an agent cannot obtain it.

The answer has the shape of the webhook body without `monitor_id`. Store `next_cursor` and pass it
back unchanged as `after=<next_cursor>` on the next read to get only what is new; without `after` the
feed starts from the oldest results kept. While `has_more` is `true`, read again at once. `limit` is 1
to 100, 100 by default.

## How to verify

- After **Save changes**, **Settings** shows **Webhook URL** with your address and **Webhook secret**.
- After the next run that finds something, **Run history** reads **Webhook delivered** under it.

## Troubleshooting

- **The address must start with https://** or **Use a public address: not localhost, a private
  network or an internal host name.** under **Webhook URL**: nothing was saved. Use the public
  `https://` address of your receiver.
- **Webhook failed · …** in **Run history**: the reason names what went wrong, such as **no answer in
  time** or **answered with a server error (5xx)**. Fix the receiver and read that run from the news
  feed.
- `401` with `{"detail": "Token is empty"}`: the call carried no `access-token` header, or it went to a
  route a key cannot read. A key reads `GET /monitors`, `GET /monitors/<monitor id>`, its results
  (`posts`, `changes`, `people`, `companies`, `items`), `news` and `export`; Run history, settings, pause, resume,
  delete and the other routes answer only to the signed-in dashboard.
- `403` with `{"detail": "Monitors are not available for guest keys"}`: guest keys cannot read monitors.
- `404` with `{"detail": "Monitor not found"}`: the id is not one of your monitors; take it from
  `GET /monitors`.
- `422` with `{"detail": "cursor_invalid"}`: pass a `next_cursor` from an earlier answer unchanged, or
  leave `after` out.
