# Base keys: make one, limit it, replace it, end it

You are an AI agent. This page is one of Base's guides for agents (https://createbases.com/llms.txt lists them all). Follow it as written. Where it says stop, stop and tell the person what it says.

A name in code such as `list_bases` is a tool of the Base connector. In a hosted chat it is one of your own tools. From a shell it runs as a command, with a key in `BASE_KEY` or after `npx -y base-knowledge login`: `npx -y base-knowledge call <tool> '<json>'`. `npx -y base-knowledge tools <tool>` prints a tool's arguments: read them before a first call, and never guess one.

A key is how a program signs in to Base when no person is at a browser: a sandbox, a server, a scheduled job, an application's backend. It starts with `bk_`, is sent as `Authorization: Bearer <key>`, and works on the connector (https://createbases.com/api/mcp), the Base API (https://createbases.com/api/v1) and the `base-knowledge` command (`BASE_KEY`).

## What a key is

- **A key acts as the person who approved it.** It reaches what they can reach and no more, with their role on each Base, and spends against their workspace's credits and limits.
- **It can be narrower than its person, never wider.** It has one of three access levels, and may be limited to named Bases. A Base outside that list answers as if it did not exist.
- **It is shown once**, in the reply that makes it. Base keeps only a hash. A key that is lost cannot be shown again: make another and end the lost one.
- **It expires.** 30, 90, 365 days are the terms; 90 is the default.
- **It can be ended at any moment**, and every request checks: a revoked or expired key answers `401` on its next call.

| Access | It can |
| --- | --- |
| `full` | read the Bases in the library and what they know; search; read documents, tables and the entries of structures; add documents and versions; run an action on an entry; ask for a structure to be read again; read comments, tasks and threads; comment, open and work threads, in the person's name; follow a Base someone shared; create and publish Bases, change their settings, declare a structure, upgrade a Base's format |
| `contribute` | read the Bases in the library and what they know; search; read documents, tables and the entries of structures; read comments, tasks and threads; comment, open and work threads, in the person's name; follow a Base someone shared |
| `read` | read the Bases in the library and what they know; search; read documents, tables and the entries of structures; read comments, tasks and threads |

Ask for the least that does the job. An application that only reads gets `read`. `full` is for setting things up, and can do everything its person can.

## Getting one

Take the first that fits. **Do not ask the person to paste a key into the conversation when you can ask for one yourself**: a pasted key sits in the transcript.

1. **Ask for one, and let the person approve it** (the device sign-in). Nothing is pasted, and it works from any shell or sandbox that can reach https://createbases.com:

   ```bash
   curl -s -X POST https://createbases.com/api/oauth/device -H 'Content-Type: application/json' \
     -d '{"name":"<what will use it>","access":"read","expiry_days":90}'
   ```

   The reply holds a `message` with a link and a short code. **Write that message to the person yourself, before your next command**: nothing goes on until they open the link, sign in, and press Allow. Then ask every 5 seconds, saving the reply to a file, because the key is in it once:

   ```bash
   curl -s -X POST https://createbases.com/api/oauth/device/token -H 'Content-Type: application/json' \
     -d '{"device_code":"<device_code from the first reply>"}' -o base-key-reply.json
   ```

   `authorization_pending`: keep asking. `slow_down`: wait ten seconds. `expired_token` (after 15 minutes): start again. `access_denied`: the person said no; stop. When it works the file holds `{"key", "row": {"id", "access", "base_ids", "expires_at"}}`. The person may lower the access when they press Allow, so read `row.access`. A key made this way reaches every Base its person can: it cannot be limited to one here.
2. **Mint one from a key or a connection you already hold**, for another program. It can be limited to Bases, never has more than its maker, lasts at most 90 days, and stops the moment its maker is revoked or expires:

   ```bash
   curl -s -X POST https://createbases.com/api/oauth/keys -H "Authorization: Bearer $BASE_KEY" -H 'Content-Type: application/json' \
     -d '{"name":"<what will use it>","access":"read","base_ids":["<base id>"],"expiry_days":90}' -o base-key-reply.json
   ```
3. **The person makes one** at https://createbases.com/settings/connect, under Keys, where they choose its access, its term (up to 365 days) and the Bases it is limited to. This is the way to a key that is both limited to a Base and long-lived. Ask them to put it straight into the program's secret store, not into this conversation.

**Check:** `curl -s https://createbases.com/api/oauth/keys -H "Authorization: Bearer <the key>"` answers 200 (it lists the keys that key has minted, usually none). `401` means the key is wrong, expired or revoked.

## Where a key lives

- In the program's secret store or server environment, as `BASE_KEY`. Read it from there at run time.
- Never in code a browser loads, a URL, a log line, a commit, or a message to anyone. Do not print it: read it from the file the reply was saved to, put it where it belongs, then delete the file.
- A browser application calls its own server, and the server calls Base.
- A person may hold at most 25 live keys. One key may make 1,200 requests in 10 minutes to the Base API.

## Before it expires

Two weeks before a key that is still in use expires, Base emails its owner a line to give an agent. That agent replaces the key like for like (the same access, the same Bases and workspaces, the same term), and only the key's owner can approve it. Whatever would have stopped the old key early still stops the new one: if another key or a connection made it, revoking that ends both, and a key already stopped that way cannot be replaced. The steps are "Replace a key that is about to expire" at https://createbases.com/api/v1. A key nothing has used for a month gets no email.

When a request answers `401`, do not retry it and do not look for another key: tell the person the key has expired or was revoked, and that a new one is needed.

## Ending one

- **The person** revokes any of their keys at https://createbases.com/settings/connect, under Keys.
- **A key ends itself**: `curl -s -X DELETE "https://createbases.com/api/oauth/keys?id=self" -H "Authorization: Bearer <the key>"`. It stops at once, and so does every key it minted.
- **A key ends one it minted**: the same request with `id=<that key's id>`.

End a key the moment it is no longer needed, or you think anyone else has seen it. Tell the person when you do, and when you could not.
