# API authentication and tokens

How to authenticate with the hematite API: session tokens, personal API keys, upload tokens for ShareX, app tokens, and the header that carries them.

Most endpoints of the [hematite API](https://hematite.gg/docs/developers) act as a user, so they need a token: a **session token** for everything a signed-in user may do, a **personal API key** for your own scripts that may do only the scopes you chose, an **upload token** that may only upload, or an **app's token** from [Sign in with hematite](https://hematite.gg/docs/developers/oauth), which may do what its scopes allow. The reference says which each endpoint takes (a session, a session or an upload token, or nothing) and the scope an app's token or an API key needs for it.

## Sending a token

Put the token in the `Authorization` header:

```bash
curl https://api.hematite.gg/v1/users/1/images \
  -H "Authorization: $HEMATITE_TOKEN"
```

The token alone is how the website, the SDK and ShareX send it. `Bearer` in front of it works too (`Authorization: Bearer …`), as OAuth apps send theirs.

An endpoint that needs a token answers `401` with the code `UNAUTHORIZED` when there's none, or when it's expired or was removed (see [API errors](https://hematite.gg/docs/developers/errors)).

## What a token looks like

Every hematite token says what it is, and never whose it is:

```text
hms_live_m0plGGtn67kMZy6D_0fpSq8rMh8LefocvzsssAcXqFMdcLfv0rwXDOmBe2YhDbZXSQk86Zmf9MyOHDuG270MH7DhOKWtDsos9_0OjTqM
```

- **Its kind** comes first: `hms_` a session token, `hmk_` a personal API key, `hmu_` an upload token, `hmo_` an app's access token, `hmr_` its refresh token, `hma_` an app's secret.
- **Then where it works:** `live` on hematite.gg. A token made elsewhere (a staging copy) is refused here.
- **Then its public id,** 16 characters that name it. It's safe to show: support may ask for it, and a session's `publicId` in [`GET /users/:userId/sessions`](https://hematite.gg/docs/developers/reference/users#get-users-userid-sessions) is the same. It proves nothing on its own.
- **Then its secret,** and a short checksum that tells a whole token from a typo. hematite keeps only a keyed hash of the secret, never the token.

Its length is fixed, so a token cut off when pasted is easy to tell. Nothing in it says which user it belongs to: ask [`GET /users/@me`](https://hematite.gg/docs/developers/reference/users#get-users-me). Keep tokens out of public repositories and screenshots: anyone holding one can use it until it's removed.

## Session tokens

A session token is what signing in gives you, and what the website uses: it may do anything you may do on the website.

1. **Sign in** with [`POST /auth/login`](https://hematite.gg/docs/developers/reference/auth#post-auth-login), your email and password in the body.
2. **Keep the token** from the answer's `data.token`. The answer's `data.user.id` is your user id, which endpoints like `/users/:userId/images` ask for; [`GET /users/@me`](https://hematite.gg/docs/developers/reference/users#get-users-me) answers with the user a token belongs to, at any time. [The SDK](https://hematite.gg/docs/developers/sdk) takes the token when you make its client.
3. **Send it** in `Authorization` with every request.

```bash
curl -X POST https://api.hematite.gg/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email":"nova@example.com","password":"••••••••"}'
```

With [two-step sign-in](https://hematite.gg/docs/account/two-step-sign-in) on, the answer has no token yet but a `challenge`: its `token` (`hmt_…`), the second steps the account can take (`methods`) and when it runs out, five minutes on. Send the challenge with a code from the authenticator app, a recovery code or a passkey's answer to [`POST /auth/login/second-factor`](https://hematite.gg/docs/developers/reference/auth#post-auth-login-second-factor), which answers the session token as signing in does. A challenge takes five wrong answers.

A session token lasts 7 days. [`POST /auth/logout`](https://hematite.gg/docs/developers/reference/auth#post-auth-logout) ends the session it's sent with. Your sessions are listed by [`GET /users/:userId/sessions`](https://hematite.gg/docs/developers/reference/users#get-users-userid-sessions) and in [Settings → Security](https://hematite.gg/settings/security). Changing your password ends every other session token and keeps the one it was changed with; a password reset ends them all.

## Personal API keys

A personal API key is the token for your own code: a script, a bot, the [SDK](https://hematite.gg/docs/developers/sdk) on a server. You make it in [Settings → API keys](https://hematite.gg/settings/api-keys) with only the scopes it needs (the ones apps ask for), and it's held to them as an app's token is, endpoint by endpoint.

- **It lasts 90 days** unless you choose 7 days, 30 days or a year (366 days at most).
- **It can be narrowed:** to some addresses, a rate a minute, a number of uses, and for a key that uploads, a folder and a visibility.
- **It's shown once,** and listed by [`GET /users/:userId/keys`](https://hematite.gg/docs/developers/reference/users#get-users-userid-keys) without its token.
- **It can have child keys,** cut from it and never wider than it, which end with it.
- **Turning it off** stops it at once, with its children.

[Personal API keys](https://hematite.gg/docs/developers/api-keys) has all of it, step by step.

## Upload tokens

An upload token may upload images and nothing else: any other endpoint answers `403`. It's the safe choice for a tool that only uploads: ShareX's config holds one.

- **Make one** in [Settings → Security](https://hematite.gg/settings/security), under **Upload tokens**, or with [`POST /sessions`](https://hematite.gg/docs/developers/reference/sessions#post-sessions) and a session token. You choose when it expires, at most 366 days ahead. Making one fails while you already have 3.
- **Or download a ShareX config** ([ShareX](https://hematite.gg/docs/uploaders/sharex)): each holds a new upload token that lasts 90 days.
- **With two-step sign-in on,** making one asks you to confirm it's you first: it answers `401` with the code `REAUTH_REQUIRED` until [`POST /auth/reauth`](https://hematite.gg/docs/developers/reference/auth#post-auth-reauth) takes your second step, which holds ten minutes.
- **It's shown once.** hematite keeps only a keyed hash of its secret, so a lost token can't be shown again: make a new one.
- **Remove it** in Settings → Security, or with [`DELETE /sessions/:sessionId`](https://hematite.gg/docs/developers/reference/sessions#delete-sessions-sessionid). Whatever holds it stops working.
- **A password change keeps it** unless you tick **Remove your upload tokens too** as you change it; then every one goes. A password reset keeps them too.

## App tokens

An app that people sign in to with their hematite account gets its own tokens, never their password: an access token that lasts an hour and may do only what its scopes allow, and a refresh token for the next one. [Sign in with hematite](https://hematite.gg/docs/developers/oauth) is how, step by step, once you've [created the app](https://hematite.gg/docs/developers/create-an-app); these docs' **Try it** tabs use it to send requests as you.

## Endpoints without a token

Some endpoints answer anyone: public lists like badges and achievements, a public profile, a share by its code, an image's comments. Sent with a token, some of them answer what only you may see as well; your own user, for one, includes your email.
