fastapi-router-py
microsoft/skills
Erstellen Sie FastAPI-Router mit CRUD-Operationen, Authentifizierungsabhängigkeiten und korrekten Antwortmodellen gemäß etablierten Mustern.
...Alle erweiternFastAPI-Router
Erstellen Sie FastAPI-Router nach bewährten Mustern mit korrekter Authentifizierung, Antwortmodellen und HTTP-Statuscodes.
Schnellstart
Kopieren Sie die Vorlage aus „assets/template.py“ und ersetzen Sie die Platzhalter:
{{ResourceName}}→ Name in PascalCase (z. B.Project){{resource_name}}→ snake_case-Namen (z. B.project){{resource_plural}}→ Pluralform (z. B.projects)
Authentifizierungsmuster
# 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)
Antwortmodelle
@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-Statuscodes
@router.post("/items", status_code=status.HTTP_201_CREATED)
@router.delete("/items/{id}", status_code=status.HTTP_204_NO_CONTENT)
Integrationsschritte
- Router erstellen in
src/backend/app/routers/ - Einbinden in
src/backend/app/main.py - Entsprechende Pydantic-Modelle erstellen
- Service-Schicht bei Bedarf erstellen
- Frontend-API-Funktionen hinzufügen
Bewährte Vorgehensweisen
- Wählen Sie je nach Endpunkt „
def“ oder „async def“, je nachdem, ob Sie asynchrone E/A-Operationen aufrufen; mischen Sie keine synchronen und asynchronen blockierenden Aufrufe in einem Handler. - Verwalten Sie langlebige Ressourcen (DB-Pools, HTTP-Clients) in „
lifespan“ und injizieren Sie sie über „Depends“; verwenden Siewith/async withfür Ressourcen pro Anfrage.
---
name: fastapi-router-py
description: Create FastAPI routers with CRUD operations, authentication dependencies, and proper response models following established patterns.
license: MIT
---
# 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
## Best Practices
1. **Pick `def` or `async def` per endpoint based on whether you call async I/O;** do not mix sync and async blocking calls in one handler.
2. **Manage long-lived resources (DB pools, HTTP clients) in `lifespan` and inject via `Depends`;** use `with`/`async with` for per-request resources.
Alle Dateien
0 Dateienfastapi-router-py installieren
Laden Sie die Skill-Dateien herunter und entpacken Sie sie in Ihr Verzeichnis „.claude/skills/“.
ZIP herunterladenKlonen Sie das Repository und kopieren Sie die Skill-Dateien in Ihr Projekt.
git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-python/skills/fastapi-router-py # Copy SKILL.md to your .claude/skills/ directory
Kopieren





Heim
