# API tokens and limits

> Make a token for a script or CI, use it with curl, plinto and git, and what happens when it asks too fast.

`plinto login` gives your terminal a key for a day, from your SSH key. It asks for what `plinto` and the editors do alone: builds, runs, dry runs, reviews, imports and `plinto reproduce`, whoever you are. A script or a CI job needs a key that lasts, without an SSH key, and asks for more: an API token. You make it in plinto code, under **API tokens** in your user menu.

## Making one {#making}

A token has:

- **a name**, what it is for, so you know which one to revoke;
- **one organization of yours**: it acts in that one alone, and what it asks counts against that organization's API limits;
- **its access**: *Read* (GET and HEAD, and git fetches), *Read and write* (what you can do as a member), *Admin*, or *Specific permissions*, the last two offered when you are the organization's admin, and working while you still are;
- **its expiry**: 30, 90 or 365 days. None lasts for good.

A token with specific permissions does what you tick alone, as the organization's admin, and gets `403` for anything else, so a script that needs one thing can't do more if it leaks. There is one so far:

| Permission | Lets it |
|---|---|
| **Register hosts** | make setup lines for new servers: see [Hosts, From a script](../concepts/hosts.md#script) |

plinto shows the token once, as you make it: `plinto_` and 40 letters and digits. Copy it into your CI's secrets. If you lose it, revoke it and make another. Making one asks for your second factor when you haven't given it in the last few minutes, as adding an SSH key does.

## Using it {#using}

It goes where `plinto login`'s key goes, and asks for whatever its access lets it.

With curl, or any HTTP client, as the `X-Plinto-Key` header:

```sh
$ curl -H "X-Plinto-Key: $PLINTO_TOKEN" https://repository.plinto.build/acme/version-groups/main
```

With `plinto`, in `PLINTO_ACCESS_KEY`:

```sh
$ PLINTO_ACCESS_KEY=$PLINTO_TOKEN plinto build -a
```

With git over HTTPS, as the password (any user name):

```sh
$ git clone https://x:$PLINTO_TOKEN@build-service.plinto.build/git/acme/Billing.git
```

`plinto` never renews a token: when one is refused, it says why and to make a new one.

## Revoking {#revoking}

**Revoke** on the API tokens page stops a token from its next request on. Each token's row says when and from where it was last used.

Tokens stop on their own when:

- they expire;
- you leave the organization, or, for an admin token, stop being its admin;
- plinto's admins sign you out everywhere. Tokens you make after still work.

Making and revoking a token show in your Recent activity, on the Two-factor sign-in page.

## API limits {#limits}

Programs, tokens and `plinto login`'s keys alike, may make so many requests a second. The apps in your browser never count. Each request takes one from three buckets of its organization's:

| Bucket | Default | |
|---|---|---|
| The organization | 10 a second | everything its keys and tokens ask |
| An API site | 5 a second | `repository.`, `build-service.` and `hosts.plinto.build`, each |
| A group of endpoints | 3 a second | such as downloads, metadata, builds or git |

Each bucket also holds a minute's worth at once, so a cold build on a fresh machine gets through. Over a limit, the answer is `429 Too Many Requests`, with `Retry-After` (the seconds until there is room again) and `X-Plinto-Limited` (which bucket is empty):

```text
HTTP/1.1 429 Too Many Requests
Retry-After: 1
X-Plinto-Limited: group

{"error": "acme's API limit for downloads: 3 a second, a burst of 180; try again in 1 s", …}
```

`plinto` and the editors wait out such a 429 and send again, for up to two minutes, saying so once. A script should do the same: wait `Retry-After` seconds, and a little more. plinto's admins can give an organization higher limits.
