FastAPI is a Python web framework for building APIs, built around standard Python type hints instead of a separate schema language or heavy configuration. You declare your request and response shapes as normal Python types and Pydantic models, and FastAPI uses those declarations to validate incoming data, serialize responses, and generate interactive API documentation automatically. It's aimed at developers building HTTP APIs and backend services in Python who want editor autocompletion, fewer manual validation bugs, and documentation that stays in sync with the code, without switching languages for the parts of their stack that need to be fast.
def functions or async def coroutines, and FastAPI handles both.fastapi CLI: a command-line tool (fastapi dev, fastapi run) for running the development server with auto-reload, and fastapi deploy for deploying directly to FastAPI Cloud.datetime and UUID values and database models, back into JSON for responses.FastAPI fits teams building HTTP APIs and backend services in Python, especially where request and response validation and documentation quality matter: internal microservices, public REST APIs, ML model serving endpoints, and backend-for-frontend layers. It's a strong choice if your team already writes Python and wants type safety and automatic docs without adopting a separate framework or schema tool for validation.
It's a weaker fit if your team isn't using Python already and validation and documentation tooling alone wouldn't justify picking up a new language. It's also focused on APIs rather than full server-rendered websites; while Starlette supports templating (Jinja2 is listed as an optional dependency), teams building a traditional multi-page server-rendered site with heavy templating needs may be better served by a framework built around that use case, like Django. And if what you actually need is a command-line tool rather than a web API, the README points to Typer, described as "the FastAPI of CLIs," built by the same author with a similar type-hint-driven approach.
Create and activate a virtual environment, then install FastAPI with its standard optional dependencies:
pip install "fastapi[standard]"
Create a file main.py:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}
Run the development server:
fastapi dev
This serves the app at http://127.0.0.1:8000, with interactive docs at /docs (Swagger UI) and /redoc (ReDoc). For a minimal install without the standard extras, which include Uvicorn, the CLI, and other optional dependencies, use pip install fastapi instead; there's also a pip install "fastapi[standard-no-fastapi-cloud-cli]" option if you want the standard extras but not the FastAPI Cloud deployment tooling. Deployment to FastAPI Cloud is available with a single fastapi deploy command, or you can deploy the app to any cloud provider since it's standards-based and not tied to one host.