# Keys and credentials

Three kinds of credential meet around a brand. They never stand in for each other.

| Credential | What it is | Who holds it | How it is set | Seen by your key or AI client |
|---|---|---|---|---|
| **Caller key** | Your API key (`otbz_…` / `otbr_…`) or an AI client's sign-in. Says WHICH brand, WHICH role and WHO is acting. | You | Otobiz → Developers → API, or the sign-in page | Itself only (`whoami`) |
| **Channel credentials** | The brand's login to WhatsApp, Instagram, Messenger, Telegram, LinkedIn, X, a store, a payment provider | Otobiz, sealed per brand | A brand member connects the channel in Otobiz (`create_connect_session` gives the link) | Never. `list_connections` shows kind, status and health only |
| **Platform keys** | Otobiz's own accounts with providers (model providers, website reading, places, social platforms); every brand's AI runs on them and is paid in credits | Otobiz | Otobiz operations | Never |

## What each one can and cannot do

- **Your key never reaches a provider.** It resolves the brand, the role and the actor, and stops there. Every call to a platform uses the brand's channel credentials, inside Otobiz.
- **No key, sign-in or tool connects, disconnects or re-keys a channel.** Connecting needs the person signed in at the platform; the API answers a link (`create_connect_session`) and `409 reconnect_required` + `details.connectUrl` when a channel needs a person.
- **Revoking your key never touches a channel.** Channels keep sending and receiving, the AI agent keeps answering, webhooks owned by other keys keep delivering. Proposals your key made that still wait are rejected.
- **A connection id from another brand is `404`**, the same as one that does not exist.
- **A write-only secret is never returned**: a webhook signing secret, a channel token or any part of it. Responses carry facts about it (`hasSecret`, `status`, `lastError`).
- **Resold services spend the brand's balance** (website scans, places, connected-app tools, X reads, live data). At a zero balance they answer `402 usage_empty` + `details.paymentUrl` before any work starts.

## Every call is recorded

Each call writes one Logs row naming the caller (the key, or the person behind a sign-in and the AI app), the route, the status and, when it caused AI work, how many model calls (`aiCalls`). A send or provider write adds its own rows: the approval, the execution and the provider's message id.

## Setting up a brand, in order

1. **Brand + plan**: create the brand; pick a plan (`402 plan_required` until one is active where a feature needs it).
2. **Channels**: a member connects each channel in Otobiz (`create_connect_session` for the link).
3. **Knowledge**: the website and business info the agent answers from.
4. **Balance**: top up for AI work and resold services (`get_billing` with `include=balance`; create_billing_checkout with `kind: topup` gives the owner a checkout link).
5. **Caller keys**: mint one key per connection, of the lowest role that does the job (member, admin or owner); give AI clients a sign-in instead where they support it.

## How it behaves

- A key says which brand, which role and who is acting. It never reaches a platform; Otobiz calls the platform with the brand's own channel login.
- A reply, post or other write goes out through the brand's connected channel as the person or key that asked, and is recorded.
- A channel that needs a person (a lapsed login, a new permission) is reconnected in Otobiz. No key can do it.

## If it fails

| Answer | Cause | Fix |
|---|---|---|
| `409 reconnect_required` | The channel's login lapsed, or the channel this action needs is not connected | Open `details.connectUrl` (a member reconnects it), then repeat the call. |
| `412 platform_scope_missing` | The connected account lacks a permission this action needs; `details.scope` names it | Reconnect the channel and grant it. |
| `402 usage_empty` | A resold service (a website scan, a place lookup) with no balance left | Top up at `details.paymentUrl`. Nothing ran. |
| `402 plan_required` | The brand has no active plan | Open `details.paymentUrl` and choose a plan. |
| `404` on a connection id | The id belongs to another brand, or does not exist | List the brand's connections and use one of those ids. |
