Getting Started with FastAPI — Build an API in Python
Dev Guides / fastapi-quickstart.html
How to build a REST API in Python with automatic docs, from installation to a minimal working example.
"How do I build an API quickly in Python?"
FastAPI is a web framework for building REST APIs in Python. Declare type hints on your function arguments and return values, and you get request validation and API docs for free. This is the flow the official docs recommend (venv plus uv or pip), up to a minimal running app.
Prepare the environment
- Python 3.9 or newer
- One virtual environment per project (recommended)
Install
The official docs use uv to scaffold a project. If you do not use uv, pip works the same.
# Using uv (official docs approach)
uv init --bare myapi
cd myapi
uv add fastapi "uvicorn[standard]"
# Or pip + venv
python3 -m venv .venv
source .venv/bin/activate
pip install fastapi "uvicorn[standard]"
Minimal example
Put this in main.py.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"message": "hello"}
Run it.
uvicorn main:app --reload
Open http://127.0.0.1:8000 to see the JSON, and http://127.0.0.1:8000/docs for the auto-generated API documentation.
Key ideas to understand
- Decorators like
@app.get("/")register routes. - Type hints drive validation. For example
def read(item_id: int)meansitem_idmust be an integer. /docsis Swagger UI and/redocis ReDoc. Both stay in sync without extra documentation work.
Common problems
ModuleNotFoundErroron run — usually the interpreter differs from where you installed. See this document.--reloadis for development. For production you need a separate process manager (uvicorn, gunicorn, and so on).
Next steps
- Official tutorial sections: Request Body, Path Parameters, Dependencies
- Database access: the official SQL (SQLAlchemy) section
- Deployment: packaging as a Docker image is the common route
Sources
- GitHub fastapi/fastapi — Official repository
- Official docs Official tutorial — Step-by-step
- Official docs Release notes — Version info
Verified date indicates freshness. Check applicability conditions for older material.