Start a FastAPI Project with uv

In progress

Goal

A new FastAPI project with its dependencies locked by uv and a development server that reloads on save.

Prerequisites

  • uv installed (brew install uv or the official installer).

Steps

  1. Create the project:
    uv init fastapi-app
    cd fastapi-app
    
  2. Add FastAPI with its standard extras (Uvicorn, the fastapi CLI, Jinja2, python-multipart, httpx…):
    uv add "fastapi[standard]"
    
  3. 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"}]
    
  4. 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/items returns the JSON list.
  • http://127.0.0.1:8000/docs shows the generated Swagger UI.

Troubleshooting

  • fastapi: command not found: run it through uv run, or install the standard extra (the CLI comes with it).
  • Changes not picked up: fastapi dev reloads, fastapi run (production mode) doesn't.
  • zsh: no matches found: fastapi[standard]: zsh expands the square brackets; keep the quotes around "fastapi[standard]".

References