# Authentication

Brands sit in a workspace. A key or sign-in reaches brands of one workspace: all of them, or the ones it names. Every call acts on one of those brands: the one you name with the `X-Otobiz-Brand` header (MCP: the `brandId` argument), else its only brand. A key or sign-in that reaches several brands and names none gets `409 brand_required` with `details.candidates`, the brands it may name. The brand is never taken from the request body or a query parameter.

## API keys

| | |
|---|---|
| Where | Otobiz, **Developers > API** (`https://app.otobiz.ai/apikeys`). A brand owner or admin creates them. |
| Format | `otbz_...`; `otbr_...` for a key that expires |
| Header | `Authorization: Bearer <key>`, on REST and on the MCP server |
| Shown | Once, when the key is created. Store it in a secret manager. |
| Revoke | Any time, from the same page. A revoked key answers `401` at once, and any approval it proposed that is still waiting is rejected. |

Never put a key in a URL, a query string, a shared file or a prompt. A key is a password for the brand.

## Role

An API key acts as one role: owner, admin or member, picked when you make it (your own by default). It can do what a teammate with that role can do, nothing more. A key stops working when its maker leaves the team or can no longer make keys. A call the role does not allow answers `403 forbidden` with `details.cappedBy: "role"` and `details.role`. The scopes a role holds are on [Scopes](scopes.md).

## Signing in an AI client (MCP only)

AI clients that support MCP sign-in (OAuth 2.1 with PKCE) can connect to `https://mcp.otobiz.ai` without a key. The person signs in to Otobiz, picks the brand and approves the scopes; the client receives a token for that one person and that one brand.

- The token works on the MCP server only. Sent to a REST route it answers `401`.
- The person's role is checked on every call. A viewer reads only; a person limited to some locations works on those locations only: lists narrow to them, and a brand-wide read answers `403 brand_wide_only`.
- An owner or admin sign-in may register webhook endpoints. It sees and changes only the endpoints it registered, and they stop receiving events when the AI client is disconnected or the person is no longer an owner or admin.
- Discovery: every request without a credential (`initialize`, `tools/list`, `resources/*` and any tool call) answers `401` (JSON-RPC `-32001`) with `WWW-Authenticate: Bearer resource_metadata="..."`; clients read `/.well-known/oauth-protected-resource` and `/.well-known/oauth-authorization-server` from there and sign in. The docs server (`https://mcp.otobiz.ai/docs`) needs no credential.
- Never put a key or token in a URL: a `token`, `api_key` or `access_token` query parameter answers `400 credential_in_url`.

## Which one to use

| You are | Use |
|---|---|
| Your own backend, a script, a connection | An API key over REST |
| An AI coding tool or agent the user runs (Claude Code, Cursor, Codex, Gemini CLI) | An API key in the client's secret store, or sign-in where the client supports it |
| A hosted assistant (Claude, ChatGPT) | Sign-in |

## Request ids

Every response carries `x-request-id`. Send your own `X-Request-Id` and reuse it on every retry of one logical call; quote it when you write to support.

## How it behaves

- A key works on the brands it was made for: every brand of its workspace, or the ones named when it was made. A sign-in works on the person's brands. Name the brand per call when there are several (`X-Otobiz-Brand`, MCP `brandId`, CLI `--brand`); `get_current_brand` lists them. REST and MCP run the same operations, so the same scope, plan, consent and location rules apply on both.
- Revoking a key or ending a sign-in takes effect on the next call.
- A sign-in's role is read on every call, so a role change applies at once.

## If it fails

| Answer | Cause | Fix |
|---|---|---|
| `401 unauthorized` | No live key or sign-in, a revoked or expired one, or a sign-in token sent to REST | Send a live key from Developers > API. An AI client signs in again. |
| `403 forbidden` | The key's role, or the sign-in capped by the person's role, does not allow this (`details.cappedBy: "role"`, `details.role`) | Make a key with a role that does, or sign in as that role. |
| `403 insufficient_scope` | A sign-in lacks the scope in `details.requiredScope` | Sign in again and approve that scope. |
| `403 brand_wide_only` | A sign-in limited to some locations asked for a total across the whole brand | Use a brand-wide key or sign-in, or read the lists, which narrow to your locations. |
| `400 credential_in_url` | A key or token was in the URL | Move it to the `Authorization` header and treat that key as exposed. |
