Authdog

aiohttp quickstart

Last updated Sep 10, 2026CI passing
View as Markdown

Outcome: an aiohttp handler that returns 401 unless Authdog authenticates the caller. About five minutes.

Unreleased: not on PyPI. Install from the public source tree.

  1. Prerequisites: Python 3.10+, aiohttp 3.9+, and an Authdog environment.

1. Create an environment and copy the public key

In the console, create a tenant → project → environment and copy Public key from Dashboard. Details: Environments.

2. Install from source

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

Pin `packages/python`.

3. Set the env var

export PK_AUTHDOG="pk_..."

4. Construct the client

import os
from authdog.aiohttp import Authdog

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

5. Protect one handler

from aiohttp import web

@authdog.require_auth
async def me(request):
    ctx = await authdog.session(request)
    return web.json_response(ctx.user)

app = web.Application(middlewares=[authdog.middleware])
app.router.add_get("/me", me)

6. Verify

GET /me without a session → 401. With a valid cookie or bearer token from hosted sign-in → user JSON.

If it fails: middleware not registered; wrong environment key; expected FastAPI Depends (this binding is a decorator plus await session).

Next

The aiohttp guide covers logout (HTTPFound) and async session. Other Python frameworks: FastAPI, Django, Flask, Starlette.