
Fastapi Patterns
This is a comprehensive reference for structuring production FastAPI applications with modern Python patterns. It covers the full stack: app factories with lifespan management, pydantic-settings for configuration, proper dependency injection with type annotations, transactional service layers that separate business logic from routes, and SQLAlchemy async sessions. The code samples are complete and opinionated, showing real implementations of JWT auth, CORS middleware, Pydantic v2 validators, and pagination. Use this when you're past the tutorial phase and need to see how the pieces fit together in a maintainable codebase. The service layer pattern alone will save you from tangled route handlers.
- skills.sh installs
- 6K
- GitHub stars
- 300.3K
- last updated
- Aug 21, 2026
Install
$ npx -y skills add affaan-m/ecc --skill fastapi-patterns --agent claude-codeInstalls into .claude/skills of the current project.
Files
FastAPI Patterns
Modern, production-grade FastAPI development: project layout, Pydantic v2 schemas, dependency injection, async patterns, auth, transactional service methods, and testing.
Project Structure
my_app/
|-- app/
| |-- main.py # App factory, lifespan, middleware
| |-- config.py # Settings via pydantic-settings
| |-- dependencies.py # Shared FastAPI dependencies
| |-- database.py # SQLAlchemy engine + session
| |-- routers/
| | `-- users.py
| |-- models/ # SQLAlchemy ORM models
| | `-- user.py
| |-- schemas/ # Pydantic request/response schemas
| | `-- user.py
| `-- services/ # Business logic layer
| `-- user_service.py
|-- tests/
| |-- conftest.py
| `-- test_users.py
|-- pyproject.toml
`-- .env
App Factory and Lifespan
# app/main.py
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.config import settings
from app.database import engine, Base
from app.routers import users
Sponsors
Details
- Repository
- github.com/affaan-m/ecc
- Categories
- Backend & APIs, Testing & QA, Security, Python
- First seen
- Jul 14, 2026
More from affaan-m/ecc
All 263- Remotion Video Creation6K
- React Patterns5.9K
- Perl Security5.9K
- Kotlin Coroutines Flows5.9K
- React Performance5.9K
- Kotlin Patterns5.9K
- Architecture Decision Records5.9K
- Deployment Patterns5.8K
- React Testing5.8K
- Pytorch Patterns5.8K
- Hexagonal Architecture5.8K
- Flutter Dart Code Review5.8K
- Frontend A11y5.8K
- Error Handling5.8K
- Ui Demo5.8K
- Agentic Engineering5.8K
- Vite Patterns5.8K
- Bun Runtime5.8K
- Agent Architecture Audit5.8K
- Nodejs Keccak2565.8K
- Security Scan5.8K
- Cpp Testing5.8K
- Django Tdd5.8K
- Dashboard Builder5.7K