Concepts 🚀 Project 🧮 Labs
🧮 Labs / FastAPI / Your first route — @app.get('/path')

Your first route — @app.get('/path')

A route is a function FastAPI calls when a matching HTTP request arrives. Decorator specifies method + path.

main.py v3.1
API simulator click Simulate
Click "Simulate" to see what this FastAPI app does.
🧠 How it works

@app.get('/path') registers the function below as the handler for GET requests to /path. Other decorators: @app.post, @app.put, @app.delete, @app.patch. async def — the function can await I/O. If you have no awaits, regular def works too (FastAPI runs it in a threadpool). Returning a dict — FastAPI auto-converts to JSON. {user_id} in the path is a PATH PARAMETER — FastAPI extracts it and passes as a function arg. The type hint (int) VALIDATES the input — /users/abc returns 422 automatically. /docs is auto-generated Swagger UI — every FastAPI app gets it for free.

✏️ Try these changes
  • Change '/' to '/home'
    → root is now at /home instead of /
  • Change user_id: int to user_id: str
    → /users/abc now works (no validation)
  • Add a new route @app.get('/about') that returns {'app': 'My API'}
    → new endpoint
  • Change the return of /health to {'status': 'ok', 'uptime': 100}
    → more fields in response