affaan-m avatar

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-code

Installs 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
1aws avataraws-blocksaws/agent-toolkit-for-awsGuides building full-stack applications with AWS Blocks — an Infrastructure-from-Code framework. Applies when creating APIs, selecting Building Blocks (KVStore,5.9Kskills.sh installs
2okx avatarokx-dex-wsokx/onchainos-skillsUse this skill when the user mentions 'onchainos ws', 'ws start', 'ws poll', 'ws stop', 'ws channels', 'ws session', 'ws channel-info', 'idle-timeout', 'idle ti5.8Kskills.sh installs
3qianwen-ai avatarqianwen-usageqianwen-ai/qianwen-ai[QianWen] Manage account auth and query usage/billing. Use for: login, logout, check usage, view billing, free tier quota, Token Plan status, pay-as-you-go cost5.8Kskills.sh installs
4affaan-m avatardjango-tddaffaan-m/eccDjango testing strategies with pytest-django, TDD methodology, factory_boy, mocking, coverage, and testing Django REST Framework APIs.5.8Kskills.sh installs
5pexoai avatarvideoagent-image-studiopexoai/pexo-skillsTired of juggling 8 API keys? This skill gives you one-command access to Midjourney, Flux, Ideogram, and more, with zero setup. Use when you want to generate an5.6Kskills.sh installs
6forcedotcom avatarintegration-connectivity-generateforcedotcom/sf-skillsSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Serv5.5Kskills.sh installs