Skip to content
dreamcode
dreamcode
Map
FastAPI + REST
Lesson 71 of 77
+15 XP on finish
PYTHON APPLIEDChapter 12 · Python for Web Development

FastAPI and REST APIs

FastAPI is a modern Python framework built on type hints. It generates interactive API documentation automatically, validates request data using Pydantic models, and supports async handlers natively.

Worked example
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Star(BaseModel):
    name: str
    magnitude: float

@app.get("/api/stars")
async def list_stars():
    return [
        {"name": "Vega", "magnitude": 0.03},
        {"name": "Sirius", "magnitude": -1.46},
    ]

@app.post("/api/stars")
async def create_star(star: Star):
    return {"created": star.name}

How it reads

  • @app.get and @app.post map HTTP methods to handler functions
  • BaseModel validates incoming JSON against the type hints automatically
  • async def lets FastAPI handle concurrent requests efficiently
Cloud tip: Visit /docs on a running FastAPI server to see auto-generated Swagger UI documentation for every endpoint.

Check your understanding

0 / 3

Answer all 3 to complete this lesson and earn 15 XP.

  1. 1. What library does FastAPI use for request data validation?
  2. 2. What URL path shows auto-generated API documentation on a FastAPI server?
  3. 3. What advantage does 'async def' provide in FastAPI handlers?
Answer every question to unlock the next lesson.