FastAPI Guide

FastAPIModern Python Web Framework

Fast, modern Python web framework for building APIs with automatic documentation and type hints.

Contents

FastAPI Basics

FastAPI is a modern, fast web framework for building APIs with Python 3.7+ based on standard Python type hints.

bash
# Installation
pip install fastapi uvicorn[standard]

# Run Server
uvicorn main:app --reload
python
# main.py
from fastapi import FastAPI

app = FastAPI()

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

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

# Automatic API Documentation
# Swagger UI: http://localhost:8000/docs
# ReDoc: http://localhost:8000/redoc