home / skills / sickn33 / antigravity-awesome-skills / fastapi-router-py

fastapi-router-py skill

/skills/fastapi-router-py

This skill helps you generate FastAPI routers with CRUD, authentication, and proper response models for robust REST APIs.

npx playbooks add skill sickn33/antigravity-awesome-skills --skill fastapi-router-py

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.6 KB
---
name: fastapi-router-py
description: Create FastAPI routers with CRUD operations, authentication dependencies, and proper response models. Use when building REST API endpoints, creating new routes, implementing CRUD operations, or adding authenticated endpoints in FastAPI applications.
---

# FastAPI Router

Create FastAPI routers following established patterns with proper authentication, response models, and HTTP status codes.

## Quick Start

Copy the template from [assets/template.py](assets/template.py) and replace placeholders:
- `{{ResourceName}}` → PascalCase name (e.g., `Project`)
- `{{resource_name}}` → snake_case name (e.g., `project`)
- `{{resource_plural}}` → plural form (e.g., `projects`)

## Authentication Patterns

```python
# Optional auth - returns None if not authenticated
current_user: Optional[User] = Depends(get_current_user)

# Required auth - raises 401 if not authenticated
current_user: User = Depends(get_current_user_required)
```

## Response Models

```python
@router.get("/items/{item_id}", response_model=Item)
async def get_item(item_id: str) -> Item:
    ...

@router.get("/items", response_model=list[Item])
async def list_items() -> list[Item]:
    ...
```

## HTTP Status Codes

```python
@router.post("/items", status_code=status.HTTP_201_CREATED)
@router.delete("/items/{id}", status_code=status.HTTP_204_NO_CONTENT)
```

## Integration Steps

1. Create router in `src/backend/app/routers/`
2. Mount in `src/backend/app/main.py`
3. Create corresponding Pydantic models
4. Create service layer if needed
5. Add frontend API functions

Overview

This skill scaffolds FastAPI routers with ready-made CRUD endpoints, authentication hooks, and correct response models. It speeds up creating REST routes by providing a copy-paste template and clear integration steps for service layers and frontend wiring. Use it to produce consistent, secure, and well-typed route modules in FastAPI projects.

How this skill works

The skill supplies a template router file with placeholders for resource names and pluralization. It adds optional and required authentication dependency patterns, response_model annotations, and proper HTTP status codes for create and delete operations. Follow the integration steps to place the router, mount it in the main app, add Pydantic models, and connect a service layer and frontend API calls.

When to use it

  • Creating new REST resources and CRUD endpoints quickly
  • Adding authenticated routes that require optional or required user context
  • Standardizing router structure across a FastAPI codebase
  • Bootstrapping route files for new features or microservices
  • Onboarding contributors who need a clear router pattern

Best practices

  • Replace template placeholders with PascalCase, snake_case, and plural forms before committing
  • Use response_model for all GET endpoints to enforce output shapes and docs
  • Prefer service-layer functions for business logic and make routers thin
  • Use optional auth for public endpoints and required auth for protected actions
  • Return proper HTTP status codes (201 for create, 204 for delete) to match client expectations

Example use cases

  • Generate a projects router with endpoints for create, read, update, list, and delete
  • Add an authenticated profile endpoint using get_current_user_required
  • Standardize API routes in a monorepo so frontend teams can rely on consistent responses
  • Quickly scaffold admin-only endpoints by swapping auth dependency to the required pattern
  • Integrate new microservice routers and mount them in main.py with minimal boilerplate

FAQ

How do I handle authentication in generated routers?

Use the provided dependency patterns: optional auth (Depends(get_current_user)) for public endpoints and required auth (Depends(get_current_user_required)) for protected endpoints.

Where do I put the generated router file?

Place it under src/backend/app/routers/, then import and include it in src/backend/app/main.py so the routes are mounted on the FastAPI app.