fastapi-router-py
microsoft/skills
確立されたパターンに従い、CRUD操作、認証の依存関係、および適切なレスポンスモデルを備えたFastAPIルーターを作成します。
...すべて拡張しますFastAPI ルーター
適切な認証、レスポンスモデル、およびHTTPステータスコードを備えた、確立されたパターンに従ったFastAPIルーターを作成します。
クイックスタート
assets/template.py からテンプレートをコピーし、プレースホルダーを置き換えます:
{{ResourceName}}→ PascalCase形式の名前(例:Project){{resource_name}}→ snake_case形式の名称(例:project){{resource_plural}}→ 複数形(例:projects)
認証パターン
# 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)
レスポンスモデル
@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ステータスコード
@router.post("/items", status_code=status.HTTP_201_CREATED)
@router.delete("/items/{id}", status_code=status.HTTP_204_NO_CONTENT)
統合の手順
- 以下の場所にルーターを作成する
src/backend/app/routers/ - マウント先
src/backend/app/main.py - 対応するPydanticモデルを作成する
- 必要に応じてサービス層を作成する
- フロントエンドAPI関数を追加する
ベストプラクティス
- 非同期I/Oを呼び出すかどうかによって、エンドポイントごとに `
def` または `async def` を選択してください。1つのハンドラ内で、同期呼び出しと非同期のブロッキング呼び出しを混在させないでください。 lifespan内で長期存続リソース(DBプール、HTTPクライアント)を管理し、Dependsを介して注入してください。リクエストごとのリソースについてはwith/async withを使用してください。
---
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.
すべてのファイル
0件のファイルfastapi-router-pyをインストール
スキルファイルをダウンロードし、.claude/skills/ ディレクトリに解凍してください。
ZIPをダウンロードリポジトリをクローンし、スキルファイルをプロジェクトにコピーしてください。
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
コピー





家
