Your First FastAPI App
Create a FastAPI application, define your first endpoint, and understand how HTTP routing works.
I've written Python scripts, but how do I make Python respond to web requests?
You need a web framework — a library that listens for HTTP requests and routes them to your Python functions. We're using FastAPI because it's fast, modern, and generates documentation automatically.
Here's the smallest possible FastAPI app:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"message": "Hello, world"}
Three things just happened:
FastAPI()creates the application object@app.get("/")registers the function as a handler forGET /- The function returns a dictionary — FastAPI converts it to JSON automatically
What does @app.get("/") actually do?
It's a decorator. It tells FastAPI: "When someone sends a GET request to the path /, call this function." The path "/" is the root — like visiting https://yoursite.com/ with nothing after it.
You can have multiple routes:
@app.get("/")
def root():
return {"message": "Hello, world"}
@app.get("/health")
def health():
return {"status": "ok"}
@app.get("/about")
def about():
return {"name": "My API", "version": "1.0"}
Each decorator maps a path to a function. GET /health calls health(). GET /about calls about(). FastAPI figures out which function to run based on the URL.
How do I actually run this?
You'd save it as main.py and run it with uvicorn:
uvicorn main:app --reload
This starts a server on http://127.0.0.1:8000. The --reload flag watches for file changes and restarts automatically — perfect for development.
FastAPI also generates interactive docs at /docs — a full Swagger UI where you can test every endpoint from your browser.
And the function can return any dictionary?
Any dictionary, list, string, or number. FastAPI serializes it to JSON. Later we'll use Pydantic models for more control, but for now, dictionaries are fine:
@app.get("/stats")
def stats():
return {
"users": 42,
"active": True,
"tags": ["python", "api"]
}
FastAPI handles the JSON conversion, sets the Content-Type header, and returns a proper HTTP response. You just write normal Python functions.
Practice your skills
Sign up to write and run code in this lesson.
Your First FastAPI App
Create a FastAPI application, define your first endpoint, and understand how HTTP routing works.
I've written Python scripts, but how do I make Python respond to web requests?
You need a web framework — a library that listens for HTTP requests and routes them to your Python functions. We're using FastAPI because it's fast, modern, and generates documentation automatically.
Here's the smallest possible FastAPI app:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"message": "Hello, world"}
Three things just happened:
FastAPI()creates the application object@app.get("/")registers the function as a handler forGET /- The function returns a dictionary — FastAPI converts it to JSON automatically
What does @app.get("/") actually do?
It's a decorator. It tells FastAPI: "When someone sends a GET request to the path /, call this function." The path "/" is the root — like visiting https://yoursite.com/ with nothing after it.
You can have multiple routes:
@app.get("/")
def root():
return {"message": "Hello, world"}
@app.get("/health")
def health():
return {"status": "ok"}
@app.get("/about")
def about():
return {"name": "My API", "version": "1.0"}
Each decorator maps a path to a function. GET /health calls health(). GET /about calls about(). FastAPI figures out which function to run based on the URL.
How do I actually run this?
You'd save it as main.py and run it with uvicorn:
uvicorn main:app --reload
This starts a server on http://127.0.0.1:8000. The --reload flag watches for file changes and restarts automatically — perfect for development.
FastAPI also generates interactive docs at /docs — a full Swagger UI where you can test every endpoint from your browser.
And the function can return any dictionary?
Any dictionary, list, string, or number. FastAPI serializes it to JSON. Later we'll use Pydantic models for more control, but for now, dictionaries are fine:
@app.get("/stats")
def stats():
return {
"users": 42,
"active": True,
"tags": ["python", "api"]
}
FastAPI handles the JSON conversion, sets the Content-Type header, and returns a proper HTTP response. You just write normal Python functions.
Practice your skills
Sign up to write and run code in this lesson.