FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.7+. It is built on top of Starlette (for web parts) and Pydantic (for data validation), and uses standard Python type hints to automatically generate:
/docs.async/await support for concurrency.FastAPI().@app.get(), @app.post(), etc.? in the URL./docs, ReDoc at /redoc.A minimal FastAPI application follows these steps:
main.py).FastAPI instance.@app.get(), @app.post(), etc.).
# Install (run in terminal)
# pip install "fastapi[all]" uvicorn
from fastapi import FastAPI
# Create application instance
app = FastAPI()
# Define a path operation (GET /)
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}
# Define a path operation with path parameter
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "query": q}
# Run the server from terminal:
# uvicorn main:app --reload
To receive structured JSON from the client, you define a Pydantic model. FastAPI automatically:
int, float, str).
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
# Define a Pydantic model
class Item(BaseModel):
name: str
description: str | None = None
price: float
in_stock: bool = True
# POST endpoint to create an item
@app.post("/items/")
def create_item(item: Item):
# item is automatically validated and converted to a Python object
tax = item.price * 0.18
total = item.price + tax
return {
"item": item,
"tax": tax,
"total_price": total
}
Send a POST request to http://127.0.0.1:8000/items/ with JSON:
{
"name": "Laptop",
"description": "Fast developer machine",
"price": 60000,
"in_stock": true
}
FastAPI will:
create_item() with an Item instance.Expected response (simplified):
{
"item": {
"name": "Laptop",
"description": "Fast developer machine",
"price": 60000.0,
"in_stock": true
},
"tax": 10800.0,
"total_price": 70800.0
}
FastAPI uses function parameters to detect where data should come from:
"/items/{item_id}" are path parameters.
# Path and query parameters example
from fastapi import FastAPI
# Create FastAPI application
app = FastAPI()
# Endpoint with path and query parameters
@app.get("/users/{user_id}")
def get_user(user_id: int, active: bool = True):
"""
Example URL:
/users/10?active=false
"""
return {
"user_id": user_id,
"active": active
}
routers, models, schemas, services).uvicorn main:app --reload during development for auto-reload.responses parameter.GET / returning a welcome message.GET /square/{number} returning the square of a number.Student with fields id, name, and marks. Create a POST /students/ endpoint that accepts and returns a student object.min_marks to filter students by minimum marks./docs in your browser and test all endpoints using the auto-generated Swagger UI.GET /students/ to return them.Once your FastAPI app is running, open the following URLs in your browser:
http://127.0.0.1:8000/docs – Swagger UI (interactive docs).http://127.0.0.1:8000/redoc – ReDoc alternative documentation.You can send requests directly from these UIs without writing a separate client.