# auth.md

You are an agent. This document tells you how to register and use a credential (`api_key` / `Bearer`) for **Citame.ai** — including temporary anonymous sandbox environments you can provision without an account and (optionally) have the user claim later to make permanent. Follow the steps in order; do not skip ahead.

This supports both the **user claimed (anonymous one-shot)** flow and **workspace API key (`ctm_...`)** authentication.

Endpoints live at `https://app.citame.io`. The claim ceremony happens at `https://app.citame.io/settings/api`.

## Step 1 — Provision the environment

```http
POST /api/v1/agent-auth/register HTTP/1.1
Host: app.citame.io
Content-Type: application/json

{
  "agent_name": "example-agent",
  "credential_type": "api_key",
  "scopes": ["citame:read", "mcp:tools"]
}
```

No authentication required for initial anonymous provisioning.

Response (200):

```json
{
  "clientId": "client_ctm_01ABC123",
  "apiKey": "ctm_test_anonymous_key_...",
  "claimToken": "clm_ctm_...",
  "expiresIn": 86400
}
```

The API may also respond in snake_case (`client_id`, `api_key`, `claim_token`) — accept either.

What each field is:

- `apiKey` — your bearer credential (`api_key`) for the Citame.ai REST API v1 (`https://app.citame.io/api/v1`) and MCP Server (`https://app.citame.io/api/mcp/mcp`). Use it immediately ([Step 2](#step-2--use-the-credential)).
- `clientId` — identifies the agent environment. Required for the claim flow ([Step 3](#step-3--claim-ceremony-optional)).
- `claimToken` — single-use handle for the claim ceremony. Returned **exactly once** — persist it alongside `apiKey`; do not log it.

## Step 2 — Use the credential

```http
GET /api/v1/prompts HTTP/1.1
Host: app.citame.io
Authorization: Bearer <apiKey>
```

And for the Model Context Protocol (MCP) Streamable HTTP server:

```http
POST /api/mcp/mcp HTTP/1.1
Host: app.citame.io
Authorization: Bearer <apiKey>
Content-Type: application/json
```

If you get a 401 on a previously-working `apiKey`: the credential was revoked or expired. Drop the credential and restart at [Step 1](#step-1--provision-the-environment).

## Step 3 — Claim ceremony (optional)

Claim links the agent credential to a user's permanent Citame.ai workspace, unlocking full plan quotas (Pro / Agency).

### 3a. Mint a claim nonce

```http
POST /api/v1/agent-auth/claim HTTP/1.1
Host: app.citame.io
Content-Type: application/json

{
  "client_id": "<clientId>",
  "claim_token": "<claimToken>"
}
```

Response (200):

```json
{
  "nonce": "nonce_ctm_...",
  "claim_url": "https://app.citame.io/settings/api?claim_nonce=nonce_ctm_..."
}
```

### 3b. Send the user to the dashboard

Construct the claim URL:

```
https://app.citame.io/settings/api?claim_nonce=<nonce>
```

### 3c. Revocation

To revoke an agent credential when no longer needed:

```http
POST /api/v1/agent-auth/revoke HTTP/1.1
Host: app.citame.io
Authorization: Bearer <apiKey>
```

## Errors

| Status | Where | Meaning | What to do |
| ------ | ----- | ------- | ---------- |
| 401 | `/api/v1/*` or `/api/v1/agent-auth/claim` | `apiKey` or `claim_token` invalid or expired | Restart at Step 1 or treat prior claim as completed |
| 402 | `/api/v1` | Payment Required (x402 / MPP Challenge) | Present `PAYMENT-SIGNATURE` or `Authorization: Payment` or use a claimed Pro/Agency `apiKey` |
| 404 | `/api/v1/agent-auth/claim` | Environment not found for `client_id` | Restart at Step 1 |
| 409 | `/api/v1/agent-auth/claim` | Environment already claimed | Stop the ceremony — the credential is permanent |
| 429 | any | Rate limited | Exponential backoff, retry |
| 5xx | any | Transient server error | Exponential backoff, retry |

Full API reference: <https://app.citame.io/api/v1/docs>.
