Python ยท FastAPI

Authentication for FastAPI

A session dependency, a require_auth gate, and a logout handler for Python backends. Validate Authdog sessions on every request โ€” on the same wire as the Node SDKs.

A dependency, not a framework

Everything FastAPI apps need

A drop-in session dependency, a require_auth gate, and a typed context โ€” wire-compatible with the rest of your Authdog stack.

A FastAPI dependency

Depends(authdog.session) resolves the request's session and returns a typed AuthdogContext โ€” token, user, and is_authenticated โ€” for any path operation.

require_auth gate

Depend on authdog.require_auth and FastAPI raises HTTPException(401) for unauthenticated requests, returning the user object otherwise. This is the security boundary.

At most one userinfo call

The resolved context is cached on request.state, so combining session and require_auth on one request makes a single outbound userinfo call.

Reads the cookie itself

The dependency reads the authdog-session cookie or an Authorization: Bearer header directly โ€” fastapi and httpx are the only runtime dependencies.

Safe logout handler

authdog.logout(request) returns a RedirectResponse that expires the session cookie and redirects to a redirect_uri sanitized against open redirects.

Same wire as Node

It mirrors @authdog/express and @authdog/fastify on the wire, so one Authdog environment serves your Node and Python services interchangeably.

Gate routes with a dependency

Protect a route with require_auth

Depend on authdog.require_auth and FastAPI rejects unauthenticated requests before your handler runs. The dependency returns the verified user object, resolved once per request.

routes.py
# routes.py
@app.get("/me")
async def me(user=Depends(authdog.require_auth)):
return user

Ship secure Python APIs

Authdog's FastAPI SDK resolves the session as a dependency and enforces auth at a single gate, caching the result so each request makes at most one userinfo call.

Depends()

To resolve the session

Add authdog.session as a dependency and any path operation gets a typed AuthdogContext โ€” no middleware boilerplate.

require_auth

The single enforcement point

Depend on authdog.require_auth and unauthenticated requests are rejected with 401 before your handler runs.

2

Runtime dependencies

Only fastapi and httpx โ€” httpx is pulled in automatically, so there's nothing else to install.

Add auth to your FastAPI app.

pip install the SDK, add the session dependency, and gate your routes with require_auth today. Free to start, with secure defaults built in.