Claude-skill-registry fastapi-patterns
Advanced FastAPI patterns including hierarchical dependency injection, background task management, and type-safe dependency annotation. Triggers: fastapi, dependency-injection, background-tasks, annotated-dependency, permission-chain.
install
source · Clone the upstream repo
git clone https://github.com/majiayu000/claude-skill-registry
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/majiayu000/claude-skill-registry "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/data/fastapi-patterns" ~/.claude/skills/majiayu000-claude-skill-registry-fastapi-patterns && rm -rf "$T"
manifest:
skills/data/fastapi-patterns/SKILL.mdsource content
FastAPI Advanced Patterns
Overview
FastAPI leverages Python's type hints and async capabilities to provide a robust framework for building APIs. Its core strength lies in its Dependency Injection system, which allows for clean, reusable, and hierarchical code structure.
When to Use
- Shared Logic: When multiple endpoints need the same authentication, database session, or pagination logic.
- Offloading I/O: When a request triggers a slow operation (like sending an email) that shouldn't block the user response.
- Complex Security: When you need multi-level permission checks (e.g., User -> Active User -> Admin).
Decision Tree
- Do you need the same logic in 3+ endpoints?
- YES: Create a reusable Dependency.
- Does an operation take more than 50ms and doesn't return data to the user?
- YES: Use
.BackgroundTasks
- YES: Use
- Do you want full IDE autocompletion for injected values?
- YES: Use
syntax.Annotated[Type, Depends(func)]
- YES: Use
Workflows
1. Implementing Reusable Shared Logic
- Define a dependency function that extracts common parameters (e.g., pagination or auth).
- Create a type alias using
.Annotated[Type, Depends(dependency_function)] - Inject this alias into multiple path operation functions to access the shared logic results.
2. Offloading Tasks to the Background
- Define a standard Python function for the slow task (e.g., sending an email).
- Include
as a parameter in the API endpoint.background_tasks: BackgroundTasks - Call
inside the endpoint.background_tasks.add_task(task_function, *args) - Return a response immediately to the user while the task runs in the background.
3. Hierarchical Permission Checks
- Create a base dependency for
.get_current_user - Create a sub-dependency
that depends onget_active_user
.get_current_user - Create a final
that depends onget_admin_user
.get_active_user - FastAPI will execute the chain in order and stop if any dependency raises an HTTPException.
Non-Obvious Insights
- Dependency Tree Resolution: FastAPI resolves a full tree of dependencies automatically; if three different dependencies all require the same database session, FastAPI can be configured to call the session creator only once per request.
- Annotated is Best Practice: Using
keeps your code DRY (Don't Repeat Yourself) and ensures that static analysis tools understand exactly what type is being injected.Annotated - Flexible Background Tasks:
can be declared anywhere in the dependency chain, not just in the final endpoint function.BackgroundTasks
Evidence
- "Dependency Injection means... that there is a way for your code to declare things that it requires to work and use." - FastAPI Docs
- "BackgroundTasks is useful for operations that need to happen after a request... client doesn't really have to be waiting." - FastAPI Docs
- "Annotated dependencies... type information will be preserved, which means your editor will keep providing autocompletion." - FastAPI Docs
Scripts
: Example of Dependency Injection and Background Tasks.scripts/fastapi-patterns_tool.py
: (Simulated) Pattern comparison for Node.js middleware.scripts/fastapi-patterns_tool.js
Dependencies
fastapipydantic