Set up the project, run the server, and make your first API calls.
- Create a virtual environment and install dependencies
- Start the FastAPI dev server
- Use Swagger UI to explore endpoints
- Login and call a protected endpoint
FastAPI is a Python web framework for building REST APIs — servers that receive HTTP requests and return JSON. It is:
- Fast — high performance (uses Starlette + Pydantic)
- Auto-documented — generates Swagger UI at
/docs - Type-safe — uses Python type hints for validation
Uvicorn is the ASGI server that actually runs your FastAPI app. Think of it as the engine; FastAPI is the car.
uvicorn app.main:app --reload
│ │
│ └── FastAPI instance named `app`
└── Python module path
--reload restarts the server when you save code changes (dev only).
cd backend-with-fastapi/task-api
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
make devYou should see:
INFO: Uvicorn running on http://127.0.0.1:8000
INFO: Application startup complete.
curl http://127.0.0.1:8000/api/v1/healthExpected:
{"status":"ok","app":"Task API","version":"1.0.0","environment":"development"}curl http://127.0.0.1:8000/api/v1/readyExpected: "database": "connected"
- Open http://127.0.0.1:8000/docs
- Expand Auth →
POST /api/v1/auth/login - Click Try it out
- Enter:
- username:
user@example.com - password:
user123
- username:
- Click Execute — copy the
access_token
- Click the Authorize button (top right)
- Enter:
Bearer <paste_access_token> - Now try Tasks →
GET /api/v1/tasks
TOKEN="<your_access_token>"
curl -X POST http://127.0.0.1:8000/api/v1/tasks \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"title":"My first task","status":"pending"}'| File | Purpose |
|---|---|
app/main.py |
Entry point — exports app for uvicorn |
app/factory.py |
create_app() — builds and configures FastAPI |
app/core/config.py |
Reads settings from .env |
app/api/v1/endpoints/health.py |
Health + readiness endpoints |
| Mistake | Fix |
|---|---|
command not found: uvicorn |
Activate .venv first |
401 Unauthorized on tasks |
Login and pass Bearer token |
| Login fails with JSON body | Login uses form data, not JSON — use username + password fields |
| Port already in use | Kill old process or change PORT in .env |
- What command starts the dev server?
- What URL shows interactive API docs?
- Why does login use
usernameinstead ofemailin the form?
Answers: (1) make dev or uvicorn app.main:app --reload (2) /docs (3) OAuth2 standard uses username field — we put email there.