FastAPI: apps and routes
FastAPI maps URL paths and HTTP methods to Python functions. JSON dict return values become JSON responses.
Minimal app
A typical minimal FastAPI module looks like this:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"hello": "world"}
@app.get("/ping")
def ping():
return {"message": "pong"}
@app.post("/alive")
def alive():
return {"message": "you're alive!"}
@app.get("/path")registers a GET route.@app.post(...)registers POST.- Return a
dict→ JSON object; return a list → JSON array.
Note
Typos in keys (e.g.
mesagevsmessage) become typos in JSON—clients and tests should match the exact spelling you intend.
Path vs query parameters (preview)
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}
FastAPI parses and validates types for you.
Implement
- Complete Virtual environments and packages so FastAPI is installed in your venv.
- Create
app.py(or the filename your template uses) withapp = FastAPI(). - Add
GET /returning any small JSON dict. - Add
GET /pingreturning{"message": "pong"}(your capstone route—keep it working from here on). - Commit and push (application code only—not personal test files).