---
name: "fastapi-dependency-injection-security"
description: "Build modular, secure FastAPI endpoints using Depends() and OAuth2 password bearer schemes."
metadata:
  author: "Tiangolo"
  version: "1.0.0"
  runtime_compatibility:
    - "codex"
    - "claude-code"
    - "cursor"
    - "windsurf"
triggers:
  - "fastapi dependency injection depends"
  - "oauth2passwordbearer fastapi"
  - "database session dependency fastapi"
permissions:
  fs_read: true
  fs_write: "workspace"
  shell_exec: false
  net_access: false
---

# FastAPI Dependency Injection & Security OAuth2 Schemes

Architects FastAPI backends: creating hierarchical dependency trees with Depends(), OAuth2PasswordBearer authentication, database session scoping with yield generator dependencies, and custom security scopes.

## When to Invoke
Invoke this skill whenever you need to:
- fastapi dependency injection depends
- oauth2passwordbearer fastapi
- database session dependency fastapi

## Execution SOP & Rules
1. Define session dependency: async def get_db(): async with SessionLocal() as session: yield session.
2. Define auth dependency: async def get_current_user(token: str = Depends(oauth2_scheme), db = Depends(get_db)): ...
3. Use in route: @app.get('/users/me') async def read_me(current_user: User = Depends(get_current_user)): return current_user.
4. Leverage dependency overrides (app.dependency_overrides) for isolated mock testing in pytest.

## Verification Checklist
- Run local linting, syntax validation, and type-checks before reporting completion.
- Ensure all created or modified files adhere to workspace boundaries and avoid uncommitted secrets.
- Verify tests or build commands exit cleanly with return code 0.
