# API Authentication

The `/api/v1` endpoints are an API over your team's sources, logs, issues, monitors,
alerts, incidents, status page and on-call: reads, and writes for most of them. It is
separate from the [ingest endpoint](https://tailcove.dev/docs/sending-logs/http-api), which uses a per-source
ingest token instead. The whole API is described by an
[OpenAPI document](https://tailcove.dev/docs/api/openapi.json) generated from the routes themselves (see
[Reference](https://tailcove.dev/docs/api/reference)).

## Roles

A token acts as the person who made it, in one team, and is as limited as that person's [role](https://tailcove.dev/docs/team-security) is now. A **Viewer** can make tokens with read abilities only, and a
token whose owner becomes a Viewer stops writing. A **Billing** member cannot make or use tokens. If the team has an [IP allow-list](https://tailcove.dev/docs/team-security), requests from any other address are refused.

## Creating a token

Go to **Settings → API tokens** and create a
token with a name and the abilities it needs, and no more. There is one `read`
and one `write` ability per resource; a write ability does **not** include the
read of the same resource, and the form marks every ability that changes data.

| Ability              | Grants                                                                     |
| -------------------- | -------------------------------------------------------------------------- |
| `logs:read`          | `GET /sources` and `/sources/{id}`, `GET /sources/{id}/logs`               |
| `issues:read`        | `GET /sources` and `/sources/{id}`, `GET /sources/{id}/issues`             |
| `issues:write`       | resolve, reopen and ignore an issue                                        |
| `sources:read`       | `GET /sources`, `GET /sources/{id}`                                        |
| `sources:write`      | create, replace, delete a source; rotate its ingest token                  |
| `monitors:read`      | list and read monitors                                                     |
| `monitors:write`     | create, replace, delete monitors                                           |
| `alerts:read`        | list and read a source's alerts                                            |
| `alerts:write`       | create, replace, delete a source's alerts                                  |
| `incidents:read`     | list incidents, read one with its timeline                                 |
| `incidents:write`    | open, acknowledge, resolve and comment on incidents                        |
| `status-pages:read`  | read the status page and its announcements                                 |
| `status-pages:write` | post, update and delete announcements (public, and emailed to subscribers) |
| `oncall:read`        | schedules, escalation policies, who is on call now, overrides              |
| `oncall:write`       | add and remove on-call overrides                                           |
| `deploys:read`       | list a source's deploys                                                    |
| `deploys:write`      | record deploys and rollbacks (for CI)                                      |
| `sourcemaps:write`   | upload a source's source maps (for CI)                                     |

The plaintext token is shown once, at creation time. Tailcove only ever
stores a hash. If you lose it, revoke it and create a new one. A team can
hold at most 25 tokens.

The form's **For CI** preset ticks what a pipeline needs to record a deploy, wait for its
check and upload source maps: `sources:read`, `deploys:read`, `deploys:write` and
`sourcemaps:write`.

Every token is scoped to the team you were acting within when you created
it. Requests authenticated with it can only ever see that team's sources:
a source belonging to another team behaves exactly like one that doesn't
exist (`404`), never a `403`.

A token acts **as the person who created it**, never as more (a write fails where that person's role
or plan would fail it in the web app):

- If that person is removed from the team (or leaves it), the token stops
  working at once with `401`; nothing has to be revoked first.
- If that person is restricted to certain client groups, the token lists and
  opens only the sources in those groups; any other source is a `404`, exactly
  as it is in the web app.
- A token only works on `/api/v1`. The web app's own internal endpoints (the
  ones the log viewer and dashboard call) accept a browser session only and
  answer a token with `403`.

## From the command line

The [`tailcove` command line](https://tailcove.dev/docs/cli) doesn't need a token pasted into it. Run
`tailcove login`: it shows a code, you approve it in the browser at
`https://tailcove.dev/cli/activate`, and the CLI receives a token of its own, named
"Tailcove CLI on" and your machine's name, with `sources:read`, `sources:write`,
`logs:read`, `deploys:read`, `deploys:write` and `sourcemaps:write` (a Viewer gets the
three reads). It's an ordinary token in every other way: it acts as you in your current
team, counts toward the team's 25, and you revoke it in **Settings → API tokens** like any
other. The sign-in is the OAuth device authorization grant (RFC 8628), at
`POST /api/v1/cli/device` and `POST /api/v1/cli/token`; those two take no token and are
rate limited per address.

`php artisan tailcove:install` from the [Laravel package](https://tailcove.dev/docs/sending-logs/laravel-package) signs in the same
way, on the same page. It sends `"purpose": "install"` with the app's name, environment and host, so approving
makes a new Laravel source instead, and the terminal receives that source's ingest token and a `logs:read` token
for `tailcove:tail`. If you're writing your own tool, use these two endpoints too:

```bash
curl -X POST https://tailcove.dev/api/v1/cli/device -H "Content-Type: application/json" \
  -d '{"device_name": "build-runner"}'
# → device_code, user_code, verification_uri_complete, interval

curl -X POST https://tailcove.dev/api/v1/cli/token -H "Content-Type: application/json" \
  -d '{"device_code": "…"}'
# → {"error": "authorization_pending"} until someone approves, then the token, once
```

Poll no faster than `interval` seconds. An early poll gets `slow_down` and five more seconds.

## Making a request

Send the token as a bearer token:

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

A missing, invalid, or revoked token returns `401`:

```json
{ "error": { "code": "unauthenticated", "message": "Unauthenticated." } }
```

A token missing the required ability for an endpoint returns `403`:

```json
{ "error": { "code": "forbidden", "message": "Invalid ability provided." } }
```

See [Errors & Rate Limits](https://tailcove.dev/docs/api/errors-and-rate-limits) for the full
error shape and the rate limits, and [Writing](https://tailcove.dev/docs/api/writing) for how writes behave.
