← Back to Tutorials

3. Hello World

First FastAPI App

# main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

Run the Server

# Terminal
uvicorn main:app --reload

# Output
INFO:     Uvicorn running on http://127.0.0.1:8000
INFO:     Application startup complete.

Test the API

Open http://127.0.0.1:8000 in your browser. You should see {"message":"Hello World"}. Try http://127.0.0.1:8000/items/42?q=test.

✏️ Exercise: Create the Hello World app, run it with uvicorn, and verify the root and items endpoints respond correctly. Add a third endpoint returning your name.