# Authentication

Every call is made with a bearer token in the `Authorization` header. A token always belongs to one person in one organisation, and the call can do what that person can do, narrowed by the token's [scopes](/guides/scopes/).

|            | Personal API token                            | OAuth app                                                      |
| ---------- | --------------------------------------------- | -------------------------------------------------------------- |
| For        | Your own scripts                              | A product other people connect to their account                |
| Made by    | The person, in Personal settings › API tokens | The app, through the person's consent                          |
| Looks like | `pqt_…`                                       | access `pqa_…`, refresh `pqr_…`                                |
| Lasts      | 30, 90 (default) or 365 days                  | access 1 hour; refresh rotates, ends after 90 days without use |
| Guide      | This page                                     | [OAuth 2.0 apps](/guides/oauth/)                               |

## Sending the token

```sh
curl "$PROJQO_API/me" -H "Authorization: Bearer $PROJQO_TOKEN"
```

- Send it in the header only. A token in the address would end up in logs and browser history.
- The API takes tokens only, never projqo's sign-in cookie; projqo's own pages, the other way round, never take a token.

## Personal API tokens

A person makes a token in **Personal settings › API tokens**, for one organisation:

- they give it a **name**, tick its **scopes** and pick when it **expires**: 30, 90 (the default) or 365 days. No token lasts longer than a year;
- the token is shown **once**. projqo keeps only a hash of it, its last four characters, and when and from which address it was last used;
- it starts with `pqt_`, so secret scanners can recognise a leaked one.

The person is emailed when a token is made, and again 7 days before it expires. They can revoke it at any time, and it stops working at once.

### Who may make tokens

- Every member of the organisation except guests, by default. An organisation admin can limit tokens to admins, or switch API access off, in **Organisation settings › API**.
- Guests can't make tokens or connect apps.
- A project viewer can make a token; it reads what the viewer reads, and its changes are refused as they are in the app.

### What admins see

Organisation owners and admins see every token in their organisation (its name, its person, its scopes, when it was last used) and can revoke any of them.

### Keeping a token safe

- Keep it in an environment variable or your system's secret store, never in code you commit.
- Tick only the scopes the script needs.
- Make one token per script, so you can revoke one without stopping the others.
- If a token leaks, revoke it in Personal settings › API tokens and make a new one.

## When a token stops working

A call without a token, or with a wrong, expired or revoked one, answers `401` with the code `invalid_token` and a `WWW-Authenticate` header:

```json
{
  "error": {
    "code": "invalid_token",
    "message": "…",
    "status": 401,
    "request_id": "…"
  }
}
```

A token also stops working when its person leaves the organisation, and loses the API when they become a guest. This is checked at every call.

A token without the scope a call needs answers `403` with the code `insufficient_scope`. The message names the scope it needs, and so does the `WWW-Authenticate` header, as OAuth's bearer tokens standard (RFC 6750) writes it:

```text
WWW-Authenticate: Bearer realm="projqo", error="insufficient_scope", scope="tasks:write"
```

See [errors](/guides/errors/).
