Get the FREE Ultimate OpenClaw Setup Guide →

backend-fastapi-python

npx machina-cli add skill Vibe-Builders/claude-prime/backend-fastapi-python --openclaw
Files (1)
SKILL.md
1.6 KB

Backend FastAPI Python

Conventions for FastAPI applications using SQLModel, pydantic-settings, and async SQLAlchemy.

Core Principles (See detailed in references)

Key Conventions

  1. Services are stateless functions - Not classes. First param is db: AsyncSession
  2. Generic response wrapper - Always use ApiResponse[T] for consistency
  3. Dependencies chain - get_current_userrequire_authrequire_admin
  4. Module-scoped config - Each module can have its own {module}_config.py
  5. Error codes for frontend - AppException(status, message, error_code)

References

TopicReference
Project Layoutfile-structure.md
Configurationconfiguration.md
Databasedatabase.md
Modelsmodels.md
Schemasschemas.md
Routingrouting.md
Servicesservices.md
Dependenciesdependencies.md
Middlewaremiddleware.md
Error Handlingerror-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

  1. Step 1: Create module_config.py for your feature/module and ensure you have an AsyncSession available.
  2. Step 2: Implement a stateless service function with signature (db: AsyncSession, ...).
  3. 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.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers