# auth.md

You are an agent. **Pixeltable** is a multimodal backend (an open-source library plus Pixeltable Cloud). This document tells you how to discover protected APIs and register for credentials.

- Open-source library: no auth (`pip install pixeltable`).
- A free Pixeltable Cloud database with no account: with Python, `pxt new` does Steps 3, 5 and 6 below in one command (https://www.pixeltable.com/new.md). Without Python, follow them.
- Public agent APIs on this host (`POST https://www.pixeltable.com/ask`, `POST https://www.pixeltable.com/mcp`) need no credentials.
- Pixeltable Cloud and other protected APIs use OAuth bearer tokens from WorkOS AuthKit, or dashboard API keys (`X-api-key`).

## Step 1 — Discover

Fetch Protected Resource Metadata, then Authorization Server metadata. The PRM is the machine-readable source of truth; this file is the prose companion.

```http
GET https://www.pixeltable.com/.well-known/oauth-protected-resource
```

`resource` is `https://www.pixeltable.com` (use as `aud` if you mint an identity assertion). `authorization_servers` lists the AuthKit issuer. Then:

```http
GET https://signin.pixeltable.com/.well-known/oauth-authorization-server
```

The same document is also published at `https://www.pixeltable.com/.well-known/oauth-authorization-server` (issuer remains `https://signin.pixeltable.com`). Read `agent_auth` in full: `register_uri` / `identity_endpoint`, `claim_uri` / `claim_endpoint`, and `identity_types_supported`.

OIDC discovery: `https://www.pixeltable.com/.well-known/openid-configuration` and `https://signin.pixeltable.com/.well-known/openid-configuration`.

## Step 2 — Pick a method

Public `/ask` and `/mcp` — skip registration.

Otherwise, prefer the lightest AuthKit method that fits:

1. **anonymous**: no email and no human step. Use it to get a trial organization on Pixeltable Cloud (below) that your user claims later.
2. **service_auth** — you have the user's email (`login_hint`). Claim ceremony required before tokens.
3. Human fallback — send the user to `https://www.pixeltable.com/signup`, then create an API key in the dashboard (`X-api-key`).

Do not send `identity_assertion` / ID-JAG unless `identity_types_supported` lists it.

## Step 3 — Register (AuthKit)

Set once:

```bash
ISSUER="https://signin.pixeltable.com"
EMAIL="<user-email>"
```

```bash
# anonymous
REG="$(curl -sS "$ISSUER/agent/identity" -H 'Content-Type: application/json' \
  -d '{"type":"anonymous"}')"

# service_auth (claim ceremony required)
REG="$(curl -sS "$ISSUER/agent/identity" -H 'Content-Type: application/json' \
  -d "{\"type\":\"service_auth\",\"login_hint\":\"$EMAIL\"}")"
```

Errors: `invalid_request`, `invalid_login_hint`, `anonymous_registration_disabled`, `service_auth_registration_disabled`. Do not invent a Pixeltable `/agent/identity` — registration is on the issuer.

## Step 4 — Claim ceremony (service_auth required; anonymous optional)

Mint an attempt and give the user `attempt.verification_uri`. They sign in; the page shows a code. Submit that code with the claim token.

```bash
CLAIM_TOKEN="$(printf '%s' "$REG" | jq -r .claim.token)"
ATT="$(curl -sS "$ISSUER/agent/identity/claim" -H 'Content-Type: application/json' \
  -d "{\"type\":\"service_auth\",\"claim_token\":\"$CLAIM_TOKEN\",\"login_hint\":\"$EMAIL\"}")"
printf '%s' "$ATT" | jq '{verification_uri:.attempt.verification_uri}'

USER_CODE="<code the user read off the claim page>"
VER="$(curl -sS "$ISSUER/agent/identity/claim/complete" -H 'Content-Type: application/json' \
  -d "{\"claim_token\":\"$CLAIM_TOKEN\",\"user_code\":\"$USER_CODE\"}")"
```

## Step 5 — Exchange for an access token

```bash
ASSERTION="$(printf '%s' "$REG" | jq -r .identity.assertion)"
CRED="$(curl -sS "$ISSUER/oauth2/token" -H 'Content-Type: application/x-www-form-urlencoded' \
  -d grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
  --data-urlencode "assertion=$ASSERTION")"
ACCESS_TOKEN="$(printf '%s' "$CRED" | jq -r .access_token)"
```

Send `Authorization: Bearer $ACCESS_TOKEN`. Tokens are short-lived (~5 minutes); re-exchange from the stored assertion. Refresh via `POST $ISSUER/agent/identity` with `{"type":"refresh","refresh_token":"..."}` when the assertion nears expiry.

Revoke at `https://signin.pixeltable.com/oauth2/revoke`.

## Step 6: Get a trial organization (no account)

For building on Pixeltable Cloud before anyone has signed in. With Python, `pxt new` does this and keeps the key where the `pxt` CLI finds it. With the access token of an **anonymous** registration (Steps 3 and 5), you get one organization with one database (`main`), a key that works with the `pxt` CLI, and a link your user claims it with. Unclaimed, it is deleted after 48 hours.

```bash
TRIAL="$(curl -sS -X POST "https://www.pixeltable.com/api/v1/trial-orgs" -H "Authorization: Bearer $ACCESS_TOKEN")"
printf '%s' "$TRIAL" | jq '{org, db, api_url, claim_url, expires_at}'
export PIXELTABLE_API_KEY="$(printf '%s' "$TRIAL" | jq -r .api_key)"
export PIXELTABLE_API_URL="$(printf '%s' "$TRIAL" | jq -r .api_url)"
```

Don't print `api_key`; store it with your registration. Both are shown once: a registration gets one trial, so if you lose them, register again for a new one.

Build in `pxt://<org>:main` with the `pxt` CLI (`pip install -U 'pixeltable[serve]'`: `pxt service update` needs the extra; https://docs.pixeltable.com). The organization holds one database and runs at most two services (stopped and failed ones count), each on 1 worker with up to 1 cpu and 2048 MB: the Community plan's limits, which stay once it is claimed. Until it is claimed, each also gets at most 10 GB of disk, the organization stores up to 50 GB of media (at that, new media is refused while reads and deletes work; drop a table to free space) and writes at most 100 GB of media in all, and its pods reach the internet on ports 80, 443 and 5432 only. Its services answer only requests carrying the key.

Give your user `claim_url`, and no one else: whoever opens it can take the organization. They sign in or create an account, become the organization's admin, and decide whether your key keeps working.

Errors carry `{"message", "detail": {"error_code", "message", "retryable", "retry_after"}}`, and a `Retry-After` header when a retry can succeed: wait that many seconds, and do not retry when `retryable` is false.

- `400` `TRIAL_ADDRESS_UNREADABLE`: the address you called from could not be read, which no retry fixes; send your user to `https://www.pixeltable.com/signup`.
- `401`: the access token expired or is not an Agent Registration token; exchange the assertion again.
- `403`: this registration was claimed, so it works in its person's organization instead.
- `409` `TRIAL_EXISTS`: this registration already has a trial, whose key was shown once; register again for another.
- `429` `TRIAL_SLOTS_FULL`, `TRIAL_RATE_LIMIT` or `TRIAL_NETWORK_LIMIT`: every trial slot is in use, the hour's or the last 15 minutes' new trials are made, or your network already holds 10 unclaimed trials. Retry after `retry_after`, have your user claim one, or send them to `https://www.pixeltable.com/signup`.
- `500` or `503` (`TRIALS_PAUSED`, `TRIAL_UNAVAILABLE`): new trials are paused or Pixeltable Cloud had a fault; nothing was created. Retry after `retry_after`.
- `504` `TRIAL_CREATE_TIMEOUT`: the create may have gone through, and its key cannot be shown again. Do not resend this registration: register a new agent.

In the trial, a `pxt` refusal's reason ends with its code and wait, `[error CODE; retry after N s]`. `503` `TRIAL_COMPUTE_FULL`: trial compute is full for another service or a larger size; wait, or have your user claim the organization. `507` `STORE_FULL`: the media store is full, or has written its 100 GB. `410` `TRIAL_EXPIRED`, or a `401` on a key that worked before: the trial expired unclaimed and is being deleted, or was; nothing in it can be recovered. Register a new agent for a new trial.

## API keys (dashboard)

For a Cloud deployment in the user's own account, run `pxt login` in the agent's terminal and have the user confirm its code in a browser; with no account, use `pxt new` or Step 6. An API key is optional for CLI automation. For direct hosted HTTP calls, keep the key on the application server and send `X-api-key: <key>`. See https://www.pixeltable.com/developers/llms.txt and https://docs.pixeltable.com/cloud.

## More

- Pricing: https://www.pixeltable.com/pricing.md
- Docs: https://docs.pixeltable.com
- Privacy: https://www.pixeltable.com/privacy
- Terms: https://www.pixeltable.com/terms
- API catalog: https://www.pixeltable.com/.well-known/api-catalog
- Contact: contact@pixeltable.com
