# auth.md

Bavlio is agent-native: an agent registers an account, configures it and works with it before the human does anything. The human only confirms one email and pays. Everything below is available on REST (`https://api.bavlio.com`, OpenAPI at `https://api.bavlio.com/openapi.json`, reference at `https://api.bavlio.com/docs`) and on MCP (`https://mcp.bavlio.com/mcp`).

**Before you register, ask the human which email address to use, and register with it.** Bavlio sends the confirmation email at once. Until the human confirms it, nobody can sign in to the account, not even after paying. After 7 days an unconfirmed account can no longer be claimed, and without a paid plan it is deleted with everything set up in it.

## 1. Discover

- Protected resource metadata: `GET https://mcp.bavlio.com/.well-known/oauth-protected-resource/mcp` (RFC 9728). The MCP server at `https://mcp.bavlio.com/mcp` answers `401` with `WWW-Authenticate: Bearer resource_metadata="https://mcp.bavlio.com/.well-known/oauth-protected-resource/mcp"` until it holds a credential.
- Authorization server metadata: `GET https://api.bavlio.com/.well-known/oauth-authorization-server` mirrors the issuer `https://fgdvuqqucvfxclfflffg.supabase.co/auth/v1` (OAuth 2.1 with PKCE, dynamic client registration, refresh tokens).

## 2. Pick a method

1. You have no credential: ask the human for their email and register with it (section 3). This is the default for agents.
2. You are an MCP client with a human present: connect to `https://mcp.bavlio.com/mcp` without a credential and follow the OAuth challenge; the human signs in at `https://bavlio.com/` and approves the client. The access token is a Bearer credential for REST and MCP alike.
3. The human already has an account: they create an API key at the website or ask you to call `create_api_key` once you hold any credential of that account.

## 3. Register with the human's email

```http
POST https://api.bavlio.com/registrations
Content-Type: application/json

{ "email": "user@example.com" }
```

The answer is `201`:

```json
{
  "registration": {
    "id": "...",
    "status": "awaiting_verification",
    "email": "user@example.com",
    "verification_sent_at": "2026-10-12T19:00:00.000Z",
    "expires_at": "2026-10-19T19:00:00.000Z",
    "account": { "id": "...", "email": null, "status": "ephemeral", "created_at": "...", "verified_at": null }
  },
  "api_key": "bav_..."
}
```

Then tell the human right away, before you set anything up: "Open the email from Bavlio and confirm your address." The link in it expires, so don't leave this to the end. Carry on with setup while they do.

`api_key` is shown once: keep it for the whole session, and never write it into code or a file that is committed. It authenticates every call as `Authorization: Bearer bav_...`. The same tool exists on MCP without a credential at `https://mcp.bavlio.com/mcp/registration` as `start_registration`; use the key it returns as the Bearer credential on `https://mcp.bavlio.com/mcp` or REST for everything else.

`409 registration_email_taken` means an account with that email exists already: the human signs in at the website and creates an API key there instead. Starts are limited per client address; a `429` carries `retryAfterSeconds`.

## 4. Claim later, only when no email was given

Leave `email` out only when no human can answer. As soon as you learn their email, claim the registration with your key:

```http
POST https://api.bavlio.com/registrations/{id}/claim
Authorization: Bearer bav_...
Content-Type: application/json

{ "email": "user@example.com" }
```

MCP: `claim_registration` on `https://mcp.bavlio.com/mcp`, with the key as the Bearer credential. Repeating the call with the same email resends the confirmation email. `409 registration_email_taken` means the same as in section 3.

## 5. Remind the human before checkout

Opening the link confirms the address and signs the user in to their dashboard at `https://bavlio.com/home`. Call `POST https://api.bavlio.com/workspace/onboarding/completion` (MCP: `complete_workspace_onboarding`) once the workspace is set up, or the dashboard sends the user through onboarding first.

Check `GET https://api.bavlio.com/registrations/{id}` (MCP: `get_registration`), no more than once a minute, until `status` is `verified`; the `account.status` turns `active` at the same moment. Before you open checkout, check it once more: if it is not `verified` yet, remind the human to confirm their email, then go on. Checkout works either way, but only a confirmed human can sign in to what they paid for. Until then the account is `ephemeral`: the credential works for configuration, and operations that need a verified account answer `403 auth_account_not_verified`.

## 6. Use the credential

```http
GET https://api.bavlio.com/whoami
Authorization: Bearer bav_...
```

Create further keys with `POST https://api.bavlio.com/api-keys` (`{ "name": "..." }`), list them with `GET`, revoke with `DELETE https://api.bavlio.com/api-keys/{id}`. On MCP: `whoami`, `list_api_keys`, `create_api_key`, `revoke_api_key`.

## Errors

Every error is JSON `{ "_tag", "statusCode", "code", "message" }`. `401 auth_missing` or `auth_invalid`: present a valid Bearer credential. `403 auth_account_not_verified`: finish section 5. `404 registration_not_found` after those 7 days: start over at section 3. `429 rate_limited`: wait `retryAfterSeconds`.
