backend-fastapi-python
npx machina-cli add skill Vibe-Builders/claude-prime/backend-fastapi-python --openclawBackend FastAPI Python
Conventions for FastAPI applications using SQLModel, pydantic-settings, and async SQLAlchemy.
Core Principles (See detailed in references)
Key Conventions
- Services are stateless functions - Not classes. First param is
db: AsyncSession - Generic response wrapper - Always use
ApiResponse[T]for consistency - Dependencies chain -
get_current_user→require_auth→require_admin - Module-scoped config - Each module can have its own
{module}_config.py - Error codes for frontend -
AppException(status, message, error_code)
References
| Topic | Reference |
|---|---|
| Project Layout | file-structure.md |
| Configuration | configuration.md |
| Database | database.md |
| Models | models.md |
| Schemas | schemas.md |
| Routing | routing.md |
| Services | services.md |
| Dependencies | dependencies.md |
| Middleware | middleware.md |
| Error Handling | error-handling.md |
| Auth (Example) | auth.md |
Source
git clone https://github.com/Vibe-Builders/claude-prime/blob/main/.claude/starter-skills/backend-fastapi-python/SKILL.mdView on GitHub Overview
This skill codifies FastAPI conventions using SQLModel, pydantic-settings, and async SQLAlchemy to build robust REST backends. It emphasizes stateless services, a unified ApiResponse wrapper, a dependency chain for authentication, module-scoped configs, and standardized error handling with AppException.
How This Skill Works
Functions are implemented as stateless services with the first parameter db: AsyncSession. Endpoints return ApiResponse[T] for consistent responses, and authentication is wired through a dependency chain: get_current_user → require_auth → require_admin. Each module can declare its own {module}_config.py to encapsulate module-specific settings.
When to Use It
- Building REST endpoints that interact with an async database using SQLModel.
- Implementing authentication and admin-restricted routes via a dependency chain.
- Creating a reusable service layer that is stateless and class-free.
- Enforcing a consistent response structure with ApiResponse[T].
- Organizing project structure with module-specific configuration files.
Quick Start
- Step 1: Create module_config.py for your feature/module and ensure you have an AsyncSession available.
- Step 2: Implement a stateless service function with signature (db: AsyncSession, ...).
- Step 3: Return ApiResponse[T] from endpoints and wire auth via get_current_user → require_auth → require_admin; raise AppException when needed.
Best Practices
- Write services as stateless functions with the first parameter db: AsyncSession.
- Always return results wrapped in ApiResponse[T] for uniformity.
- Use the dependency chain get_current_user → require_auth → require_admin for auth.
- Keep module-level configurations in {module}_config.py to isolate concerns.
- Handle errors with AppException(status, message, error_code) for frontend clarity.
Example Use Cases
- User service: stateless CRUD operations using AsyncSession with ApiResponse wrappers.
- Auth module: get_current_user → require_auth → require_admin gating sensitive endpoints.
- Product module: separate {product}_config.py containing feature toggles and DB settings.
- Error handling: endpoints raise AppException with specific status and error_code for frontend mapping.
- Routing: endpoints consistently return ApiResponse[T] across services and models.