Your First FastAPI Application
This topic breaks down every line of a real FastAPI application so you understand exactly what each part does. By the end, you will read and write basic FastAPI code with confidence.
The Full Application
Here is the complete app you will build and understand in this topic:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"message": "Welcome to my API"}
@app.get("/about")
def about():
return {"app": "My First FastAPI App", "version": "1.0"}
Line-by-Line Breakdown
Line 1: Import FastAPI
from fastapi import FastAPI
This line pulls the FastAPI class into your file. Think of it like picking up a tool from a toolbox. The class contains everything your API needs — routing, validation, documentation.
Line 3: Create the App Object
app = FastAPI()
This creates one instance of your API. All your routes and settings attach to this one object. The variable name app is a convention — you could name it anything, but app is what Uvicorn expects by default.
Lines 5–7: Your First Route
@app.get("/")
def home():
return {"message": "Welcome to my API"}
This is a route — a specific URL path that your API responds to. Break it into parts:
@app.get("/")
│ │
│ └─ The URL path: "/" means the root address
└────── HTTP method: GET means this handles read requests
def home(): ← the function that runs when this URL is visited
return {...} ← the data sent back to the caller (as JSON)
Lines 9–11: A Second Route
@app.get("/about")
def about():
return {"app": "My First FastAPI App", "version": "1.0"}
This second route sits at /about. Visiting http://127.0.0.1:8000/about runs the about() function and returns that dictionary as JSON.
How a Request Travels Through Your App
User visits: http://127.0.0.1:8000/about
|
v
[ Uvicorn ]
receives the request
|
v
[ FastAPI Router ]
matches "/about" to the about() function
|
v
about() runs
returns {"app": ..., "version": ...}
|
v
FastAPI converts dict → JSON
|
v
User's browser shows the JSON
What JSON Looks Like in the Browser
When you return a Python dictionary from a FastAPI route, FastAPI automatically converts it to JSON. The browser displays:
{
"app": "My First FastAPI App",
"version": "1.0"
}
Adding a Third Route with Data
Routes can return any data — numbers, lists, nested objects:
@app.get("/items")
def get_items():
return [
{"id": 1, "name": "Laptop"},
{"id": 2, "name": "Mouse"},
{"id": 3, "name": "Keyboard"}
]
This returns a JSON array. Any programming language or tool that understands JSON can read this data.
Route Naming Rules
Route paths follow simple rules:
VALID paths: / ← root /users ← single segment /users/profile ← nested INVALID paths: users ← must start with / /users /list ← spaces not allowed
Running and Testing Your App
Save your file as main.py, then run:
uvicorn main:app --reload
Test all three routes in your browser:
http://127.0.0.1:8000/— home routehttp://127.0.0.1:8000/about— about routehttp://127.0.0.1:8000/items— items route
Key Points
app = FastAPI()creates your application object.- Each route is a Python function decorated with
@app.get()(or post, put, delete). - The path string like
"/"or"/about"maps a URL to a function. - Return a Python dictionary and FastAPI sends it as JSON automatically.
- One app can have many routes — each at a different path.
