Published , Last updated
FastAPI is a modern, high-performance web framework for creating APIs in Python 3.8+. Built on Starlette and Pydantic, it offers automatic validation, interactive docs, async support, and speeds on par with NodeJS and Go.
Install with CLI support:
pip install "fastapi[standard]"
This ensures you have FastAPI (v0.115+), fastapi-cli, and Uvicorn.
Verify versions:
fastapi --version
Create main.py
:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello, FastAPI CLI!"}
Run your app:
fastapi dev main.py
Visit:
http://127.0.0.1:8000/docs
http://127.0.0.1:8000/redoc
Update main.py
:
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
is_offer: Union[bool, None] = None
@app.get("/items/{item_id}")
async def get_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
return {"item_id": item_id, "item_name": item.name, "price": item.price, "is_offer": item.is_offer}
Save & your dev CLI auto-reloads.
Output:
Navigate to:
http://127.0.0.1:8000/items/42?q=discounted
You’ll see a response like:
{
"item_id": 42,
"q": "discounted"
}
Use Swagger UI or a tool like Postman to send a PUT request to:
http://127.0.0.1:8000/items/42
With this JSON body:
{
"name": "Wireless Mouse",
"price": 19.99,
"is_offer": true
}
You’ll receive:
{
"item_id": 42,
"item_name": "Wireless Mouse",
"price": 19.99,
"is_offer": true
}
Create folder & file:
app/
└── routers/
└── users.py
main.py
routers/users.py:
from fastapi import APIRouter
router = APIRouter()
@router.get("/profile")
async def profile():
return {"user": "John Doe", "role": "admin"}
main.py:
from fastapi import FastAPI
from app.routers.users import router as users_router
app = FastAPI()
app.include_router(users_router, prefix="/users")
Run fastapi dev main.py
and test /users/profile
.
Example:
from fastapi import Depends
def common_params(q: Union[str, None] = None, limit: int = 10):
return {"q": q, "limit": limit}
@app.get("/search")
async def search(params=Depends(common_params)):
return params
Combines auto validation and async power.
from fastapi import Depends
from fastapi.security import OAuth2PasswordBearer
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
@app.get("/secure")
async def secure(token: str = Depends(oauth2_scheme)):
return {"token": token}
Now Swagger UI includes token input field for /secure
Create test_main.py
:
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_root():
r = client.get("/")
assert r.status_code == 200
assert r.json() == {"message": "Hello, FastAPI CLI!"}
Run:
pytest
Switch to production mode:
fastapi run main.py
Or manually:
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app
Deploy via Docker, Kubernetes, or cloud services.
FastAPI provides lightning-fast performance, auto-docs, strong validation, and async support, making it ideal for modern API development. From simplicity to enterprise readiness, FastAPI is perfect for any project type.