By Kevin McAleer, 5 Minutes
Page last updated November 24, 2026

By the end of this lesson you will be able to create a virtual environment, install FastAPI, and run a tiny robot API that answers in your browser.
Let’s build somewhere safe to work. A virtual environment is a private folder of Python packages for one project. Without it, every project shares one pile of packages, and upgrading FastAPI for this course could break something else on your computer.
Make a project folder and a virtual environment inside it:
$ mkdir robot-api
$ cd robot-api
$ python3 -m venv .venv
$ source .venv/bin/activate
On Windows, the last line is .venv\Scripts\activate. When it works, your prompt starts with (.venv). Run the activate line again whenever you open a new terminal.
$ pip install "fastapi[standard]"
The quotes matter in some shells, because they stop [ and ] being treated as special characters. The [standard] extra installs FastAPI plus the tools we need: the fastapi command, the Uvicorn server, httpx for testing and email-validator for checking email addresses. Check what you got:
$ pip list | grep -i -E "fastapi|pydantic|uvicorn|starlette"
When this course was tested the versions were FastAPI 0.165.0, Pydantic 2.14.4, Starlette 1.10.0 and Uvicorn 0.54.0. We will add two more packages for passwords and tokens in lesson 4.
Create a file called main.py in the robot-api folder:
from fastapi import FastAPI
app = FastAPI(title="Robot Telemetry API")
@app.get("/")
def status():
"""Say hello and report that the robot API is up."""
return {"robot": "pico-bot", "status": "online"}
@app.get("/ping")
def ping():
return {"pong": True}
Three things are happening here. app = FastAPI(...) creates the application. @app.get("/") says “when someone sends a GET request to the root address, run the function underneath”. The function returns a dictionary, and FastAPI turns it into JSON.
$ fastapi dev main.py
You should see lines like these:
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
Open http://127.0.0.1:8000/ and you will see {"robot":"pico-bot","status":"online"}. Open http://127.0.0.1:8000/docs for the interactive documentation FastAPI wrote for you. Click a route, choose Try it out, then Execute.
fastapi dev is for development only. It watches your files and restarts the server when you save, and it only listens on your own computer. Behind the scenes it starts Uvicorn, the program that actually speaks HTTP. You can start Uvicorn yourself, which is what you will see in many tutorials:
$ uvicorn main:app --reload
main:app means “in the file main.py, use the object called app”. Stop the server with Ctrl+C.
flowchart LR
B[Browser or robot] -->|HTTP request| U[Uvicorn server]
U -->|calls| F[FastAPI app in main.py]
F -->|returns dict| U
U -->|JSON response| B
Teachers: Installing packages needs internet access and permission to write to the learner’s folder. On locked-down school computers,
python3 -m venvorpipmay be blocked, and a school firewall may stop port 8000. Test the setup on one machine first. If it is blocked, run the course in a Raspberry Pi, a cloud workspace or a Codespace instead.
/leds, that returns {"red": false, "green": false, "blue": false}. Save the file and reload the page without restarting the server.title of the app and watch it change at /docs.uvicorn main:app --port 8010, and visit the new port./robot/name that returns your own robot’s name and a made-up battery percentage.fastapi: command not foundsource .venv/bin/activate) and check that pip install "fastapi[standard]" finished without errors.Why: The fastapi command is installed inside .venv, so it only exists while the environment is active.
Error: [Errno 98] address already in usefastapi dev main.py --port 8001.Why: Only one program can listen on a port at a time.
Could not import module "main"main.py, and check the file is not called main.py.txt.main.py in the current folder.Quick quiz 1 of 3
Why do we create a virtual environment for each project?
Quick quiz 2 of 3
What does uvicorn main:app mean?
Quick quiz 3 of 3
Which command is meant for development, because it restarts when you save?
fastapi dev turns on auto-reload and only listens on your own computer.
Comments