$npx -y skills add wshobson/agents --skill fastapi-templatesCreate production-ready FastAPI projects with async patterns, dependency injection, and comprehensive error handling. Use when building new FastAPI applications or setting up backend API projects.
| 1 | # FastAPI Project Templates |
| 2 | |
| 3 | Production-ready FastAPI project structures with async patterns, dependency injection, middleware, and best practices for building high-performance APIs. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Starting new FastAPI projects from scratch |
| 8 | - Implementing async REST APIs with Python |
| 9 | - Building high-performance web services and microservices |
| 10 | - Creating async applications with PostgreSQL, MongoDB |
| 11 | - Setting up API projects with proper structure and testing |
| 12 | |
| 13 | ## Core Concepts |
| 14 | |
| 15 | ### 1. Project Structure |
| 16 | |
| 17 | **Recommended Layout:** |
| 18 | |
| 19 | ``` |
| 20 | app/ |
| 21 | ├── api/ # API routes |
| 22 | │ ├── v1/ |
| 23 | │ │ ├── endpoints/ |
| 24 | │ │ │ ├── users.py |
| 25 | │ │ │ ├── auth.py |
| 26 | │ │ │ └── items.py |
| 27 | │ │ └── router.py |
| 28 | │ └── dependencies.py # Shared dependencies |
| 29 | ├── core/ # Core configuration |
| 30 | │ ├── config.py |
| 31 | │ ├── security.py |
| 32 | │ └── database.py |
| 33 | ├── models/ # Database models |
| 34 | │ ├── user.py |
| 35 | │ └── item.py |
| 36 | ├── schemas/ # Pydantic schemas |
| 37 | │ ├── user.py |
| 38 | │ └── item.py |
| 39 | ├── services/ # Business logic |
| 40 | │ ├── user_service.py |
| 41 | │ └── auth_service.py |
| 42 | ├── repositories/ # Data access |
| 43 | │ ├── user_repository.py |
| 44 | │ └── item_repository.py |
| 45 | └── main.py # Application entry |
| 46 | ``` |
| 47 | |
| 48 | ### 2. Dependency Injection |
| 49 | |
| 50 | FastAPI's built-in DI system using `Depends`: |
| 51 | |
| 52 | - Database session management |
| 53 | - Authentication/authorization |
| 54 | - Shared business logic |
| 55 | - Configuration injection |
| 56 | |
| 57 | ### 3. Async Patterns |
| 58 | |
| 59 | Proper async/await usage: |
| 60 | |
| 61 | - Async route handlers |
| 62 | - Async database operations |
| 63 | - Async background tasks |
| 64 | - Async middleware |
| 65 | |
| 66 | ## Detailed worked examples and patterns |
| 67 | |
| 68 | Detailed sections (starting with `## Implementation Patterns`) live in `references/details.md`. Read that file when the navigation summary above is insufficient. |
| 69 | |
| 70 | ## Testing |
| 71 | |
| 72 | ```python |
| 73 | # tests/conftest.py |
| 74 | import pytest |
| 75 | import asyncio |
| 76 | from httpx import AsyncClient |
| 77 | from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession |
| 78 | from sqlalchemy.orm import sessionmaker |
| 79 | |
| 80 | from app.main import app |
| 81 | from app.core.database import get_db, Base |
| 82 | |
| 83 | TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:" |
| 84 | |
| 85 | @pytest.fixture(scope="session") |
| 86 | def event_loop(): |
| 87 | loop = asyncio.get_event_loop_policy().new_event_loop() |
| 88 | yield loop |
| 89 | loop.close() |
| 90 | |
| 91 | @pytest.fixture |
| 92 | async def db_session(): |
| 93 | engine = create_async_engine(TEST_DATABASE_URL, echo=True) |
| 94 | async with engine.begin() as conn: |
| 95 | await conn.run_sync(Base.metadata.create_all) |
| 96 | |
| 97 | AsyncSessionLocal = sessionmaker( |
| 98 | engine, class_=AsyncSession, expire_on_commit=False |
| 99 | ) |
| 100 | |
| 101 | async with AsyncSessionLocal() as session: |
| 102 | yield session |
| 103 | |
| 104 | @pytest.fixture |
| 105 | async def client(db_session): |
| 106 | async def override_get_db(): |
| 107 | yield db_session |
| 108 | |
| 109 | app.dependency_overrides[get_db] = override_get_db |
| 110 | |
| 111 | async with AsyncClient(app=app, base_url="http://test") as client: |
| 112 | yield client |
| 113 | |
| 114 | # tests/test_users.py |
| 115 | import pytest |
| 116 | |
| 117 | @pytest.mark.asyncio |
| 118 | async def test_create_user(client): |
| 119 | response = await client.post( |
| 120 | "/api/v1/users/", |
| 121 | json={ |
| 122 | "email": "test@example.com", |
| 123 | "password": "testpass123", |
| 124 | "name": "Test User" |
| 125 | } |
| 126 | ) |
| 127 | assert response.status_code == 201 |
| 128 | data = response.json() |
| 129 | assert data["email"] == "test@example.com" |
| 130 | assert "id" in data |
| 131 | ``` |