Skip to main content
All integrationsAuthentication

Better Auth Integration

Layer Better Auth in front of the kit's API when you need social/passkey login.

FastAPI AI Kit's built-in auth is JWT + API keys, aimed at API-to-API and programmatic access. Better Auth is a TypeScript auth library for user-facing sign-in flows (OAuth, passkeys, magic links). The two solve different problems and typically sit at different layers.

Setup in 4 steps

  1. 1Run Better Auth in your Next.js/Node frontend for user sign-in (OAuth, passkeys, email)
  2. 2After sign-in, have your frontend exchange the Better Auth session for a kit-issued API key or short-lived JWT
  3. 3The FastAPI backend keeps validating its own JWT/API keys — no auth library needed on the Python side
  4. 4Map Better Auth user IDs to the kit's API key records for per-user usage tracking

What's included

  • Keeps user-facing auth (social login, passkeys) in the frontend where Better Auth is strongest
  • Keeps API auth (rate limiting, billing, per-key tiers) in the kit where it's already built
  • No need to reimplement OAuth flows in Python
  • Per-user Stripe metering still works — you just map Better Auth's user ID to a kit API key

Code example

example.py
// Frontend: after Better Auth sign-in, mint a kit API key
const session = await auth.api.getSession({ headers });
const res = await fetch("/internal/api-keys/issue", {
  method: "POST",
  body: JSON.stringify({ external_user_id: session.user.id }),
});
const { api_key } = await res.json();
// Use api_key for subsequent calls to the FastAPI AI Kit backend

Full documentation: /docs/authentication

Better Auth integration pre-wired and ready.

FastAPI AI Kit ships with Better Auth configured out of the box. No manual setup required.

Ready to ship your AI backend this weekend?

Join developers who skipped weeks of boilerplate and went straight to building.

Read the docs
No subscriptions · One-time payment · Lifetime updates