# Personal API keys for scripts and bots

How to make a personal API key for your scripts, bots and the SDK: the scopes it may use, how long it works, its limits, child keys, and turning it off.

A **personal API key** is a token you make for your own code: a backup script, a Discord bot, a small site that shows your uploads. It acts as you, but it can do only what you chose when you made it, and you turn it off in one press. It starts with `hmk_`, like every hematite token says what it is ([authentication](https://hematite.gg/docs/developers/authentication)).

Which token for what:

- **A screenshot tool** that only uploads: an upload token, from [Uploaders](https://hematite.gg/settings/uploaders).
- **Your own script or bot:** an API key, with only the scopes it needs.
- **An app other people sign in to:** [Sign in with hematite](https://hematite.gg/docs/developers/oauth), so each person allows it themselves.

## Make one

1. **Open [Settings → API keys](https://hematite.gg/settings/api-keys)** and press **Make key**.
2. **Name it** for what it's for ("Backup script"): the list and your [Audit log](https://hematite.gg/audit-logs) say it by that name.
3. **Choose what it may do:** a box under Read or Write for each kind of thing. A script that only reads gets a key that only reads.
4. **Choose how long it works:** 7 days, 30 days, 90 days or a year. Without a choice it's 90 days; the most is 366.
5. **Add limits if you want them** (below), then press **Make key**. With [two-step sign-in](https://hematite.gg/docs/account/two-step-sign-in) on, it asks for your second step first, unless you gave it in the last ten minutes.
6. **Copy it now.** It's shown only this once: hematite keeps just a keyed hash of its secret. If you lose it, make a new one.

An account has at most 10 API keys, each with at most 10 child keys.

## Use it

Send it in the `Authorization` header, as every token is sent:

```bash
curl https://api.hematite.gg/v1/users/@me \
  -H "Authorization: $HEMATITE_KEY"
```

Or give it to [the SDK](https://hematite.gg/docs/developers/sdk), on a server or in a script, where no realtime connection is needed:

```ts title="backup.ts"
import { Hematite } from "@hematite/sdk";

const hematite = new Hematite(process.env.HEMATITE_KEY, { websocket: false });
const me = await hematite.users.fetchMe();
```

Keep it where your code reads secrets (an environment variable, a secrets manager), never in a repository: anyone who has it can do what it may until it ends or you turn it off.

## What it may do

Its scopes are the ones an app can ask for, one Read and one Write per kind of thing, and **Child keys** (`keys:write`), which only a key can have:

| Scope | Lets an app |
|---|---|
| `images:read` | See your images and their details, private ones too |
| `images:write` | Upload images, and edit and delete yours |
| `folders:read` | See your folders and what's in them, private ones too |
| `folders:write` | Create folders, and edit and delete yours |
| `shares:read` | See your share links |
| `shares:write` | Change your share links |
| `comments:read` | Read comments, yours and on your images and folders |
| `comments:write` | Comment as you, and edit and delete your comments |
| `likes:read` | See what you've liked |
| `likes:write` | Like and unlike things as you |
| `notifications:read` | Read your notifications |
| `notifications:write` | Mark your notifications read and delete them |
| `users:read` | See your profile, upload settings and storage, and look up other users |
| `users:write` | Change your username, bio, picture and upload settings (never your email, password or privacy settings) |
| `email` | See your email address |

| Scope        | What it allows                                                       |
| ------------ | -------------------------------------------------------------------- |
| `keys:write` | Cut child keys from this key, never wider than it, and turn them off |

A key is held to the same rules as an app's token, endpoint by endpoint: the reference says the scope each one needs under **App scope**. Whatever its scopes, no key can change your password, email or privacy settings, or reach your sessions, upload tokens, billing, domains or the account's logs, nor make API keys: those stay yours, on hematite. Your email address is in your profile's answers only with the `email` scope.

A request its scopes don't cover answers `403` the way an app's does, with the code `KEY_INSUFFICIENT_SCOPE` and the scope it needs in `data.scope`; an endpoint no key may call answers `KEY_ROUTE_DENIED`, and a field no key may send `KEY_FIELD_DENIED` ([errors](https://hematite.gg/docs/developers/errors)).

## Limits

Under **Limits** as you make it, each one optional, each only ever narrowing it:

- **Only from these addresses:** up to 16 single addresses or ranges (`198.51.100.0/24`, `2001:db8::/32`). From anywhere else the key is refused as a wrong key is: `401`.
- **Requests a minute:** at most so many, across every endpoint, 1 to 120. Past it, `429` with `KEY_RATE_LIMITED` and a `Retry-After` header.
- **Uses:** each request takes one, and the key ends by itself with its last.
- **Uploads go into** a folder of yours, and **uploads are** Public, Unlisted or Private: for a key that uploads (`images:write`), every upload goes there and gets that, whatever the upload says. If the folder is deleted, uploads are refused with `409` `KEY_FOLDER_GONE` until you make a new key.

## Child keys

A **child key** is cut from one of your keys for one job (a bot per server, a script for a friend), and it's never more than its parent:

- its scopes are among its parent's, and never `keys:write`: children are one level deep;
- it ends no later than its parent, and the moment its parent ends;
- its addresses are inside its parent's, its rate no higher, its uses no more than its parent has left, and its uploads go where its parent's go;
- what it doesn't name, it takes from its parent;
- each of its requests counts toward its parent's rate and uses too, so children never add to what a key can do.

Cut one in Settings → API keys, from a key's **⋯** → **Cut a child key**. A key with `keys:write` can cut children from itself, and turn them off, with [`POST /keys/:publicId/children`](https://hematite.gg/docs/developers/reference/keys#post-keys-publicid-children) and [`DELETE /keys/:publicId`](https://hematite.gg/docs/developers/reference/keys#delete-keys-publicid), or the SDK's `hematite.keys.cutChild()`:

```ts title="bot.ts"
const key = process.env.HEMATITE_KEY ?? "";
const hematite = new Hematite(key, { websocket: false });
// A key's public id is in its token: the 16 characters after "hmk_live_".
const child = await hematite.keys.cutChild(key.split("_")[2] ?? "", {
  note: "Server 42",
  scopes: ["images:read"],
  expiresAt: new Date(Date.now() + 7 * 86_400_000).toISOString(),
});
// child.token is the child's key, shown this once.
```

A field that would make it wider than its parent answers `400` `VALIDATION_ERROR`, with that field's path in the issue.

## Turn it off

In Settings → API keys, a key's **⋯** → **Turn off**, or [`DELETE /keys/:publicId`](https://hematite.gg/docs/developers/reference/keys#delete-keys-publicid) signed in. It stops at once, with every child cut from it. Changing your password asks whether your API keys go too (**Turn off your API keys too**, unticked); a password reset by email turns them all off. Every key made and turned off is in your [Audit log](https://hematite.gg/audit-logs).

Your keys are listed by [`GET /users/:userId/keys`](https://hematite.gg/docs/developers/reference/users#get-users-userid-keys), each with its children, never with its token.
