# API authentication

The REST API is opened by a workspace API key. A key is a long random string that starts with `hms_live_`, belongs to one workspace and carries the scopes you gave it. You send it as a bearer token with every request:

```bash
curl https://heymysocial.com/api/v1/content \
  -H "Authorization: Bearer $HMS_API_KEY"
```

These keys are for the REST API only. An assistant connects with its own sign-in or a personal `hms_mcp_` key; see [Connect an assistant](https://heymysocial.com/docs/assistant/connect).

## Create a key

1. Open [Settings, API keys](https://heymysocial.com/settings/api-keys) in the workspace the key is for. The screen is there for people who may manage integrations in that workspace.
2. Under **Create a key**, give it a **Name** that says where it will be used, for example "Website form".
3. Tick the **Scopes** it needs. At least one is required.
4. Set an **Expiry date** if the key should stop by itself. It stops working at the end of that day, in the time zone of the workspace. Leave it empty for a key that does not expire.
5. Press **Create key**.

The key is then shown under **Copy your new key now**. This is the only time: only a SHA-256 fingerprint is stored, so nobody, us included, can show it again. Copy it to where your system keeps its secrets, then press **I have copied it**. If a key is lost, revoke it and create another.

## The key format

A key is `hms_live_` followed by 40 letters and digits, 49 characters in all.

The list of keys on the screen shows the first 14 characters of each, so you can tell them apart, together with the name, the scopes, when the key was last used and when it expires.

A string that does not start with `hms_live_` is refused before anything is looked up.

## Scopes

A scope says what a key may do. An operation names the scope it needs, and a key without it is answered `403`.

<div class="table-scroll"><table><thead><tr><th>Scope</th><th>Allows</th></tr></thead><tbody><tr><th scope="row"><code style="white-space:nowrap">events:write</code></th><td>Send events: page views, leads, lead status and sales</td></tr><tr><th scope="row"><code style="white-space:nowrap">leads:read</code></th><td>Read leads</td></tr><tr><th scope="row"><code style="white-space:nowrap">leads:write</code></th><td>Create and update leads</td></tr><tr><th scope="row"><code style="white-space:nowrap">sales:write</code></th><td>Record sales</td></tr><tr><th scope="row"><code style="white-space:nowrap">content:read</code></th><td>Read content</td></tr><tr><th scope="row"><code style="white-space:nowrap">content:write</code></th><td>Create content drafts</td></tr><tr><th scope="row"><code style="white-space:nowrap">reports:read</code></th><td>Read reports</td></tr><tr><th scope="row"><code style="white-space:nowrap">ads:write</code></th><td>Import ad data</td></tr></tbody></table></div>

| Scope | Operations it opens |
|---|---|
| `events:write` | [Send events](https://heymysocial.com/docs/api/operations/ingest-events) |
| `leads:write` | [Send CRM records in a batch](https://heymysocial.com/docs/api/operations/send-crm-records) |
| `sales:write` | Sale records in that batch. It is needed together with `leads:write` |
| `leads:read` | [List CRM reconciliation errors](https://heymysocial.com/docs/api/operations/list-crm-reconciliation-errors) |
| `content:read` | [List content items](https://heymysocial.com/docs/api/operations/list-content), [Get one content item](https://heymysocial.com/docs/api/operations/get-content) |
| `content:write` | [Create a draft](https://heymysocial.com/docs/api/operations/create-content-draft), [Add a version](https://heymysocial.com/docs/api/operations/add-content-version), [Ask a person to approve](https://heymysocial.com/docs/api/operations/request-content-approval) |
| `ads:write` | [Import ad results](https://heymysocial.com/docs/api/operations/import-ads) |
| `reports:read` | No operation uses it today |

No scope lets a key approve, schedule or publish. There is no such operation.

## The key decides the workspace

A request never says which workspace it is for. The workspace is the one the key was created in, and a workspace id in a body or an address is not read.

Everything a key can reach is in that workspace. The id of a content item, a channel or a CRM connection from another workspace is answered `404` `not_found`, exactly like an id that does not exist.

An agency with several client workspaces creates a key in each one.

## Expiry and revoking

- **Expiry.** A key with an expiry date is refused from the day after that date.
- **Revoking.** Press **Revoke** next to a key under **Keys**. Requests with it are refused from then on. A revoked key stays in the list, marked as revoked, and cannot be switched on again.
- **Archived workspaces.** The keys of a workspace that is archived stop working until it is restored.

Creating and revoking a key are both written to the workspace's audit log. The key itself is never logged.

## When a key is refused

A request without a usable key is answered `401`:

```json
{
  "code": "unauthenticated",
  "message": "A valid API key is required (Authorization: Bearer hms_live_...).",
  "request_id": "01M88Q4X0V67X7S6H2DCQJGDB0",
  "retryable": false
}
```

The answer is the same whether the header is missing, the key is mistyped, revoked or expired, or its workspace is archived. It never says which.

A valid key without the scope an operation needs is answered `403`, and the message names the scope:

```json
{
  "code": "access_denied",
  "message": "This API key lacks the scope content:write.",
  "request_id": "01M88Q4X0BKFD50W79NKFXK67W",
  "retryable": false
}
```

Neither is worth retrying. Fix the key first. [Errors](https://heymysocial.com/docs/api/errors) describes the shape.

## Looking after keys

- Make one key for each system, and name it after that system. When a system is retired or a key leaks, you revoke one key and nothing else stops.
- Give each key the fewest scopes that do its job. A website form needs `events:write` and nothing more.
- Keep keys on a server: in an environment variable or a secret store. Never put one in a web page, a mobile app or a repository. For a browser there is the [tracking script](https://heymysocial.com/docs/tracking/script), which uses the public key of a tracking site instead.
- To replace a key, create the new one, switch your system over, check **Last used** on the old one, then revoke it.
- If a key may have been seen by someone else, revoke it at once. It costs one minute to make another.
