Back to AI CityChapter 1 / 11 ยท FastAPI
Async Highways
Citypopy ยท District 01

Welcome to the Reception Center

Where curious citizens whisper questions, friendly AI helpers answer in sparkles, and every message travels through tidy little tubes called FastAPI.

Pip
Popy
Sunny
Meet the cast

Who lives in the Reception Center? ๐Ÿ’›

Three little helpers run the whole post office. Each one stands for a real FastAPI superpower โ€” wrapped in a friendly face.

Pip

The Greeter

Pip

GET requests

Pip stands at the city gates with a bright blue umbrella, waving every visitor in. Whenever you ask for something โ€” a fact, a forecast, a friendly hello โ€” Pip is the one who fetches it for you.

Superpower ยท Knows every door in town and the fastest path to it.

Popy

The Mail Sorter

Popy

POST & request bodies

Popy keeps a tidy desk in Postbox Lane. When letters arrive, Popy unfolds them carefully, checks every detail with Pydantic glasses, and stamps them ready for delivery.

Superpower ยท Catches typos and wrong shapes before anyone notices.

Sunny

The Lantern Keeper

Sunny

Async & streaming

Sunny tends the Lantern Garden after dusk, lighting many lights at once. Long waits never bother Sunny โ€” there's always another lantern to tend while the slow ones glow.

Superpower ยท Handles many requests in parallel without breaking a sweat.

A message takes a journey โœ‰๏ธ

Watch a citizen's letter zip across the city to an AI helper, then a gift-wrapped reply zips back. That tiny round-trip is what FastAPI does, all day, very fast.

Citizen
โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€ โ”€
GET /hello
AI Helper
1

Knock knock

A citizen sends a tiny request.

2

Sort & route

FastAPI reads the address and delivers it.

3

Sparkle reply

The helper sends a happy answer back.

The little street signs ๐Ÿชง

Every door in Citypopy has a name. FastAPI calls these routes. Tap a sign to peek inside.

@app.get('/hello')

Neighborhoods of the Reception Center ๐Ÿ—บ๏ธ

Tiny, friendly streets โ€” each one a real FastAPI idea, dressed in city clothes. Tap a card to wander inside.

In the Town Square, every helper stands behind a polished counter. When you ask a question, they tuck the answer into a tidy JSON box โ€” labels on the outside, treasures inside โ€” so your app can unwrap it without confusion.

  • โœฆReturn a Python dict and FastAPI gift-wraps it as JSON.
  • โœฆUse response_model to promise the exact shape of the box.
  • โœฆStatus codes are little colored stickers on the lid.
@app.get("/citizen/{id}")
def greet(id: int):
    return {"id": id, "hi": "๐ŸŒŸ"}

Down Postbox Lane, citizens slip detailed letters through the slot. Pydantic, the kind postmaster, reads every line โ€” checking names, ages, and shapes โ€” before letting the letter into the city.

  • โœฆDefine a BaseModel for every incoming letter.
  • โœฆWrong types get a polite 422 reply, not a crash.
  • โœฆNested models are letters tucked inside envelopes.
class Wish(BaseModel):
    text: str
    sparkles: int = 3

@app.post("/wish")
def make(w: Wish): return w

In the Lantern Garden, helpers don't stand in line. While one waits for a slow cloud to answer, the others keep lighting lanterns. Everyone's request glows in turn โ€” quickly, gently, together.

  • โœฆasync def lets a helper pause without blocking others.
  • โœฆawait is a polite 'I'll wait here, you go ahead'.
  • โœฆGreat for calling AI APIs, databases, and the weather.
@app.get("/weather")
async def sky():
    data = await ask_cloud()
    return data

At the top of Whisper Tower, two friends share a long brass tube. Once it's open, every giggle and idea travels instantly โ€” no knocking, no goodbyes โ€” until someone gently closes the lid.

  • โœฆWebSockets stay connected for live chats and games.
  • โœฆSend and receive whenever โ€” both sides can speak.
  • โœฆPerfect for streaming AI replies token by token.
@app.websocket("/chat")
async def chat(ws: WebSocket):
    await ws.accept()
    await ws.send_text("hi โœจ")
Get started

Build your own little city ๐Ÿงฑ

Four cozy steps. By the end, you'll have a real FastAPI app saying hello โ€” ready for Pip, Popy, and Sunny to move in.

  1. 1

    Open your toolbox

    Make a cozy folder for your tiny city, then create a Python world inside it.

    mkdir citypopy && cd citypopy
    python -m venv .venv
    source .venv/bin/activate
  2. 2

    Invite FastAPI to play

    Install FastAPI and Uvicorn โ€” the little engine that helps your city run.

    pip install "fastapi[standard]"
  3. 3

    Build your first door

    Make a file called main.py and write your very first route. Pip will greet visitors.

    # main.py
    from fastapi import FastAPI
    
    app = FastAPI()
    
    @app.get("/")
    def hello():
        return {"message": "hi from Citypopy โœจ"}
  4. 4

    Open the city gates

    Run the server and visit http://127.0.0.1:8000 โ€” your first citizen has arrived!

    fastapi dev main.py
๐ŸŽ

Bonus gift

Visit /docs after starting the server โ€” FastAPI bakes a beautiful playground for you, free.

GET /docs

Try the tube โœจ

Whisper a tiny message. Watch it travel and come home wrapped in a smile. (Pretend FastAPI in your browser โ€” same idea, no servers harmed.)

You
โ†’
Popy
Hi friend! Ask me anything about FastAPI and I'll send a sparkle back โœจ

You just learned how AI apps talk ๐Ÿ’ซ

FastAPI is the friendly post office of the internet โ€” small letters, fast tubes, kind helpers. Come back soon to visit the next district of Citypopy.

GET /next-district
POST /high-five

Citypopy ยท The Reception Center ยท Built with curiosity ๐Ÿ’›

Mini Project
Build Quest

Reception API

Deliverable: Create one POST endpoint that accepts a planning request and returns structured JSON with status and summary.

Stretch: Add clear validation errors for missing or invalid fields.

Complete the deliverable first, then unlock the stretch goal.

Back
๐Ÿ™๏ธ AI City
Next District
๐Ÿ›ฃ๏ธ Async Highways