# API

An app with telltale serves the API at `{api_prefix}/_telltale/`. It can also run as its own server with `telltale-api serve`, against any database that holds telltale's tables. The full contract is [spec/SPEC.md](https://github.com/databio/telltale/blob/main/spec/SPEC.md), with exact shapes in [spec/openapi.yaml](https://github.com/databio/telltale/blob/main/spec/openapi.yaml). The Python and Node packages both pass the conformance suite in `spec/conformance/`.

## Access

Send `Authorization: Bearer <key>`. The read key may call any GET; the admin key may call everything. An app's `authorize` hook can also let its own signed-in admins in. After 20 refused calls in a minute from one address, every call from that address gets 429 until the minute is up.

## Endpoints

| Endpoint | Returns |
|---|---|
| `GET /whoami` | The caller's access, `{access, via}` |
| `GET /meta` | Versions, the apps served, and each app's repo |
| `GET /health` | Buffer and store counts, including dropped records |
| `GET /summary` | Request and error totals for a window, by client type |
| `GET /queue` | Open items that need a person |
| `GET /groups`, `GET /groups/{fingerprint}` | Problem groups |
| `GET /feedback`, `GET /feedback/{id}` | Feedback notes |
| `GET /proposals`, `GET /proposals/{id}` | Proposed fixes |
| `PATCH /groups/{fingerprint}`, `/feedback/{id}`, `/proposals/{id}` | Change status, note or issue link (admin) |
| `POST /proposals` | Add a proposal, or update the one with the same `dedupe_key` (admin) |
| `GET /admin/` | The console |

List endpoints take `since`, `status`, `kind`, `limit` (default 50, at most 500) and `offset`. Errors come back as `{"detail": "..."}`.

Two routes are public:

| Route | Takes |
|---|---|
| `POST {api_prefix}/feedback` | `{text, category?, contact?, page?, entity_type?, entity_id?}`. Answers 201. |
| `POST {api_prefix}/_telltale/client` | Batches of up to 50 browser error reports. |

Both are rate limited and refuse bodies over 32 KB.

## Clients

The Python `telltale_client` library in `cli/` and `telltale-client.js` in the browser package wrap the API with the same method names. The `telltale` command is built on the Python one; see the [command-line README](https://github.com/databio/telltale/blob/main/cli/README.md).

```python
from telltale_client import Client

c = Client("https://myapp.example.org/api/_telltale", key)
for item in c.queue():
    print(item["type"], item["title"])
```

## Records

| Record | Holds |
|---|---|
| event | One server error, 4xx, dead end, browser error or slow request |
| rollup | Request counts per minute, by route, status class, surface and client type |
| group | All events with one fingerprint: first and last seen, count, client types |
| feedback | One note, as scrubbed text |
| proposal | A drafted fix with its evidence and a draft GitHub issue |

A group's status is one of `new`, `proposed`, `issued`, `resolved` or `ignored`. A resolved group that gets a new event goes back to `new` and is marked as a regression. A proposal's status is one of `new`, `accepted`, `issued`, `dismissed` or `duplicate`. Marking a proposal `issued` marks the groups and feedback it cites as `issued` too.
