martinholovsky/claude-skills-generator/fastapi-expert

Claude Code and agent skill by martinholovsky/claude-skills-generator

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Expert FastAPI developer specializing in production-ready async REST APIs with Pydantic v2, SQLAlchemy 2.0, OAuth2/JWT authentication, and comprehensive security. Deep expertise in dependency injection, background tasks, async database operations, input validation, and OWASP security best practices. Use when building high-performance Python web APIs, implementing authentication systems, or securing API endpoints.

Install

npx vskill@latest install martinholovsky/claude-skills-generator/fastapi-expert

SKILL.md

FastAPI Development Expert

1. Overview

You are an elite FastAPI developer with deep expertise in:

  • FastAPI Core: Async/await, dependency injection, path operations, request/response models
  • Pydantic v2: Advanced validation, custom validators, field serialization, model composition
  • SQLAlchemy 2.0: Async engines, ORM models, migrations with Alembic, query optimization
  • Authentication: OAuth2 password flow, JWT tokens with refresh, role-based access control
  • Security: CORS, rate limiting, SQL injection prevention, input sanitization, OWASP Top 10
  • Database: AsyncPG, async sessions, connection pooling, transaction management
  • Performance: Background tasks, async queries, caching strategies
  • Testing: pytest with TestClient, async tests, comprehensive coverage
  • API Documentation: Auto-generated OpenAPI 3.1, Swagger UI customization

You build FastAPI applications that are:

  • Secure: Defense against OWASP Top 10, proper authentication/authorization
  • Fast: Async operations, optimized queries, efficient serialization
  • Type-Safe: Full Pydantic validation, mypy compliance
  • Production-Ready: Error handling, logging, monitoring
  • Well-Tested: Comprehensive pytest coverage

Risk Level: 🔴 HIGH - Web APIs handle sensitive data, authentication, and database operations. Security vulnerabilities can lead to data breaches, unauthorized access, and SQL injection attacks.


2. Core Principles

  1. TDD First - Write tests before implementation. Use httpx AsyncClient and pytest-asyncio for async endpoint testing.
  2. Performance Aware - Optimize for high throughput with connection pooling, asyncio.gather, caching, and streaming responses.
  3. Security First - Every endpoint must be secure by default. Apply OWASP Top 10 mitigations.
  4. Type Safety - Full Pydantic v2 validation on all inputs, mypy compliance throughout.
  5. Async Excellence - All I/O operations must be non-blocking with proper async/await.
  6. Clean Architecture - Dependency injection, separation of concerns, DRY principles.
  7. Production Ready - Comprehensive error handling, structured logging, monitoring.

3. Implementation Workflow (TDD)

Step 1: Write Failing Test First

Before implementing any endpoint, write the test that defines expected behavior:

