Outcome: a FastAPI route that returns the Authdog user only when require_auth succeeds. About five minutes.
Unreleased: not on PyPI. Install from the public source tree. Do not run pip install authdog-fastapi in production.
- Prerequisites: Python 3.10+ and an Authdog environment.
1. Create an environment and copy the public key
In the console:
- Create a tenant, a project, and an environment.
- Open Dashboard (
/dashboard/home) and copy Public key (pk_...).
The public key is publishable. Details: Environments.
2. Install from source
python -m pip install "./packages/python[fastapi]"Clone or pin `packages/python`. FastAPI 0.110+.
3. Set the env var
export PK_AUTHDOG="pk_..."4. Construct the client
import os
from authdog.fastapi import Authdog
authdog = Authdog(public_key=os.environ["PK_AUTHDOG"])5. Protect one route
from fastapi import Depends, FastAPI
app = FastAPI()
@app.get("/me")
async def me(user=Depends(authdog.require_auth)):
return user6. Verify
GET /me without a session should be 401. With a valid authdog-session cookie or Authorization: Bearer <token> from hosted sign-in, you should get the user.
If it fails: wrong environment key; used session instead of require_auth; or pip looked on PyPI (use the source extra).
Next
The FastAPI guide covers logout, caching, and fetch_user=False. Other Python frameworks: Django, Flask, Starlette, aiohttp.