Released · improving
Framework · Since 2018
FastAPI is a high-performance Python web framework for building APIs, using type hints to drive validation, serialization and automatic API docs.
FastAPI is a modern framework for building web APIs in Python. Sebastián Ramírez first released it in 2018, and it is developed in the open with a large community of contributors. It builds on Starlette for the web layer and Pydantic for data validation, so the type hints on your function parameters are enough to parse and validate requests, serialize responses and generate an OpenAPI description of your API.
FastAPI is a natural fit wherever input validation and documentation matter: REST backends, microservices, machine learning model serving and internal tools. Because it runs on ASGI, async endpoints can handle many concurrent connections efficiently, while plain def functions keep working too. Its dependency injection system lets you factor out database sessions, authentication and settings, and swap them out cleanly in tests.
To get started, create a virtual environment, install it with pip install "fastapi[standard]", run fastapi dev main.py and open /docs to call your endpoints from the interactive documentation. The official Tutorial - User Guide walks step by step from path parameters through security, databases and testing, and is designed to be read in order. Once you are comfortable with Python type hints and Pydantic models, most of FastAPI follows naturally.
Parameter types and Pydantic models are all FastAPI needs to convert and validate request data, returning detailed errors for invalid input.
An OpenAPI schema is generated from your code and served as interactive documentation at /docs (Swagger UI) and /redoc.
Declare database sessions, authentication or shared parameters with Depends; FastAPI resolves them per request and lets tests override them.
Built on Starlette and ASGI, async def endpoints handle many concurrent connections efficiently, and regular def functions work alongside them.
Create and activate a Python virtual environment in your project folder. The command below is for macOS and Linux; on Windows, activate it with .venv\Scripts\activate.
python -m venv .venv && source .venv/bin/activateInstall FastAPI with the standard extras, which bring in commonly used dependencies such as the Uvicorn server and the fastapi command-line tool.
pip install "fastapi[standard]"Create main.py and start the development server. It reloads automatically when you change code, and http://127.0.0.1:8000/docs shows the interactive API documentation.
fastapi dev main.pyFastAPI() creates the application, and the @app.get decorator binds a path and HTTP method to a function. The dictionary the function returns becomes a JSON response. In the second endpoint, item_id: int converts and validates the path value as an integer, while q, which has a default, becomes an optional query parameter. Run fastapi dev main.py and try both endpoints from /docs.
main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, world!"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str | None = None):
return {"item_id": item_id, "q": q}
pip install "fastapi[standard]"
fastapi dev main.pySix chapters that take you from installation to the core ideas of FastAPI.
Ask questions, share experience and trade opinions about FastAPI.
No discussions yet. Start the first one.
0 comments
Sign in · Sign in to leave a comment.
Be the first to comment.