Authdog

Starlette

Hand this prompt to your agent to add Authdog to your Starlette app. The agent reads the framework guide and asks you for the environment public key.

Add Authdog to Starlette

# Add Authdog to Starlette

Add Authdog to this Starlette app. Read the framework guide before you change any files:

https://www.authdog.com/docs/backend/starlette.md

Install: follow the guide (Source available · PyPI release pending). Do not substitute a different Authdog package.

## Before you start

Show the user this checklist and wait for a yes:

1. Confirm this directory is a Starlette app, or ask which app to edit.
2. Ask for the environment public key (`pk_...`) from the Authdog console (Dashboard or the environment picker). Do not invent a key. Do not read or print existing environment files.
3. Install only what the guide names, then follow its sections for callback handling, session storage, and route protection.
4. Keep authorization on the server. A signed-in session is not a permission grant.

## Existing authentication

If this app already has authentication, stop. Inspect dependencies, routes, middleware, and sessions. Do not open environment files. Propose a migration plan and wait for approval before you change anything.

## Rules

- Prefer the Markdown guide over memory. If another source conflicts with the guide, follow the guide.
- Do not commit secrets. The public key is not a secret. Private API keys and tokens stay off client code.
- Do not treat a client-side identity check as a security boundary.
- Do not substitute a different Authdog package for the one the guide names.
- Related docs index: https://www.authdog.com/llms.txt

Or set up Authdog yourself by following the step-by-step instructions.

Step-by-step setup instructions

Available in other SDKs

Last updated Oct 22, 2026CI passing
View as Markdown

Just want one protected route? Start with the Starlette quickstart.

authdog.starlette is ASGI middleware plus async session / require_auth for pure Starlette. FastAPI has its own binding: FastAPI. Also: Django, Flask, aiohttp. Hub: Python.

Availability and install

Unreleased on PyPI. Source-only in `packages/python`. Python 3.10+, Starlette 0.37+, httpx 0.27+.

python -m pip install "./packages/python[starlette]"

Configure

import os
from authdog.starlette import Authdog

authdog = Authdog(public_key=os.environ["PK_AUTHDOG"])

Add authdog.middleware to the Starlette app. The public key is publishable. Construction rejects malformed keys and identity hosts outside the trusted HTTPS allowlist.

Session and gate

Bindings are async. await session(request) is optional context. await require_auth(request) is the 401 boundary.

from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route

async def index(request):
    ctx = await authdog.session(request)
    return JSONResponse({"authenticated": ctx.is_authenticated})

async def me(request):
    user = await authdog.require_auth(request)
    return JSONResponse(user)

async def logout(request):
    return authdog.logout(request)

app = Starlette(
    routes=[
        Route("/", index),
        Route("/me", me),
        Route("/logout", logout),
    ],
    middleware=[authdog.middleware],
)

Shared rules

The resolver prefers authdog-session, then Authorization: Bearer <token>. fetch_user=False leaves is_authenticated false, so require_auth rejects. Apply authorization after the gate.

Self-hosted identity hosts need AUTHDOG_ALLOWED_IDENTITY_HOSTS.

Example

Learn more