Why FastAPI?
FastAPI is a modern Python web framework for building APIs with automatic API documentation, type validation, and high performance.
Basic FastAPI Application
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
app = FastAPI(title="My API", version="1.0.0")
class Item(BaseModel):
name: str
price: float
description: str = None
class ItemResponse(BaseModel):
id: int
name: str
price: float
items = []
next_id = 1
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/items", response_model=List[ItemResponse])
async def get_items():
return items
@app.post("/items", response_model=ItemResponse)
async def create_item(item: Item):
global next_id
new_item = {
"id": next_id,
"name": item.name,
"price": item.price
}
items.append(new_item)
next_id += 1
return new_item
@app.get("/items/{item_id}", response_model=ItemResponse)
async def get_item(item_id: int):
for item in items:
if item["id"] == item_id:
return item
raise HTTPException(status_code=404, detail="Item not found")
Running FastAPI
# Install FastAPI
pip install fastapi uvicorn
# Run development server
uvicorn main:app --reload
# Run production server
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
Database Integration
from sqlalchemy import create_engine, Column, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
DATABASE_URL = "sqlite:///./app.db"
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
class ItemDB(Base):
__tablename__ = "items"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
price = Column(Float)
Base.metadata.create_all(bind=engine)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.post("/items")
async def create_item(item: Item, db: Session = Depends(get_db)):
db_item = ItemDB(**item.dict())
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
Authentication
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
SECRET_KEY = "your-secret-key"
ALGORITHM = "HS256"
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
async def get_current_user(token: str = Depends(oauth2_scheme)):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials"
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub")
if username is None:
raise credentials_exception
except JWTError:
raise credentials_exception
return username
@app.get("/protected")
async def protected_route(current_user: str = Depends(get_current_user)):
return {"message": f"Hello {current_user}"}
Features
- Automatic API documentation at /docs
- Type validation with Pydantic
- Async/await support
- High performance (comparable to Node.js)
- Built-in data validation