Chapter 2, Basic
Installing and running the first app
In a few minutes you have an API up and running. The important thing is to do this within an isolated environment, and to know what each command does.
Requirements
The current FastAPI requires Python 3.10 or higher. To check which Python you have:
python3 --version
On Windows, use python --version (or py --version). If the version is older than 3.10, update before continuing.
Create the project and install
A virtual environment isolates the dependencies of each project, so that one does not break the other. Choose the tool you use:
uv init minha-api
cd minha-api
uv add "fastapi[standard]"
The uv add creates the environment, installs the package, and registers it in pyproject.toml. To run any command inside the environment, use the prefix uv run.
Installing without a virtual environment works, but it's a bad idea: two projects that need different versions of FastAPI will start fighting for the same Python. The quotes in "fastapi[standard]" prevent the terminal from interpreting the brackets.
The extra [standard] brings the uvicorn (the server that runs the application), the command fastapi and the dependencies for forms and uploads. In tests, you will also want the HTTP client that the TestClient uses:
uv add --dev pytest httpx2
The first app
Create the file main.py:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"mensagem": "Olá, FastAPI"}
To see what the route returns without starting a server, the TestClient makes the request in memory. The entire course uses this feature to show real results:
from fastapi.testclient import TestClient
cliente = TestClient(app)
resposta = cliente.get("/")
print(resposta.status_code, resposta.json())
200 {'mensagem': 'Olá, FastAPI'}
Run the server
The FastAPI development command starts the server and restarts itself every time you save the file:
uv run fastapi dev main.py
Open http://127.0.0.1:8000 in the browser to see the JSON, and http://127.0.0.1:8000/docs for the interactive documentation. The classic way, which you will find in much material, calls the uvicorn directly and gives the same result:
uv run uvicorn main:app --reload
The text main:app means "in the module main, the object called app". In production, the command is fastapi run main.py (without automatic reload), and chapter 30 details this.
The pages that come for free
FastAPI automatically creates three addresses in addition to your routes:
print(cliente.get("/docs").status_code, cliente.get("/redoc").status_code, cliente.get("/openapi.json").status_code)
200 200 200
| Address | What is |
|---|---|
/docs | Swagger UI: lists the routes and lets you test them |
/redoc | The same documentation, in a readable format |
/openapi.json | The API description, which the other two pages use |
What is `--reload`
Automatic reloading is only for development. It watches the files and restarts the process on each change, which consumes resources and is not safe in production.
Exercise 1
A health route
Create a app with the route GET /saude that returns {"status": "ok"}, and check with the TestClient. Routes like this are used by deployment platforms to know if the application is alive.