Start a FastAPI Project with uv
Goal
A new FastAPI project with its dependencies locked by uv and a development server that reloads on save.
Prerequisites
- uv installed (
brew install uvor the official installer).
Steps
- Create the project:
uv init fastapi-app cd fastapi-app - Add FastAPI with its standard extras (Uvicorn, the
fastapiCLI, Jinja2,python-multipart,httpx…):uv add "fastapi[standard]" - Write a first endpoint in
main.py. Decorators take the path only, never the full URL:from fastapi import FastAPI app = FastAPI() @app.get("/items") async def get_items(): return [{"id": 101, "item": "Keyboard"}, {"id": 102, "item": "Monitor"}] - Start the development server, which reloads on every change:
uv run fastapi dev main.py
Without uv: python -m venv .venv, activate it, pip install "fastapi[standard]", then fastapi dev main.py (or uvicorn main:app --reload).
Verify
-
curl http://127.0.0.1:8000/itemsreturns the JSON list. -
http://127.0.0.1:8000/docsshows the generated Swagger UI.
Troubleshooting
fastapi: command not found: run it throughuv run, or install thestandardextra (the CLI comes with it).- Changes not picked up:
fastapi devreloads,fastapi run(production mode) doesn't. zsh: no matches found: fastapi[standard]: zsh expands the square brackets; keep the quotes around"fastapi[standard]".
References
- FastAPI documentation: First Steps - the first endpoint and the docs pages.
- FastAPI documentation: FastAPI CLI -
fastapi devvsfastapi run. - uv documentation: Working on projects -
uv init,uv add,uv run. - FastAPI MOC - hub for FastAPI notes.