FastAPI | Sheetly Cheat Sheet

Last Updated: November 21, 2025

FastAPI

Modern Python API framework

Basic API

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float
    is_offer: bool = False

@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.post("/items/")
async def create_item(item: Item):
    return {"item": item, "status": "created"}

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

Path Operations

Item Description
@app.get() GET request
@app.post() POST request
@app.put() PUT request
@app.delete() DELETE request
@app.patch() PATCH request

Dependencies

from fastapi import Depends, HTTPException

async def get_token_header(x_token: str = Header()):
    if x_token != "secret-token":
        raise HTTPException(status_code=400, detail="Invalid token")
    return x_token

@app.get("/items/")
async def read_items(token: str = Depends(get_token_header)):
    return {"token": token}

Running

pip install fastapi uvicorn
Install FastAPI
uvicorn main:app --reload
Run development server
uvicorn main:app --host 0.0.0.0 --port 8000
Run on custom port

💡 Pro Tips

Quick Reference

High performance async API framework with automatic docs

← Back to Programming Languages | Browse all categories | View all cheat sheets