Favicon of fastapi

fastapi

A Python web framework for building APIs with type hints, automatic data validation, and generated OpenAPI docs.

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.

Key features

  • Type hint-driven validation: request bodies, path parameters, query parameters, headers, cookies, and forms are declared as Python types, and FastAPI validates and converts them automatically, returning clear errors when the data doesn't match.
  • High performance: built on Starlette and Pydantic, with independent TechEmpower benchmarks placing FastAPI among the fastest Python frameworks available, close behind Starlette and Uvicorn themselves.
  • Automatic interactive docs: generates OpenAPI-compliant documentation with two ready-to-use interfaces, Swagger UI and ReDoc, without any extra configuration.
  • Async and sync support: endpoints can be written as normal def functions or async def coroutines, and FastAPI handles both.
  • Editor support: because everything is typed, editors can offer real completion and type checking throughout your API code, not just in isolated spots.
  • Dependency injection: includes a dependency injection system for sharing logic like authentication or database sessions across endpoints.
  • Built-in auth support: includes support for OAuth2 with JWT tokens and HTTP Basic auth, along with WebSockets, CORS, and cookie sessions, inherited from Starlette.
  • 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.
  • Data conversion both ways: converts incoming data (JSON, path parameters, query parameters, cookies, headers, forms, files) into Python types, and converts Python types and objects, including datetime and UUID values and database models, back into JSON for responses.

Ideal use cases

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.

Installation

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.

Categories:

Frequently asked questions

Share:

Stars
100.3K
Forks
9.6K
Last commit
2 days ago
Repository age
8 years
License
MIT
Self-hosted
No
Activity score
89/100
View Repository
Built with:

Similar to fastapi

Favicon

 

  
  
Favicon

 

  
  
Favicon