fastapi-pro
Skill72 starsChanged 7 months agoMITView on GitHub
- Reads credentials
6 other projects use a version of this.
Adopted by
Most recently taken up by
- MortuisVMain/ai-agent-superpowerstheir own version, first seen Sep 15, 2026
- TheHiddenDeveloper/jarvis-skillstheir own version, first seen Aug 19, 2026
- andrewthetechie/andrew-skillstheir own version, first seen Jul 20, 2026
- ai4brands-design/claude-skillstheir own version, first seen Mar 22, 2026
- FutureJJ/claude-skillstheir own version, first seen Mar 7, 2026
- xfstudio/skillstheir own version, first seen Jan 22, 2026
Other versions6 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 6
- xfstudio/skills★ 9same name
- andrewthetechie/andrew-skills★ 4same name
- FutureJJ/claude-skills★ 3same name
- ai4brands-design/claude-skills★ 2same name
- MortuisVMain/ai-agent-superpowers★ 0same name
- TheHiddenDeveloper/jarvis-skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository117 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- agent-healthSkill
- agent-styleSkill
- angular-best-practicesSkill
- annotateSkill
- api-designSkill
- architecture-decision-recordsSkill
- aws-serverlessSkill
- backend-architectSkill
- backend-patternsSkill
- brainstormSkill
- bug-reportSkill
- changelogSkill
- claude-apiSkill
- cloud-architectSkill
- cloud-run-puppeteerSkill
- code-review-checklistSkill
- code-reviewSkill
- code-simplificationSkill
- codex-sddSkill
- commitSkill
- context-engineeringSkill
- database-architectSkill
- db-reviewSkill
- deep-interviewSkill
- design-reviewSkill
- design-systemSkill
- devops-deploySkill
- diagnoseSkill
- django-patternsSkill
- docker-patternsSkill
- dotnet-backend-patternsSkill
- dreamSkill
- drizzle-orm-expertSkill
- estimateSkill
- event-sourcing-architectSkill
- fork-joinSkill
- freezeSkill
- frontend-designSkill
- frontend-patternsSkill
- frontend-ui-dark-tsSkill
- gate-checkSkill
- gemini-api-integrationSkill
- gitlab-ci-patternsSkill
- guardSkill
- handoffSkill
- hotfixSkill
- hybrid-cloud-architectSkill
- kubernetes-architectSkill
- laravel-patternsSkill
- launch-checklistSkill
- learnerSkill
- llm-app-patternsSkill
- localizeSkill
- map-systemsSkill
- map-workflowSkill
- markdown-injection-scannerSkill
- microservices-patternsSkill
- milestone-reviewSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
2 rules. 0 are common, 0 are distinctive, and 2 are very specific to this project.
0
Common
Most of the 6 edited versions we read keep these.
0
Distinctive
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 19
SQLAlchemy 2.0 async session must not leak across requests: always scope via Depends with async with AsyncSession(...) — raw module-level …
- Very specific: line 294
Add prometheus-fastapi-instrumentator for /metrics.
“Common” is measured: kept by at least 6 in 10 of the 6 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
FastAPI Production Patterns
Critical rules (non-obvious)
async defendpoint blocking sync DB call → blocks entire event loop. Either useasyncDB driver (asyncpg/aiomysql) throughout OR switch endpoint to plaindef(FastAPI runs it in threadpool).- Pydantic V2
model_config = ConfigDict(...)replaces V1class Config. Forgetting this silently loses settings likefrom_attributes=Trueneeded for ORM → DTO conversion. Depends()caches per-request: same dependency called twice in one request returns same instance. Don't rely on this for cross-request state — use app state / Redis instead.- SQLAlchemy 2.0 async session must not leak across requests: always scope via
Dependswithasync with AsyncSession(...)— raw module-level session causesGreenletErrorunder load. BackgroundTasksruns AFTER response sent in the same worker process: if worker dies mid-task the work is lost. For durable background jobs use Celery / Dramatiq / ARQ.- Uvicorn
--workers Nforks processes — can't share in-memory state. Use Redis or DB for any shared state (rate-limit counters, cache).
Project layout
app/
├── main.py # FastAPI() instance + lifespan
├── api/
│ ├── deps.py # shared Depends (get_db, get_current_user)
│ └── v1/
│ ├── users.py # APIRouter
│ └── products.py
├── core/
│ ├── config.py # Pydantic Settings
│ ├── security.py # JWT encode/decode, password hashing
│ └── db.py # engine + AsyncSession factory
├── models/ # SQLAlchemy ORM models
├── schemas/ # Pydantic DTOs (Request/Response)
├── services/ # business logic (no framework coupling)
└── tests/Pydantic V2 settings + config
# app/core/config.py
from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env", env_prefix="APP_")
database_url: str = Field(..., description="postgresql+asyncpg://...")
jwt_secret: str = Field(..., min_length=32)
jwt_algorithm: str = "HS256"
jwt_exp_minutes: int = 30
cors_origins: list[str] = Field(default_factory=list)
settings = Settings() # fails fast at import if required vars missingSQLAlchemy 2.0 async session (per-request)
# app/core/db.py
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
engine = create_async_engine(
settings.database_url,
pool_size=20,
max_overflow=10,
pool_pre_ping=True, # reconnect on stale conns (LB idle timeout)
echo=False,
)
SessionLocal = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
# app/api/deps.py
from typing import AsyncIterator
from fastapi import Depends
async def get_db() -> AsyncIterator[AsyncSession]:
async with SessionLocal() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
# close is automatic via `async with`Lifespan + startup/shutdown
# app/main.py
from contextlib import asynccontextmanager
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup: warm caches, test DB
async with engine.begin() as conn:
await conn.execute(text("SELECT 1"))
yield
# Shutdown: drain connections
await engine.dispose()
app = FastAPI(title="My API", lifespan=lifespan)JWT auth with OAuth2PasswordBearer
# app/core/security.py
from datetime import datetime, timedelta, timezone
from jose import jwt, JWTError
from passlib.context import CryptContext
pwd = CryptContext(schemes=["bcrypt"], deprecated="auto")
def hash_password(p: str) -> str: return pwd.hash(p)
def verify_password(p: str, h: str) -> bool: return pwd.verify(p, h)
def create_access_token(sub: str) -> str:
exp = datetime.now(timezone.utc) + timedelta(minutes=settings.jwt_exp_minutes)
return jwt.encode({"sub": sub, "exp": exp}, settings.jwt_secret, settings.jwt_algorithm)
# app/api/deps.py
from fastapi import HTTPException, status
from fastapi.security import OAuth2PasswordBearer
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/auth/login")
async def get_current_user(
token: str = Depends(oauth2_scheme),
db: AsyncSession = Depends(get_db),
) -> User:
try:
payload = jwt.decode(token, settings.jwt_secret, algorithms=[settings.jwt_algorithm])
user_id: str = payload.get("sub")
except JWTError:
raise HTTPException(status.HTTP_401_UNAUTHORIZED, "Invalid token")
user = await db.get(User, user_id)
if not user:
raise HTTPException(status.HTTP_401_UNAUTHORIZED, "User not found")
return userEndpoint pattern (thin controller, service below)
# app/api/v1/users.py
from fastapi import APIRouter, Depends, HTTPException, status
router = APIRouter(prefix="/users", tags=["users"])
@router.post("", response_model=UserOut, status_code=status.HTTP_201_CREATED)
async def create_user(
payload: UserCreate,
db: AsyncSession = Depends(get_db),
) -> UserOut:
try:
user = await user_service.create(db, payload)
except DuplicateEmailError:
raise HTTPException(status.HTTP_409_CONFLICT, "Email taken")
return UserOut.model_validate(user) # V2 from-attributes
@router.get("/{user_id}", response_model=UserOut)
async def get_user(
user_id: int,
db: AsyncSession = Depends(get_db),
current: User = Depends(get_current_user),
) -> UserOut:
user = await db.get(User, user_id)
if not user:
raise HTTPException(status.HTTP_404_NOT_FOUND)
if user.id != current.id and not current.is_admin:
raise HTTPException(status.HTTP_403_FORBIDDEN)
return UserOut.model_validate(user)Pydantic V2 schemas with from_attributes
from pydantic import BaseModel, ConfigDict, EmailStr, Field
class UserBase(BaseModel):
model_config = ConfigDict(from_attributes=True)
email: EmailStr
full_name: str = Field(min_length=1, max_length=100)
class UserCreate(UserBase):
password: str = Field(min_length=8, max_length=128)
class UserOut(UserBase):
id: int
created_at: datetimeGlobal exception handler
# app/main.py
from fastapi import Request
from fastapi.responses import JSONResponse
class AppError(Exception):
def __init__(self, msg: str, status_code: int = 400):
self.msg, self.status_code = msg, status_code
@app.exception_handler(AppError)
async def app_error_handler(req: Request, exc: AppError):
return JSONResponse(status_code=exc.status_code, content={"error": exc.msg})
@app.exception_handler(Exception)
async def unhandled(req: Request, exc: Exception):
logger.exception("Unhandled error", extra={"path": req.url.path})
return JSONResponse(status_code=500, content={"error": "Internal server error"})Testing with pytest-asyncio
# tests/conftest.py
import pytest_asyncio
from httpx import AsyncClient, ASGITransport
@pytest_asyncio.fixture
async def client():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
yield c
@pytest_asyncio.fixture
async def db_session():
async with SessionLocal() as s:
yield s
await s.rollback() # isolate test
# tests/test_users.py
@pytest.mark.asyncio
async def test_create_user(client: AsyncClient):
r = await client.post("/api/v1/users", json={"email": "a@b.com", "full_name": "A", "password": "pw12345678"})
assert r.status_code == 201
assert r.json()["email"] == "a@b.com"Production deployment (Uvicorn + Gunicorn)
# Dockerfile CMD — recommended for production
gunicorn app.main:app \
--workers 4 \
--worker-class uvicorn.workers.UvicornWorker \
--bind 0.0.0.0:8000 \
--timeout 60 \
--keep-alive 5 \
--access-logfile -Workers = (2 × CPU) + 1 for CPU-bound; lower for IO-heavy async (async workers share event loop already).
Common pitfalls
| Pitfall | Fix |
async def + sync psycopg2/pymysql | Use asyncpg / aiomysql OR drop async on endpoint |
BaseSettings V1 pattern (class Config) | V2 uses model_config = SettingsConfigDict(...) |
from_attributes=True missing → validation error from ORM instance | Add to ConfigDict on every DTO reading from ORM |
Forgetting await on SQLAlchemy 2.0 async query | Linter — use sqlalchemy[asyncio] type stubs + mypy |
| Returning ORM object → leaks relationships (N+1 on serialize) | Always model_validate(orm_obj) to scoped Pydantic DTO |
BackgroundTasks for durable work | Switch to Celery / Dramatiq / ARQ |
CORSMiddleware added AFTER auth middleware | CORS must be OUTERMOST — added first |
response_model + return extra fields → silently stripped | Use response_model_exclude_unset=True consciously |
Observability hooks
# Structured logging
import structlog
logger = structlog.get_logger()
@app.middleware("http")
async def log_requests(request: Request, call_next):
start = time.monotonic()
response = await call_next(request)
logger.info("http_request",
method=request.method, path=request.url.path,
status=response.status_code,
duration_ms=(time.monotonic() - start) * 1000,
)
return response- Add
prometheus-fastapi-instrumentatorfor/metrics. - Health check: GET
/healthzreturns{"status": "ok"}+ DBSELECT 1. - Request ID:
X-Request-IDheader middleware → bind to contextvars for logging.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

