fastapi

FastAPI Python framework. Covers REST APIs, validation, dependencies, security. Use when building Python web APIs with FastAPI, configuring Pydantic models, implementing dependency injection, or setting up OAuth2/JWT authentication. Keywords: FastAPI, Pydantic, async, OAuth2, JWT, REST API.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "fastapi" with this command: npx skills add itechmeat/llm-code/itechmeat-llm-code-fastapi

FastAPI

This skill provides comprehensive guidance for building APIs with FastAPI.

Quick Navigation

TopicReference
Getting startedreferences/first-steps.md
Path parametersreferences/path-parameters.md
Query parametersreferences/query-parameters.md
Request bodyreferences/request-body.md
Validationreferences/validation.md
Body advancedreferences/body-advanced.md
Cookies/Headersreferences/cookies-headers.md
Pydantic modelsreferences/models.md
Forms/Filesreferences/forms-files.md
Error handlingreferences/error-handling.md
Path configreferences/path-config.md
Dependenciesreferences/dependencies.md
Securityreferences/security.md
Middlewarereferences/middleware.md
CORSreferences/cors.md
Databasereferences/sql-databases.md
Project structurereferences/bigger-applications.md
Background tasksreferences/background-tasks.md
Metadata/Docsreferences/metadata-docs.md
Testingreferences/testing.md
Advanced responsesreferences/responses-advanced.md
WebSocketsreferences/websockets.md
Templatesreferences/templates.md
Settings/Env varsreferences/settings.md
Lifespan eventsreferences/lifespan.md
OpenAPI advancedreferences/openapi-advanced.md

When to Use

  • Creating REST APIs with Python
  • Adding endpoints with automatic validation
  • Implementing OAuth2/JWT authentication
  • Working with Pydantic models
  • Adding dependency injection
  • Configuring CORS, middleware
  • Uploading files, handling forms
  • Testing API endpoints

Installation

Requires Python 3.10+. Install: pip install "fastapi[standard]" (full with uvicorn) or pip install fastapi (minimal). Add python-multipart for forms/files.

Release Highlights (0.133.0 → 0.135.1)

  • 0.134.0: streaming JSON Lines and streaming binary data support using yield.
  • 0.135.0: first-class Server-Sent Events (SSE) support (EventSourceResponse).
  • 0.135.1: fix around TaskGroup usage in request async exit stack (stability fix).

Quick Start

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str | None = None):
    return {"item_id": item_id, "q": q}

Run: fastapi dev main.py

Core Patterns

Type-Safe Parameters

from typing import Annotated
from fastapi import Path, Query

@app.get("/items/{item_id}")
def read_item(
    item_id: Annotated[int, Path(ge=1)],
    q: Annotated[str | None, Query(max_length=50)] = None
):
    return {"item_id": item_id, "q": q}

Request Body with Validation

from pydantic import BaseModel, Field

class Item(BaseModel):
    name: str = Field(min_length=1, max_length=100)
    price: float = Field(gt=0)

@app.post("/items/", response_model=Item)
def create_item(item: Item):
    return item

Dependencies

from fastapi import Depends

async def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@app.get("/users/")
def list_users(db: Annotated[Session, Depends(get_db)]):
    return db.query(User).all()

Authentication

from fastapi.security import OAuth2PasswordBearer

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
    return decode_token(token)

@app.get("/users/me")
def read_me(user: Annotated[User, Depends(get_current_user)]):
    return user

API Documentation

  • Swagger UI: /docs
  • ReDoc: /redoc
  • OpenAPI: /openapi.json

Best Practices

  • Use Annotated[Type, ...] for parameters
  • Define Pydantic models for request/response
  • Use response_model for output filtering
  • Add status_code for proper HTTP codes
  • Use tags for API organization
  • Add dependencies at router/app level for auth

Prohibitions

  • ❌ Return raw database models (use response models)
  • ❌ Store passwords in plain text (use bcrypt/passlib)
  • ❌ Mix Body with Form/File in same endpoint
  • ❌ Use sync blocking I/O in async endpoints
  • ❌ Skip HTTPException for error handling

Links

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

react-testing-library

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

social-writer

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

commits

No summary provided by upstream source.

Repository SourceNeeds Review