Meet Maya: Why FastAPI?
Meet Maya. She runs a small bakery called Sweet Crumbs. Every morning, there is a line out the door. Orders arrive by phone, by sticky notes, and by messages, and by Friday, she has lost track of half of them. So Maya has an idea. An app, where customers can see the menu, and place their own orders. But there is a catch. An app needs something working behind it. And that something is called an API.
What is an API?
So what is an API? Think about a restaurant. You sit at a table, and the kitchen has everything needed to cook. But you never walk into the kitchen. A waiter takes your order to the kitchen, and brings your food back. An API is that waiter. Maya's app is the customer. Her menu and her orders are the kitchen. And the API carries requests and answers between them, using a simple language called HTTP.
Why FastAPI?
There are many ways to build an API. So why FastAPI? First, it is fast to write. You describe what you want in plain Python, and a few lines go a long way. Second, it is fast to run, as one of the quickest Python frameworks. Third, it catches mistakes for you. If an app sends the wrong kind of data, FastAPI rejects it, with a clear message. Fourth, it writes your documentation automatically. And last, it is friendly to beginners. If you know a little Python, you are ready.
What FastAPI is built on
Under the hood, FastAPI stands on two great libraries. Starlette handles the web part. Pydantic handles the data. Your own code only uses ordinary Python type hints, and FastAPI turns those into validation, editor help, and interactive documentation, with almost no extra code.
Set up your environment
Now let's build Maya's API. First, create a virtual environment, and activate it. A virtual environment keeps this project's packages separate from everything else on your computer. Then install FastAPI, with its standard extras, using pip.
python3 -m venv .venv source .venv/bin/activate pip install "fastapi[standard]" Collecting fastapi[standard] Using cached fastapi-0.162.0-py3-none-any.whl.metadata (26 kB) Collecting starlette>=0.46.0 (from fastapi[standard]) Using cached starlette-1.10.0-py3-none-any.whl.metadata (6.6 kB) … (dependencies download) … Successfully installed MarkupSafe-3.0.4 agent-detector-2.0.0 annotated-doc-0.0.5 annotated-types-0.8.0 any… pip list | grep -E "fastapi|pydantic|starlette|uvicorn" fastapi 0.162.0 pydantic 2.14.2 starlette 1.10.0 uvicorn 0.54.0
Your first endpoint
Here is the very first endpoint. We import FastAPI, and create an app, giving it a title. Then we decorate a function with app dot get, and a slash. That means, when someone asks for the home page, run this function. It returns a dictionary with a welcome message, and FastAPI turns it into JSON.
from fastapi import FastAPI
app = FastAPI(title="Sweet Crumbs API")
@app.get("/")
def read_root():
return {"message": "Welcome to Sweet Crumbs!"}Run it and call it
Start the development server with fastapi dev, main dot py. It watches your files, and reloads whenever you save. Now call it with curl. And there is the first answer from Maya's API. Welcome to Sweet Crumbs.
fastapi dev main.py ⚡️ Starting FastAPI in development mode 🐍 Using import string: main:app 🌐 Server started at http://127.0.0.1:8000 Documentation at http://127.0.0.1:8000/docs Logs: INFO: Will watch for changes in these directories: ['~/sweet-crumbs'] INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [29080] using WatchFiles INFO: Started server process [29105] INFO: Waiting for application startup. INFO: Application startup complete. INFO: 127.0.0.1:63578 - "GET / HTTP/1.1" 200 OK
curl localhost:8000 {"message":"Welcome to Sweet Crumbs!"}
Now the magic. Open slash docs in your browser. FastAPI has generated interactive documentation, straight from your code. Expand the endpoint, click Try it out, and then Execute. You get the real response, right here. There is also slash redoc, in a different style, and slash openapi dot json, the machine readable description behind it all.
The complete code for this lesson
Here is the full main.py exactly as it runs at the end of this lesson. Save it, install FastAPI with pip install "fastapi[standard]", and start it with fastapi dev main.py, then open http://127.0.0.1:8000/docs.
from fastapi import FastAPI
app = FastAPI(title="Sweet Crumbs API")
@app.get("/")
def read_root():
return {"message": "Welcome to Sweet Crumbs!"}Summary
In just a few minutes, Maya's bakery has a working API. But right now it only says hello. In the next lesson, her customers want to look up their orders by number, and we will meet path parameters. See you there.
Key takeaways