FastAPI Guide

FastAPI Authentication

Integrate GitHat authentication into FastAPI apps. JWT validation, dependency injection auth guards, and async token management for Python APIs.

GitHat + FastAPI

Add enterprise-grade auth to FastAPI with GitHat's REST API. Use async HTTP calls to validate tokens, check org membership, and enforce role-based access on every request.

Auth Dependency

Build a reusable auth dependency: extract the Bearer token, validate it with GitHat, and return the user context. Apply it to any endpoint with Depends(get_current_user).

Async Token Validation

FastAPI is async-first. Use httpx.AsyncClient to validate tokens against GitHat without blocking the event loop. The /auth/me endpoint returns user + org context in one call.

Install

pip install fastapi httpx uvicorn

Example

# main.py
from fastapi import FastAPI, Depends, HTTPException, Header
import httpx

app = FastAPI()
GITHAT_API = 'https://api.githat.io'

async def get_current_user(authorization: str = Header()):
    async with httpx.AsyncClient() as client:
        resp = await client.get(f'{GITHAT_API}/auth/me',
            headers={'Authorization': authorization})
        if resp.status_code != 200:
            raise HTTPException(status_code=401)
        return resp.json()

@app.get('/api/profile')
async def profile(user=Depends(get_current_user)):
    return user
Try GitHat free

Ship authenticated apps in minutes, not weeks.