> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agentonboard.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# FastAPI

> Authenticate AI agents in FastAPI using AgentBearerAuth and verify RS256 tokens locally against public JWKS keys.

`agentonboard-sdk[fastapi]` provides `AgentBearerAuth`, a FastAPI `Depends()` security dependency that reads the Bearer token, caches public JWKS keys in memory, verifies the `RS256` signature and `aud` claim, and maps outcomes to HTTP `401`, `403`, and `500` responses.

## Copy-paste agent prompt

```text theme={null}
Integrate AgentOnboard into this FastAPI repository.

1. Inspect the codebase and install `agentonboard-sdk[fastapi]`.
2. Read these docs before editing:
   - https://docs.agentonboard.xyz/fastapi.md
   - https://docs.agentonboard.xyz/verify.md
   - https://docs.agentonboard.xyz/accounts.md
   - https://docs.agentonboard.xyz/auth-md.md
3. Ask me three questions before writing code:
   - Which database or user model should the `resolver` query?
   - Should unknown emails return `403 ACCOUNT_REQUIRED` (reject) or create an account on `POST /api/signup`?
   - What is the production Domain hostname (e.g. `notes.com` or `api.notes.com`)?
4. Implement the integration following these invariants:
   - Use `AgentBearerAuth` from `agentonboard.frameworks.fastapi`.
   - Hardcode `audience` to the normalized production Domain.
   - Normalize emails consistently in lowercase (`identity.email.strip().lower()`) before querying your database.
   - Serve the discovery file at both `/auth.md` and `/.well-known/auth.md` with matching `## Audience` and `403` instructions.
5. Verify against localhost by minting a test token with `POST https://agentonboard.xyz/api/v1/token/exchange` and testing `403 ACCOUNT_REQUIRED`, `200 OK`, and `401 INVALID_SIGNATURE`.
```

***

## 1. Install the Python SDK

> [tab] pip
>
> ```bash theme={null}
> pip install "agentonboard-sdk[fastapi]"
> ```

> [tab] uv
>
> ```bash theme={null}
> uv add "agentonboard-sdk[fastapi]"
> ```

> [tab] poetry
>
> ```bash theme={null}
> poetry add agentonboard-sdk --extras fastapi
> ```

***

## 2. Guard your FastAPI routes

Pass `resolver` to look up your existing database user by verified email. When `resolver` returns `None`, `AgentBearerAuth` responds with `403 ACCOUNT_REQUIRED`:

```python title="main.py" theme={null}
from fastapi import Depends, FastAPI
from agentonboard import AgentIdentity
from agentonboard.frameworks.fastapi import AgentBearerAuth

app = FastAPI(title="Notes API")

async def get_user_by_email(identity: AgentIdentity):
    user = await db.users.find_one({"email": identity.email.strip().lower()})
    return str(user["_id"]) if user else None

agent_auth = AgentBearerAuth(
    audience="notes.com",
    resolver=get_user_by_email,
)

@app.post("/api/notes")
async def create_note(user_id: str = Depends(agent_auth)):
    return {"success": True, "created_by": user_id}
```

### Enabling explicit agent signup (`POST /api/signup`)

Keep `agent_auth` (`auto_create=False`) on all regular routes so read and write calls never create accounts as a side effect. Add one dedicated `POST /api/signup` route using `create_agent_account_async`:

```python title="main.py" theme={null}
from fastapi import FastAPI, Header, HTTPException
from fastapi.responses import JSONResponse
from agentonboard import AgentIdentity, create_agent_account_async
from agentonboard.codes import status_from_code

@app.post("/api/signup", status_code=201)
async def signup_agent(authorization: str = Header(default="")):
    outcome = await create_agent_account_async(
        token=authorization,
        audience="notes.com",
        resolver=get_user_by_email,
        creator=lambda identity: create_user_in_db(identity.email.strip().lower()),
    )

    if not outcome.ok:
        raise HTTPException(
            status_code=status_from_code(outcome.code),
            detail={"code": outcome.code, "error": outcome.error},
        )

    return JSONResponse(
        {"user": outcome.user, "created": outcome.created},
        status_code=201,
    )
```

***

## 3. Token-only verification (without account lookup)

If you omit `resolver`, `AgentBearerAuth` verifies the JWT signature and claims and returns `AgentIdentity(email=...)`:

```python title="main.py" theme={null}
from fastapi import Depends, FastAPI
from agentonboard import AgentIdentity
from agentonboard.frameworks.fastapi import AgentBearerAuth

app = FastAPI()
agent_auth = AgentBearerAuth(audience="notes.com")

@app.get("/api/whoami")
async def get_caller(identity: AgentIdentity = Depends(agent_auth)):
    return {"email": identity.email}
```

***

## 4. Test with curl

Mint a 5-minute test token using [`POST /api/v1/token/exchange`](https://docs.agentonboard.xyz/api/token-exchange) and call your FastAPI endpoint:

```bash theme={null}
# Mint a 5-minute test token
TOKEN=$(curl -s -X POST https://agentonboard.xyz/api/v1/token/exchange \
  -H "Authorization: Bearer aon_live_your_key_here" \
  -H "Content-Type: application/json" \
  -d '{"audience":"notes.com"}' \
  | jq -r '.token')

# Call your FastAPI endpoint
curl -i -X POST http://localhost:8000/api/notes \
  -H "Authorization: Bearer $TOKEN"
```

***

> [card] [Django](https://docs.agentonboard.xyz/django.md)
>
> Guard Django views and map tokens to django.contrib.auth users.

> [card] [Testing & Production](https://docs.agentonboard.xyz/testing-and-production.md)
>
> Run all six curl checks and review JWKS caching.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.