Agent skill
fastapi-patterns
Install this agent skill to your Project
npx add-skill https://github.com/cuba6112/skillfactory/tree/main/skills/fastapi-patterns
SKILL.md
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
Annotated[Type, Depends(func)]syntax.
- 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
background_tasks: BackgroundTasksas a parameter in the API endpoint. - Call
background_tasks.add_task(task_function, *args)inside the endpoint. - 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
get_active_userthat depends onget_current_user. - Create a final
get_admin_userthat depends onget_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
Annotatedkeeps your code DRY (Don't Repeat Yourself) and ensures that static analysis tools understand exactly what type is being injected. - Flexible Background Tasks:
BackgroundTaskscan be declared anywhere in the dependency chain, not just in the final endpoint function.
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
scripts/fastapi-patterns_tool.py: Example of Dependency Injection and Background Tasks.scripts/fastapi-patterns_tool.js: (Simulated) Pattern comparison for Node.js middleware.
Dependencies
fastapipydantic
References
- references/README.md
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
prompt-engineering
Comprehensive prompt engineering techniques for Claude models. Use this skill when crafting, optimizing, or debugging prompts for Claude API, Claude Code, or any Claude-powered application. Covers system prompts, role prompting, multishot examples, chain of thought, XML structuring, long context handling, extended thinking, prompt chaining, Claude 4.x-specific best practices, and agentic orchestration including subagents, agent loops, skills, MCP integration, and multi-agent workflows.
adk-rag-agent
Build RAG (Retrieval-Augmented Generation) agents with Google ADK and Vertex AI RAG Engine. Use when implementing document Q&A, knowledge base search, or citation-backed responses. Covers VertexAiRagRetrieval tool, corpus setup, and citation formatting.
headless-cli-agents
Build agentic systems using Claude CLI in headless mode or the Claude Agent SDK. Use when building automation pipelines, CI/CD integrations, multi-agent orchestration, or programmatic Claude interactions. Covers CLI flags (-p, --output-format), session management (--resume, --continue), Python SDK (claude-agent-sdk), custom tools, and agent loop patterns.
notion-knowledge-capture
Capture conversations and decisions into structured Notion pages; use when turning chats/notes into wiki entries, how-tos, decisions, or FAQs with proper linking.
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).
gh-fix-ci
Inspect GitHub PR checks with gh, pull failing GitHub Actions logs, summarize failure context, then create a fix plan and implement after user approval. Use when a user asks to debug or fix failing PR CI/CD checks on GitHub Actions and wants a plan + code changes; for external checks (e.g., Buildkite), only report the details URL and mark them out of scope.
Didn't find tool you were looking for?