Skip to content
Back to skills

Fastapi Advanced

ASecurity

When to activate: FastAPI advanced patterns, middleware, lifespan, OpenAPI customization, rate limiting, CORS, file uploads

  • 2 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added October 26, 2026
ai-agentspythonrustgofastapiapisecuritydocumentation

Works with

  • cursor
  • cli
  • api

Security analysis

A100/100

Scanned October 26, 2026

npx -y skills add Mattakushi432/Claude-Code-Skills-Custom-DevTools-Pack --skill fastapi-advanced --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Fastapi Advanced?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Fastapi Advanced
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/mattakushi432-fastapi-advanced/badge)](https://www.skillsdirectory.com/skills/mattakushi432-fastapi-advanced)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: fastapi-advanced
description: "When to activate: FastAPI advanced patterns, middleware, lifespan, OpenAPI customization, rate limiting, CORS, file uploads"
origin: zakr
metadata:
  namespace: zakr/devtools-pack
  version: 1.0.0
  category: engineering
  language: python
  platforms: [claude-code, codex, gemini-cli, cursor, aider, windsurf]
---

# Advanced FastAPI Patterns

## Lifespan (startup/shutdown)
```python
from contextlib import asynccontextmanager
from fastapi import FastAPI

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup
    await db_pool.start()
    await redis.connect()
    logger.info("Application started")
    
    yield  # app is running
    
    # Shutdown
    await db_pool.close()
    await redis.disconnect()
    logger.info("Application stopped")

app = FastAPI(lifespan=lifespan)
```

## OpenAPI Customization
```python
from fastapi.openapi.utils import get_openapi

def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    
    schema = get_openapi(
        title="My API",
        version="1.0.0",
        description="Production API documentation",
        routes=app.routes,
    )
    
    # Add security scheme
    schema["components"]["securitySchemes"] = {
        "BearerAuth": {"type": "http", "scheme": "bearer", "bearerFormat": "JWT"}
    }
    schema["security"] = [{"BearerAuth": []}]
    
    app.openapi_schema = schema
    return schema

app.openapi = custom_openapi
```

## File Uploads
```python
from fastapi import UploadFile, File
import aiofiles

ALLOWED_TYPES = {"image/jpeg", "image/png", "application/pdf"}
MAX_FILE_SIZE = 10 * 1024 * 1024  # 10MB

@router.post("/upload")
async def upload_file(
    file: UploadFile = File(...),
    current_user: User = Depends(get_current_user),
) -> dict:
    # Validate content type
    if file.content_type not in ALLOWED_TYPES:
        raise HTTPException(400, f"File type {file.content_type} not allowed")
    
    # Stream to check size without loading all into memory
    contents = b""
    async for chunk in file:
        contents += chunk
        if len(contents) > MAX_FILE_SIZE:
            raise HTTPException(413, "File too large")
    
    # Save
    dest = Path(f"uploads/{uuid4()}_{file.filename}")
    async with aiofiles.open(dest, "wb") as out:
        await out.write(contents)
    
    return {"filename": file.filename, "size": len(contents), "path": str(dest)}
```

## Middleware Stack
```python
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.trustedhost import TrustedHostMiddleware
from fastapi.middleware.gzip import GZipMiddleware

# Order matters: outermost first
app.add_middleware(GZipMiddleware, minimum_size=1000)
app.add_middleware(
    CORSMiddleware,
    allow_origins=settings.cors_origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["api.example.com", "localhost"])
```

## Custom Exception Handling
```python
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as StarletteHTTPException

@app.exception_handler(RequestValidationError)
async def validation_error_handler(request: Request, exc: RequestValidationError) -> JSONResponse:
    return JSONResponse(
        status_code=422,
        content={
            "error": "validation_error",
            "details": [
                {"field": ".".join(str(loc) for loc in err["loc"]), "message": err["msg"]}
                for err in exc.errors()
            ],
        },
    )

@app.exception_handler(StarletteHTTPException)
async def http_error_handler(request: Request, exc: StarletteHTTPException) -> JSONResponse:
    return JSONResponse(
        status_code=exc.status_code,
        content={"error": exc.detail},
    )
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.