# Authentication

> Make keys, choose what they may do, and know whom a key acts as and who pays for its calls.

Source: https://imario.ai/docs/api/authentication

## Send the key

Every request carries a credential in the `Authorization` header:

```bash
curl https://api.imario.ai/v1/workspace \
  -H "Authorization: Bearer imk_..."
```

The header `X-iMario-Key: imk_...` works too. A request with no credential is refused with 401 `missing_api_key`. A key that is unknown, revoked or expired, or a personal key whose holder has left the workspace, is refused with 401 `invalid_api_key`.

A key belongs to one workspace and works only there.

## Kinds of credential

| Credential | Starts with | Made by | Acts as |
|---|---|---|---|
| Personal key | `imk_` | any member, for themselves | its creator, with the creator's role at the time of each call |
| Service account key | `imk_` | an owner or admin | the workspace, with the role chosen when the key was made |
| Test key, personal or service account | `imk_test_` | the same people | the same, in the [sandbox](https://imario.ai/docs/api/sandbox) only |
| OAuth access token | `imo_` | an app a member approved | the member who approved it, with their current role |

**Personal keys** suit one person's scripts and tools. A personal key never does more than its creator may: if the creator becomes a viewer, the key acts as a viewer from the next call. When the creator leaves the workspace or is removed, their personal keys stop working.

**Service account keys** suit production systems that should not depend on one person. Only an owner or admin can make one. It gets the role Admin, Editor or Viewer, never above the role of the person making it. The key belongs to the workspace and keeps working when its creator leaves.

## Create a key

1. Open the workspace, then **Settings > MCP & API**.
2. Under **API keys**, select **Create key**.
3. Fill in the dialog:
   - **Name**: what the key is for, such as "Nightly CI job".
   - **Type**: **Personal**, or **Service account** with its **Role**. Only owners and admins see this choice.
   - **Mode**: **Live**, or **Test** for the sandbox.
   - **Access**: **Full access**, **Read only**, or **Custom** to choose read and write for each resource.
   - **Expires**: **Never**, after 30 days, 90 days or 1 year, or a custom number of days up to 3,650.
   - **Allowed IP addresses**, optional: one address or CIDR range per line.
4. Select **Create key** and copy the key. It is shown once; afterwards the list shows only its first characters.

Members see their own keys in the list. Owners and admins see every key of the workspace, with who made it, when it was last used and when it expires, and can narrow the list to their own with **Show keys**. A workspace holds at most 50 working keys, live and test together; one more is refused with 409 `key_limit_reached`. A service account cannot get a role above its maker's (403 `service_role_too_high`). Over the API, a service account's key can make only service-account keys (403 `personal_key_needs_member`), and a test key cannot make a live one (403 `test_key_cannot_mint_live`).

Owners and admins can also make keys from code, with `POST /v1/api-keys` and a live key that holds `keys:write`. A key made this way can never do more than the key that made it. See [API keys](https://imario.ai/docs/api/reference/api-keys) in the reference.

## Scopes and roles

A call needs two things: the key must hold the call's scope, and the key's role must reach the role that scope needs. A personal key's role is its creator's current role; a service account's role is the one it was given. A missing scope is refused with 403 `insufficient_scope`, a role that is too low with 403 `role_not_allowed`. Both errors name what was missing in `required_scope` or `required_role`. A test key is refused with 403 `livemode_only` on the calls that need `members:write`, `keys:write` or `connections:write`.

| Scope | What it allows | Least role |
|---|---|---|
| `workspace:read` | The workspace, the credit balance, today's API budget and the credit ledger | Viewer |
| `audiences:read` | Audiences, their people, one person's profile and the key's chat history with them (each key has its own conversation with each person) | Viewer |
| `audiences:write` | Create an audience, a sub-group or an audience from files, and rebuild one | Editor |
| `studies:read` | Estimates, studies, results, responses and segments | Viewer |
| `studies:write` | Run and cancel studies, which spends credits | Editor |
| `insights:write` | The AI read of a group in a study, which spends credits | Viewer |
| `reports:read` | Reports, report estimates, downloads and share links | Viewer |
| `reports:write` | Write reports, which spends credits, make downloads and share links | Viewer |
| `library:read` | Surveys, discussion guides, announcements and files | Viewer |
| `library:write` | Create, change and delete them, and upload files | Editor |
| `chat:write` | Send a message to one person in an audience, which spends credits | Viewer |
| `auto:read` | Auto studies | Viewer |
| `auto:write` | Start, steer, stop and continue Auto studies, which spends credits | Editor |
| `connections:read` | Connected data sources and what each offers | Viewer |
| `connections:write` | Start a sync | Editor |
| `members:read` | The workspace's members | Admin |
| `members:write` | Invite, change roles and remove members | Admin |
| `keys:read` | The workspace's API keys | Admin |
| `keys:write` | Make and revoke API keys | Admin |
| `webhooks:read` | Webhook endpoints and their deliveries | Admin |
| `webhooks:write` | Create, change, test and delete webhook endpoints, and rotate their secrets | Admin |

"Admin" means an owner or an admin. Viewers may spend credits on reports, the AI read of a group and chat with a person. Running studies, building audiences and starting Auto studies need an editor or above.

**Full access** holds every scope, so what the key can do is set by its role. **Read only** holds every `:read` scope: the key can never spend credits or change anything. **Custom** holds the scopes you choose. Keys made before scopes existed have full access.

The same scopes apply on the [MCP server](https://imario.ai/docs/agent-setup/mcp). A key with custom access needs `audiences:read` to list or read audiences, `audiences:write` to create one, `studies:read` to estimate or read a study, `studies:write` to run one and `library:write` to upload a file. Test keys work on the REST API only.

## OAuth access tokens for apps

An app that serves other iMario users, rather than your own workspace, should not ask people to paste keys. It uses iMario's OAuth 2.1 server, the same one AI apps use to connect to the MCP server: authorization code with PKCE (S256). The server's metadata, with every endpoint, is at `https://mcp.imario.ai/.well-known/oauth-authorization-server`. An app registers itself there, or identifies itself by the https address of its own client metadata document.

The person signs in, chooses the workspace the app may reach and approves. The app receives an access token that starts with `imo_` and lasts one hour, and a refresh token that lasts 60 days and is replaced each time it is used. Keep only the newest refresh token. A used one presented again within 60 seconds of its replacement is refused and the connection stays (the app lost the answer and retried); presented later, it revokes every token of the connection, and the app has to be approved again. The access token acts as the person who approved it, with their current role, in that workspace only, and is always live.

| OAuth scope | API scopes it grants |
|---|---|
| `mcp:read` | Every `:read` scope except `members:read`, `keys:read` and `webhooks:read` |
| `mcp:run` | The same, plus `studies:write`, `audiences:write`, `library:write`, `insights:write`, `reports:write` and `chat:write` |

A token never carries the members, keys or webhooks scopes, nor `auto:write` or `connections:write`: those stay with keys a member makes on purpose. The person can disconnect the app under **Connected apps** in **Settings > MCP & API**; its tokens stop working on the next request.

## Limit where and how long a key works

- **Allowed IP addresses.** A key with an allowlist works only from those addresses: IPv4 or IPv6, single addresses or CIDR ranges, up to 50 entries. A call from anywhere else is refused with 403 `ip_not_allowed`.
- **Expiry.** An expired key is refused with 401 `invalid_api_key`. The key list shows each key's date; make the replacement before it.
- **Revoking.** Select the bin icon next to a key and confirm **Revoke**. Members revoke their own keys; owners and admins can revoke any key. From code, an owner or admin calls `DELETE /v1/api-keys/{id}`. The key is refused from its next request. Revoking does not cancel a study the key already started.
- **Suspension.** If the account a key acts for is suspended (the creator of a personal key, the workspace owner for a service account), its calls are refused with 403 `account_suspended`.

## Who pays for a call

| Credential | Credits come from |
|---|---|
| Personal key, OAuth token | The workspace's credit pool when the workspace pays from a pool, otherwise the member's own wallet |
| Service account key | The workspace's credit pool, otherwise the workspace owner's wallet |

Three limits sit on top of the balance:

- **Daily limit**, set by an owner or admin in **Settings > MCP & API**: one ceiling for every API key and connected app of the workspace together, reset at 00:00 UTC. A call that would pass it is refused before it starts, with 402 `daily_budget_exceeded`. `GET /v1/usage` shows what is left today.
- **Member monthly caps** in a workspace that pays from a pool apply to personal keys and OAuth tokens as they apply to the member in the app (402 `member_cap_exceeded`). A service account has no cap of its own; its studies, reports and AI reads are recorded under the workspace's owner, so a cap set on the owner applies to them (chat never counts against one).
- **Rate limits** per key: see [Rate limits](https://imario.ai/docs/api/rate-limits).

Each charge appears in the credit ledger with the key that made it (`api_key_id` in `GET /v1/credit-transactions`).

## See what a key did

**API activity**, on the same Settings page, lists the last 200 requests: time, key, method and route, status, duration and request id. Filter it by key. Members see requests made with their own keys; owners and admins see every key's. Each response also carries its request id in the `Request-Id` header (see [API errors](https://imario.ai/docs/api/errors)).

## Keep keys secret

- Call the API from a server only. Never put a key in a web page, a browser extension or a mobile app: anyone who opens it can read the key. If a browser needs results, your server calls the API and passes on what the page needs.
- Keep keys in an environment variable or a secret manager. Never put one in a committed file, a ticket, a chat message or a support request.
- Give each system its own key with only the scopes it needs, so you can revoke one without stopping the others.
- To rotate a key, create the new one, switch your system to it, check in **API activity** that calls arrive with the new key, then revoke the old one.
- If a key leaks, revoke it at once.
