# Query your logs from scripts with the API

The read API exposes the same search as the log viewer, so anything you can find in the UI you can also fetch from a script: a nightly error report, a CLI for your team, or a check in your deploy pipeline.

## 1. Create an API token

Open your user menu, go to **Settings → API tokens**, and create a token with the `logs:read` ability. Add `issues:read` too if you want to list grouped errors.

The token is shown once. It is scoped to the team you were working in, and it reads as you: if you leave the team, it stops working.

## 2. Find your source id

```bash
curl https://tailcove.dev/api/v1/sources \
  -H "Authorization: Bearer YOUR_API_TOKEN"
```

Each source in the response has an `id`, a public id such as `src_01jbx5q8k7m3n2p4r6s8t0v1w3`.

## 3. Search a time range

```bash
curl -G https://tailcove.dev/api/v1/sources/src_01jbx5q8k7m3n2p4r6s8t0v1w3/logs \
  -H "Authorization: Bearer YOUR_API_TOKEN" \
  --data-urlencode "q=level:error" \
  --data-urlencode "from=2026-10-19T00:00:00Z" \
  --data-urlencode "to=2026-10-19T23:59:59Z" \
  --data-urlencode "limit=500"
```

`q` uses the same [query language](https://tailcove.dev/docs/query-language) as the search bar. Without `from`, the API searches the hour before `to`. A single request can span at most 31 days.

## 4. Page through results

Responses include `meta.next_cursor`. Pass it back as `cursor`, with the same `q`, `from` and `to`, until it is `null`. In Python:

```python
import os

import requests

API = "https://tailcove.dev/api/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['TAILCOVE_API_TOKEN']}"}


def errors(source_id, start, end):
    params = {"q": "level:error", "from": start, "to": end, "limit": 500}

    while True:
        response = requests.get(f"{API}/sources/{source_id}/logs", headers=HEADERS, params=params)
        response.raise_for_status()
        body = response.json()

        yield from body["data"]

        if not body["meta"]["next_cursor"]:
            break

        params["cursor"] = body["meta"]["next_cursor"]


for line in errors("src_01jbx5q8k7m3n2p4r6s8t0v1w3", "2026-10-19T00:00:00Z", "2026-10-19T23:59:59Z"):
    print(line["ts"], line["host"], line["message"])
```

Treat cursors as opaque: never build or edit one.

## 5. Respect the rate limit

Each token can make 60 requests per minute. With `limit=500` that is plenty for reports; if you hit `429`, wait and retry rather than opening more tokens.

## Ideas

- A daily summary of error counts per host, posted to your team chat.
- A deploy-pipeline step that fails if new errors appear in the minutes after a release. The [deploy check](https://tailcove.dev/guides/fail-deploys-when-errors-spike) does this for you: `tailcove deploy --wait` exits `1` when a release made errors worse.
- A quick terminal command for the queries you run most. The [`tailcove` command line tool](https://tailcove.dev/guides/tail-logs-from-the-terminal) wraps this endpoint: `tailcove search` pages through it, and `--json` hands each line to `jq`.

## Next steps

- Endpoint details in the [logs API reference](https://tailcove.dev/docs/api/logs).
- Error codes in [errors and rate limits](https://tailcove.dev/docs/api/errors-and-rate-limits).
