Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Plan and build production-ready FastAPI endpoints with async SQLAlchemy, Pydantic v2 models, dependency injection for auth, and pytest tests. Uses interview-driven planning to clarify data models, authentication method, pagination strategy, and caching before writing any code.
.claude/skills/davila7-fastapi-endpoint/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-15 | ✗→✓ | ▲ Improved | 241% | 0% |
| case-03 | ✓→✗ | ▼ Worse | 273% | 0% |
| case-04 | ✓→✓ | = Same ✓ | 224% | 0% |
| case-05 | ✓→✓ | = Same ✓ | 157% | 0% |
| case-06 | ✓→✓ | = Same ✓ | 139% | 0% |
Use this skill when you need to:
Enter plan mode. Before writing any code, explore the existing project to understand:
main.py, app.py, or app/__init__.py)routers/ directory)models/, schemas/, crud/, or services/ directoriespyproject.toml or requirements.txt for installed dependenciesDepends(get_db), middleware, other){"data": ..., "meta": ...})tests/, test_*.py, *_test.py)Use AskUserQuestion to clarify requirements. Ask in rounds — do NOT dump all questions at once.
Question: "What resource does this endpoint manage?"
Header: "Resource"
Options:
- "New resource (I'll describe the fields)" — Creating a new data model from scratch
- "Existing model (extend it)" — Adding endpoints for a model that already exists in the codebase
- "Relationship endpoint (nested)" — e.g., /users/{id}/orders — endpoint on a related resource
Question: "Which HTTP methods do you need?"
Header: "Methods"
multiSelect: true
Options:
- "Full CRUD (GET list, GET detail, POST, PUT/PATCH, DELETE)" — All standard operations
- "Read-only (GET list + GET detail)" — No mutations
- "Custom action (POST /resource/{id}/action)" — Business logic endpoint, not standard CRUDQuestion: "What fields does the resource have? (describe briefly)"
Header: "Fields"
Options:
- "Simple (< 6 fields, basic types)" — Strings, ints, booleans, dates
- "Medium (6-15 fields, some relations)" — Includes foreign keys or enums
- "Complex (nested objects, polymorphic)" — JSON fields, discriminated unions, computed fieldsQuestion: "How should this endpoint be authenticated?"
Header: "Auth"
Options:
- "JWT Bearer token (Recommended)" — OAuth2PasswordBearer with JWT decode
- "API Key header" — X-API-Key header validation
- "No auth (public)" — Open endpoint, no authentication required
- "Use existing auth" — Reuse the auth dependency already in the project
Question: "Do you need role-based access control?"
Header: "RBAC"
Options:
- "No — any authenticated user" — Single permission level
- "Yes — role check (admin, user, etc.)" — Require specific roles per endpoint
- "Yes — ownership check" — Users can only access their own resourcesQuestion: "What pagination style for list endpoints?"
Header: "Pagination"
Options:
- "Cursor-based (Recommended)" — Best for real-time data, no offset drift
- "Offset/limit" — Simple, good for admin panels with page numbers
- "No pagination" — Small datasets, return all results
Question: "Do you need response caching?"
Header: "Caching"
Options:
- "No caching" — Fresh data on every request
- "Cache-Control headers" — Client-side caching via HTTP headers
- "Redis/in-memory cache" — Server-side caching with TTLWrite a concrete implementation plan covering:
Create, Update, Response, and List schemas with field typesPresent via ExitPlanMode for user approval.
After approval, implement following this order:
pythonfrom pydantic import BaseModel, ConfigDict from datetime import datetime from uuid import UUID class ResourceBase(BaseModel): """Shared fields between create and response.""" name: str # ... fields from interview class ResourceCreate(ResourceBase): """Fields required to create the resource.""" pass class ResourceUpdate(BaseModel): """All fields optional for partial updates.""" name: str | None = None class ResourceResponse(ResourceBase): """Full resource with DB-generated fields.""" model_config = ConfigDict(from_attributes=True) id: UUID created_at: datetime updated_at: datetime class ResourceListResponse(BaseModel): """Paginated list response.""" data: list[ResourceResponse] next_cursor: str | None = None has_more: bool
pythonfrom sqlalchemy import Column, String, DateTime, func from sqlalchemy.dialects.postgresql import UUID as PG_UUID import uuid from app.database import Base class Resource(Base): __tablename__ = "resources" id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) name = Column(String, nullable=False, index=True) created_at = Column(DateTime(timezone=True), server_default=func.now()) updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
pythonfrom sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select from uuid import UUID async def get_resource(db: AsyncSession, resource_id: UUID) -> Resource | None: result = await db.execute(select(Resource).where(Resource.id == resource_id)) return result.scalar_one_or_none() async def list_resources( db: AsyncSession, cursor: str | None = None, limit: int = 20, ) -> tuple[list[Resource], str | None]: query = select(Resource).order_by(Resource.created_at.desc()).limit(limit + 1) if cursor: query = query.where(Resource.created_at < decode_cursor(cursor)) result = await db.execute(query) items = list(result.scalars().all()) next_cursor = encode_cursor(items[-1].created_at) if len(items) > limit else None return items[:limit], next_cursor async def create_resource(db: AsyncSession, data: ResourceCreate) -> Resource: resource = Resource(**data.model_dump()) db.add(resource) await db.commit() await db.refresh(resource) return resource async def update_resource( db: AsyncSession, resource_id: UUID, data: ResourceUpdate ) -> Resource | None: resource = await get_resource(db, resource_id) if not resource: return None for field, value in data.model_dump(exclude_unset=True).items(): setattr(resource, field, value) await db.commit() await db.refresh(resource) return resource async def delete_resource(db: AsyncSession, resource_id: UUID) -> bool: resource = await get_resource(db, resource_id) if not resource: return False await db.delete(resource) await db.commit() return True
pythonfrom fastapi import APIRouter, Depends, HTTPException, Query, status from sqlalchemy.ext.asyncio import AsyncSession from uuid import UUID router = APIRouter(prefix="/resources", tags=["resources"]) @router.get("", response_model=ResourceListResponse) async def list_resources_endpoint( cursor: str | None = Query(None), limit: int = Query(20, ge=1, le=100), db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user), # if auth required ): items, next_cursor = await list_resources(db, cursor=cursor, limit=limit) return ResourceListResponse( data=items, next_cursor=next_cursor, has_more=next_cursor is not None, ) @router.get("/{resource_id}", response_model=ResourceResponse) async def get_resource_endpoint( resource_id: UUID, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user), ): resource = await get_resource(db, resource_id) if not resource: raise HTTPException(status_code=404, detail="Resource not found") return resource @router.post("", response_model=ResourceResponse, status_code=status.HTTP_201_CREATED) async def create_resource_endpoint( data: ResourceCreate, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user), ): return await create_resource(db, data) @router.patch("/{resource_id}", response_model=ResourceResponse) async def update_resource_endpoint( resource_id: UUID, data: ResourceUpdate, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user), ): resource = await update_resource(db, resource_id, data) if not resource: raise HTTPException(status_code=404, detail="Resource not found") return resource @router.delete("/{resource_id}", status_code=status.HTTP_204_NO_CONTENT) async def delete_resource_endpoint( resource_id: UUID, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user), ): deleted = await delete_resource(db, resource_id) if not deleted: raise HTTPException(status_code=404, detail="Resource not found")
pythonimport pytest from httpx import AsyncClient, ASGITransport from app.main import app @pytest.fixture async def client(): async with AsyncClient( transport=ASGITransport(app=app), base_url="http://test" ) as ac: yield ac @pytest.mark.asyncio async def test_create_resource(client: AsyncClient, auth_headers: dict): response = await client.post( "/resources", json={"name": "Test Resource"}, headers=auth_headers, ) assert response.status_code == 201 data = response.json() assert data["name"] == "Test Resource" assert "id" in data @pytest.mark.asyncio async def test_get_resource_not_found(client: AsyncClient, auth_headers: dict): response = await client.get( "/resources/00000000-0000-0000-0000-000000000000", headers=auth_headers, ) assert response.status_code == 404 @pytest.mark.asyncio async def test_list_resources_pagination(client: AsyncClient, auth_headers: dict): # Create multiple resources first for i in range(5): await client.post( "/resources", json={"name": f"Resource {i}"}, headers=auth_headers, ) response = await client.get("/resources?limit=2", headers=auth_headers) assert response.status_code == 200 data = response.json() assert len(data["data"]) == 2 assert data["has_more"] is True assert data["next_cursor"] is not None @pytest.mark.asyncio async def test_create_resource_unauthorized(client: AsyncClient): response = await client.post("/resources", json={"name": "Test"}) assert response.status_code in (401, 403) @pytest.mark.asyncio async def test_update_resource_partial(client: AsyncClient, auth_headers: dict): # Create create_resp = await client.post( "/resources", json={"name": "Original"}, headers=auth_headers, ) resource_id = create_resp.json()["id"] # Partial update response = await client.patch( f"/resources/{resource_id}", json={"name": "Updated"}, headers=auth_headers, ) assert response.status_code == 200 assert response.json()["name"] == "Updated" @pytest.mark.asyncio async def test_delete_resource(client: AsyncClient, auth_headers: dict): create_resp = await client.post( "/resources", json={"name": "To Delete"}, headers=auth_headers, ) resource_id = create_resp.json()["id"] response = await client.delete( f"/resources/{resource_id}", headers=auth_headers ) assert response.status_code == 204 # Verify deleted get_resp = await client.get( f"/resources/{resource_id}", headers=auth_headers ) assert get_resp.status_code == 404
pythonfrom fastapi import Depends, HTTPException, status from fastapi.security import OAuth2PasswordBearer oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/token") async def get_current_user( token: str = Depends(oauth2_scheme), db: AsyncSession = Depends(get_db), ) -> User: payload = decode_jwt(token) user = await db.get(User, payload["sub"]) if not user: raise HTTPException(status_code=401, detail="Invalid token") return user def require_role(*roles: str): """Factory for role-based access control.""" async def checker(current_user: User = Depends(get_current_user)): if current_user.role not in roles: raise HTTPException(status_code=403, detail="Insufficient permissions") return current_user return checker
pythonimport base64 from datetime import datetime def encode_cursor(dt: datetime) -> str: return base64.urlsafe_b64encode(dt.isoformat().encode()).decode() def decode_cursor(cursor: str) -> datetime: return datetime.fromisoformat(base64.urlsafe_b64decode(cursor).decode())
Always use FastAPI's HTTPException with consistent detail messages. For validation errors, Pydantic v2 handles them automatically via RequestValidationError (422).
python# 404 — not found raise HTTPException(status_code=404, detail="Resource not found") # 409 — conflict (duplicate) raise HTTPException(status_code=409, detail="Resource with this name already exists") # 403 — forbidden raise HTTPException(status_code=403, detail="Not allowed to modify this resource")
model_config = ConfigDict(from_attributes=True) for ORM mode| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→fail | 5,392 | 6,066 | +13% | 1 | 1 | 0% | 324 | 4,475 | +1281% | 0 | 0 | — |
case-02 | fail→fail | 29,454 | 2,094 | -93% | 1 | 1 | 0% | 6,214 | 4,459 | -28% | 0 | 0 | — |
case-03 | pass→fail | 7,813 | 3,030 | -61% | 1 | 1 | 0% | 1,174 | 4,377 | +273% | 0 | 0 | — |
case-04 | pass→pass | 9,411 | 7,655 | -19% | 1 | 1 | 0% | 1,698 | 5,501 | +224% | 0 | 0 | — |
case-05 | pass→pass | 14,688 | 15,037 | +2% | 1 | 1 | 0% | 2,640 | 6,796 | +157% | 0 | 0 | — |
case-06 | pass→pass | 14,105 | 13,269 | -6% | 1 | 1 | 0% | 2,775 | 6,632 | +139% | 0 | 0 | — |
case-07 | pass→pass | 6,880 | 4,841 | -30% | 1 | 1 | 0% | 1,295 | 4,912 | +279% | 0 | 0 | — |
case-08 | pass→pass | 12,052 | 8,080 | -33% | 1 | 1 | 0% | 2,245 | 5,574 | +148% | 0 | 0 | — |
case-09 | pass→pass | 9,897 | 9,595 | -3% | 1 | 1 | 0% | 2,036 | 5,955 | +192% | 0 | 0 | — |
case-10 | pass→pass | 11,199 | 5,745 | -49% | 1 | 1 | 0% | 2,043 | 5,084 | +149% | 0 | 0 | — |
case-11 | pass→pass | 11,913 | 7,367 | -38% | 1 | 1 | 0% | 2,346 | 5,408 | +131% | 0 | 0 | — |
case-12 | pass→pass | 9,074 | 7,593 | -16% | 1 | 1 | 0% | 1,642 | 5,458 | +232% | 0 | 0 | — |
case-13 | pass→pass | 10,794 | 9,709 | -10% | 1 | 1 | 0% | 2,057 | 5,981 | +191% | 0 | 0 | — |
case-14 | pass→pass | 7,396 | 8,256 | +12% | 1 | 1 | 0% | 1,275 | 5,579 | +338% | 0 | 0 | — |
case-15 | fail→pass | 8,239 | 6,582 | -20% | 1 | 1 | 0% | 1,582 | 5,388 | +241% | 0 | 0 | — |
case-16 | pass→pass | 12,481 | 6,047 | -52% | 1 | 1 | 0% | 2,386 | 5,171 | +117% | 0 | 0 | — |
case-17 | pass→pass | 10,316 | 7,792 | -24% | 1 | 1 | 0% | 1,917 | 5,541 | +189% | 0 | 0 | — |
case-18 | pass→pass | 9,793 | 6,968 | -29% | 1 | 1 | 0% | 1,790 | 5,431 | +203% | 0 | 0 | — |
case-19 | pass→pass | 8,428 | 6,908 | -18% | 1 | 1 | 0% | 1,605 | 5,393 | +236% | 0 | 0 | — |
case-20 | pass→pass | 9,691 | 6,677 | -31% | 1 | 1 | 0% | 1,990 | 5,367 | +170% | 0 | 0 | — |
case-21 | pass→pass | 10,855 | 6,483 | -40% | 1 | 1 | 0% | 1,970 | 5,294 | +169% | 0 | 0 | — |
case-22 | pass→pass | 10,070 | 12,954 | +29% | 1 | 1 | 0% | 1,892 | 5,569 | +194% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted, and 21 counted toward the lift figure. The other 1 produced results that are not comparable between the two arms, so they are excluded from the headline rather than averaged into it. The headline lift of 0 percentage points is the difference between those two pass rates over the 21 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.