Skip to main content

01 — Unified Auth

A complete example combining all auth features in one agent: API key auth for HTTP, allowlist auth for messaging interfaces, composite auth for combining providers, and per-trigger overrides.
from definable.agent import Agent
from definable.agent.auth import APIKeyAuth, AllowlistAuth, CompositeAuth
from definable.model.openai import OpenAIChat

agent = Agent(
  model=OpenAIChat(id="gpt-4o"),
)

# Composite auth: API keys for HTTP + allowlist for Telegram
agent.auth = CompositeAuth(
  APIKeyAuth(keys={"sk-my-api-key"}),
  AllowlistAuth(
    user_ids={"telegram-user-123", "telegram-user-456"},
    platforms={"telegram"},
  ),
)

# Public webhook (no auth)
@agent.on(Webhook("/public-hook", auth=False))
async def public_endpoint(event):
  return "Public response"

# Auth context in hooks
@agent.before_request
async def log_auth(context):
  if hasattr(context, "auth") and context.auth:
    print(f"Authenticated: {context.auth.user_id}")

agent.serve(port=8000)

Full source

definable/examples/auth/01_unified_auth.py