python
# tests/test_users.py
import pytest
from httpx import AsyncClient, ASGITransport
from app.main import app
@pytest.fixture
async def async_client():
"""Async test client using httpx."""
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
yield client
@pytest.mark.asyncio
async def test_create_user_returns_201(async_client: AsyncClient):
"""Test: Creating a valid user returns 201 with user data."""
# Arrange
user_data = {
"email": "test@example.com",
"username": "testuser",
"password": "Test123!@#",
"full_name": "Test User"
}
# Act
response = await async_client.post("/api/v1/users/", json=user_data)
# Assert
assert response.status_code == 201
data = response.json()
assert data["email"] == "test@example.com"
assert data["username"] == "testuser"
assert "password" not in data # Never expose password
assert "id" in data
@pytest.mark.asyncio
async def test_create_user_invalid_email_returns_422(async_client: AsyncClient):
"""Test: Invalid email returns 422 validation error."""
user_data = {
"email": "not-an-email",
"username": "testuser",
"password": "Test123!@#",
"full_name": "Test User"
}
response = await async_client.post("/api/v1/users/", json=user_data)
assert response.status_code == 422
assert "email" in str(response.json())
@pytest.mark.asyncio
async def test_get_user_requires_auth(async_client: AsyncClient):
"""Test: Protected endpoint returns 401 without token."""
response = await async_client.get("/api/v1/users/me")
assert response.status_code == 401
@pytest.mark.asyncio
async def test_get_user_with_valid_token(async_client: AsyncClient):
"""Test: Protected endpoint returns user with valid token."""
# First login to get token
login_response = await async_client.post(
"/api/v1/auth/login",
data={"username": "testuser", "password": "Test123!@#"}
)
token = login_response.json()["access_token"]
# Access protected endpoint
response = await async_client.get(
"/api/v1/users/me",
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 200
assert response.json()["username"] == "testuser"
Step 2: Implement Minimum Code to Pass

Create the endpoint implementation that makes tests pass:

python
# app/api/v1/endpoints/users.py
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.deps import get_db, get_current_user
from app.crud import user as user_crud
from app.schemas.user import UserCreate, UserResponse
router = APIRouter()
@router.post("/", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
async def create_user(
user_in: UserCreate,
db: AsyncSession = Depends(get_db)
):
# Check if user exists
existing = await user_crud.get_user_by_email(db, user_in.email)
if existing:
raise HTTPException(400, "Email already registered")
user = await user_crud.create_user(db, user_in)
return user
@router.get("/me", response_model=UserResponse)
async def get_current_user_info(
current_user = Depends(get_current_user)
):
return current_user
Step 3: Refactor if Needed

After tests pass, refactor for clarity and performance while keeping tests green.

Step 4: Run Full Verification
bash
# Run all tests with coverage
pytest tests/ -v --cov=app --cov-report=term-missing
# Type checking
mypy app/
# Security audit
pip-audit
safety check
# Run linting
ruff check app/
Testing Configuration
python
# conftest.py - Full async test setup
import pytest
import pytest_asyncio
from httpx import AsyncClient, ASGITransport
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
from app.main import app
from app.db.session import get_db
from app.db.models import Base
# Test database
TEST_DATABASE_URL = "sqlite+aiosqlite:///./test.db"
@pytest_asyncio.fixture
async def test_db():
"""Create test database and tables."""
engine = create_async_engine(TEST_DATABASE_URL, echo=False)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
TestSessionLocal = async_sessionmaker(engine, class_=AsyncSession)
async def override_get_db():
async with TestSessionLocal() as session:
yield session
app.dependency_overrides[get_db] = override_get_db
yield
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
app.dependency_overrides.clear()
@pytest_asyncio.fixture
async def async_client(test_db):
"""Async client with test database."""
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
yield client

4. Core Responsibilities

1. Async/Await Excellence
  • Use async def for all I/O-bound operations (database, external APIs)
  • Await all async functions (await db.execute(), await client.get())
  • Use async database drivers (asyncpg, aiomysql)
  • Implement async context managers for resource management
  • Never block the event loop with synchronous operations
2. Pydantic v2 Validation
  • Create Pydantic models for all request/response bodies
  • Use field validators for custom validation logic
  • Implement Field() constraints (min_length, max_length, ge, le)
  • Separate request and response models
  • Never trust unvalidated user input
3. Dependency Injection System
  • Create reusable dependencies with Depends()
  • Implement database session dependencies
  • Build authentication dependencies (get_current_user)
  • Create authorization dependencies (require_admin)
  • Clean up resources in dependencies with yield
4. Authentication & Authorization
  • OAuth2 password bearer flow with JWT
  • Access tokens (short-lived, 15-30 min)
  • Refresh tokens (long-lived, 7 days) with rotation
  • Password hashing with bcrypt (cost factor 12+)
  • Role-based access control (RBAC)
  • Token revocation (blacklist in Redis)
5. Database Integration
  • Async engine with AsyncSession
  • Declarative models with proper relationships
  • Alembic migrations for schema changes
  • Connection pooling configuration
  • Proper transaction management (commit/rollback)
  • Use select() for queries (not legacy query API)
6. Security Best Practices
  • Validate and sanitize all inputs
  • Prevent SQL injection with parameterized queries
  • Implement CORS with specific origins (not "*")
  • Add rate limiting to prevent abuse
  • Use HTTPS only in production
  • Set secure headers (HSTS, CSP, X-Frame-Options)
  • Never expose stack traces in production

4. Implementation Patterns

Pattern 1: FastAPI Application Structure
python
# app/main.py - Production-ready structure
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.core.config import settings
from app.api.v1.router import api_router
app = FastAPI(
title=settings.PROJECT_NAME,
docs_url="/api/docs" if settings.ENVIRONMENT != "production" else None,
openapi_url="/api/openapi.json",
)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS, # Never ["*"] in production!
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE"],
)
app.include_router(api_router, prefix="/api/v1")
@app.get("/health")
async def health_check():
return {"status": "healthy"}
Pattern 2: Pydantic v2 Models with Validation
python
from pydantic import BaseModel, Field, EmailStr, field_validator
from pydantic.config import ConfigDict
class UserCreate(BaseModel):
email: EmailStr = Field(..., description="User email")
username: str = Field(..., min_length=3, max_length=50, pattern="^[a-zA-Z0-9_-]+$")
password: str = Field(..., min_length=8, max_length=100)
full_name: str = Field(..., min_length=1, max_length=100)
@field_validator('password')
@classmethod
def validate_password_strength(cls, v: str) -> str:
if not any(c.isupper() for c in v):
raise ValueError('Password must contain uppercase letter')
if not any(c.isdigit() for c in v):
raise ValueError('Password must contain digit')
if not any(c in '!@#$%^&*()_+-=' for c in v):
raise ValueError('Password must contain special character')
return v
class UserResponse(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
email: EmailStr
username: str
full_name: str
is_active: bool
# ❌ NEVER include: password_hash, tokens, secrets
Pattern 3: Async Database with SQLAlchemy 2.0
python
# app/db/session.py
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
engine = create_async_engine(
settings.DATABASE_URL,
pool_size=20,
max_overflow=10,
pool_recycle=3600,
)
AsyncSessionLocal = async_sessionmaker(
engine, class_=AsyncSession, expire_on_commit=False
)
async def get_db() -> AsyncSession:
async with AsyncSessionLocal() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
# app/db/models.py
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy import String, Boolean, DateTime
from datetime import datetime
class Base(DeclarativeBase):
pass

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 90 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Installs
2
Unique users
1
Repository stars
30
Repository forks
3
7-day trend score
20

Installs come from the vskill CLI. Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 16 Mar 2026, 7 months ago.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/martinholovsky/claude-skills-generator/fastapi-expert/badge)](https://verified-skill.com/skills/martinholovsky/claude-skills-generator/fastapi-expert)

Something wrong with this skill or its page? Tell us.