# fastapi-templates > Architecture and best practices for FastAPI backend development - Author: alfcalo - Repository: alfcalo/cronos-app - Version: 20260204115155 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-06 - Source: https://github.com/alfcalo/cronos-app - Web: https://mule.run/skillshub/@@alfcalo/cronos-app~fastapi-templates:20260204115155 --- --- name: fastapi-templates description: Architecture and best practices for FastAPI backend development --- # FastAPI Templates & Best Practices Guidelines for building scalable, high-performance backends with FastAPI. ## Project Structure We follow a modular structure: ``` backend/ ├── app/ │ ├── api/ # Route handlers (v1, v2...) │ ├── core/ # Config, security, logging │ ├── db/ # Session management, migrations │ ├── models/ # SQLAlchemy/SQLModel entities │ ├── schemas/ # Pydantic models │ ├── services/ # Business logic │ └── main.py # App entry point ``` ## Core Practices 1. **Dependency Injection**: Use `Depends` for database sessions, authentication, and service instances. 2. **Async/Await**: Use `async def` for I/O bound operations (DB queries, API calls). 3. **Pydantic for Validation**: Use Pydantic models for request bodies and response data. 4. **Global Exception Handling**: Use middleware or custom exception handlers to ensure consistent error responses. ## DB Session Management ```python from sqlalchemy.ext.asyncio import AsyncSession from app.db.session import async_session_factory async def get_db() -> AsyncSession: async with async_session_factory() as session: yield session ``` ## Security - Use OAuth2 with Password Flow and JWT tokens. - Keep secret keys in environment variables. - Use `passlib` with `bcrypt` for password hashing.