> ## 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.

# Django

> Authenticate AI agents in Django views using @require_agent_token and map verified emails to django.contrib.auth users.

`agentonboard-sdk[django]` provides `@require_agent_token` for synchronous and asynchronous Django views. It verifies the Bearer token locally against AgentOnboard's public JWKS, resolves your Django user record, and returns a `JsonResponse` with `401`, `403`, or `500` on failure.

## Copy-paste agent prompt

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

1. Inspect the codebase and install `agentonboard-sdk[django]`.
2. Read these docs before editing:
   - https://docs.agentonboard.xyz/django.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 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 `@require_agent_token` from `agentonboard.frameworks.django`.
   - Hardcode `audience` to the normalized production Domain.
   - Query emails case-insensitively (`email__iexact` or normalized lowercase).
   - 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[django]"
> ```

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

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

***

## 2. Guard your Django views

Pass `resolver` to look up your Django `User` by email. `@require_agent_token` attaches `request.agent_user` and `request.agent_identity` to `HttpRequest`, and passes `agent_user` as a keyword argument when declared in your view signature:

```python title="notes/views.py" theme={null}
from django.contrib.auth import get_user_model
from django.http import HttpRequest, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from agentonboard import AgentIdentity
from agentonboard.frameworks.django import require_agent_token

User = get_user_model()

def resolve_django_user(identity: AgentIdentity) -> int | None:
    user = User.objects.filter(email__iexact=identity.email.strip()).first()
    return user.id if user else None

@csrf_exempt
@require_POST
@require_agent_token(audience="notes.com", resolver=resolve_django_user)
def create_note(request: HttpRequest, agent_user: int) -> JsonResponse:
    # agent_user (and request.agent_user) holds your Django User ID
    return JsonResponse({"success": True, "created_by": agent_user})
```

> [!NOTE]
>
> **Async Django views supported**
>
> `@require_agent_token` inspects your view function with `inspect.iscoroutinefunction`. When you define `async def create_note(request, agent_user):`, the decorator runs async JWKS fetching and awaits async `resolver` callbacks automatically.

***

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

Keep `@require_agent_token(..., auto_create=False)` on all regular views and add one dedicated `POST /api/signup` view using `create_agent_account`:

```python title="notes/views.py" theme={null}
from django.contrib.auth import get_user_model
from django.http import HttpRequest, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from agentonboard import AgentIdentity, create_agent_account
from agentonboard.codes import status_from_code

User = get_user_model()

def create_django_user(identity: AgentIdentity) -> int:
    email = identity.email.strip().lower()
    user = User.objects.create_user(username=email, email=email, password=None)
    return user.id

@csrf_exempt
@require_POST
def signup_agent(request: HttpRequest) -> JsonResponse:
    outcome = create_agent_account(
        token=request.headers.get("Authorization", ""),
        audience="notes.com",
        resolver=resolve_django_user,
        creator=create_django_user,
    )

    if not outcome.ok:
        return JsonResponse(
            {"code": outcome.code, "error": outcome.error},
            status=status_from_code(outcome.code),
        )

    return JsonResponse(
        {"user": outcome.user, "created": outcome.created},
        status=201,
    )
```

***

## 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 Django 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 Django endpoint
curl -i -X POST http://localhost:8000/api/notes \
  -H "Authorization: Bearer $TOKEN"
```

***

> [card] [FastAPI](https://docs.agentonboard.xyz/fastapi.md)
>
> Guard FastAPI routes with AgentBearerAuth.

> [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.