fastapi-patterns

Applies FastAPI router, service, and repository patterns to VSmartwatch Python backend code.

Updated Mar 3, 2026
One-click install
npx skills add https://github.com/manhthien2005/PM_REVIEW --skill fastapi-patterns-manhthien2005
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: fastapi-patterns
Source: https://github.com/manhthien2005/PM_REVIEW/tree/main/tooling/.windsurf-template/shared/skills/fastapi-patterns
Command: npx skills add https://github.com/manhthien2005/PM_REVIEW --skill fastapi-patterns-manhthien2005

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing or modifying FastAPI backend code across the VSmartwatch services (health_system/backend, healthguard-model-api, Iot_Simulator_clean/api_server) risks inconsistent layering, leaked error details, and security mistakes like hardcoded secrets or wildcard CORS. This Skill enforces a single, consistent architecture so every endpoint follows the same router/service/repository pattern. ## Core Features & Use Cases - Layered Architecture Templates: Provides ready-to-use code for routers, services, repositories, Pydantic v2 schemas, dependencies, and pydantic-settings config. - Security Guardrails: Enforces internal-secret and JWT auth dependencies, sanitized error responses that never leak exception details, explicit CORS allowlists, and parameterized SQL queries. - Anti-Pattern Detection: Auto-flags mutable default arguments, swallowed exceptions, sync I/O in async functions, Pydantic v1 syntax, and PHI in logs. - Use Case: When adding a new fall-detection prediction endpoint to healthguard-model-api, use this Skill to scaffold the router with response_model validation, inject the service via Depends, and wire custom exception handlers that return sanitized error payloads. ## Quick Start Use the fastapi-patterns skill to add a new vitals endpoint to the health_system backend following the router, service, and repository layering.

Frequently Asked Questions about fastapi-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure a FastAPI project with routers, services, and repositories?▼

Place endpoints in routers/ (one file per domain), business logic in services/, and database queries in repositories/. Inject services into route handlers with Depends, always declare a response_model, and keep services free of HTTP objects like Request or JSONResponse.

How do I migrate Pydantic v1 schemas to Pydantic v2 in FastAPI?▼

Replace @validator with @field_validator and class Config with model_config = ConfigDict(...). Use ConfigDict(extra="forbid") to reject unknown fields and Field constraints like min_length and ge/le for validation.

How do I hide internal error details from FastAPI API responses?▼

Define custom exception classes with a public_message and register exception handlers that return only a generic code and message to clients. Log the full exception server-side with logger.exception, and never include str(exc) in the JSON response.

Does FastAPI support JWT and internal-secret authentication together?▼

Yes, implement both as FastAPI dependencies. Use a Header-based dependency to verify an X-Internal-Secret for service-to-service calls, and a separate dependency that decodes Bearer JWT tokens for mobile-facing user endpoints.

Why is sync I/O a problem inside FastAPI async endpoints?▼

Blocking calls inside an async function stall the event loop and degrade all concurrent requests. Wrap blocking work with await asyncio.to_thread(blocking_call) or define the endpoint as a regular sync def so FastAPI runs it in a threadpool.

What are common FastAPI security mistakes to avoid?▼

Avoid wildcard CORS origins in production, hardcoded secrets instead of pydantic-settings env config, f-string SQL instead of parameterized queries, and logging raw PHI or user data. Each of these is flagged as an anti-pattern in this Skill.