FastAPI explained

FastAPI
Logo Size:200px
Developer:Sebastián Ramírez
Released:[1]
Programming Language:Python
Genre:Web framework
License:MIT

FastAPI is a web framework for building HTTP-based service APIs in Python 3.8+.[2] It uses Pydantic and type hints to validate, serialize and deserialize data. FastAPI also automatically generates OpenAPI documentation for APIs built with it.[3] It was first released in 2018.

Components

Pydantic

Pydantic is a data validation library for Python. While writing code in an IDE, Pydantic provides type hints based on annotations.[4] FastAPI extensively utilizes Pydantic models for data validation, serialization, and automatic API documentation. These models are using standard Python type hints, providing a declarative way to specify the structure and types of data for incoming requests (e.g., HTTP bodies) and outgoing responses.[5] from fastapi import FastAPIfrom pydantic import BaseModel

app = FastAPI

class Item(BaseModel): name: str price: float is_offer: bool | None = None

@app.post("/items/")def create_item(item: Item): # The 'item' object is already validated and typed return

Starlette

Starlette is a lightweight ASGI framework/toolkit, to support async functionality in Python.[6]

Uvicorn

Uvicorn is a minimal low-level server/application web server for async frameworks, following the ASGI specification. Technically, it implements a multi-process model with one main process, which is responsible for managing a pool of worker processes and distributing incoming HTTP requests to them. The number of worker processes is pre-configured, but can also be adjusted up or down at runtime.[7]

OpenAPI integration

FastAPI automatically generates OpenAPI documentation for APIs. This documentation includes both Swagger UI and ReDoc, which provide interactive API documentation that you can use to explore and test your endpoints in real time. This is particularly useful for developing, testing, and sharing APIs with other developers or users. Swagger UI is accessible by default at /docs and ReDoc at /redoc route.[8]

Features

Asynchronous operations

FastAPI's architecture inherently supports asynchronous programming. This design allows the single-threaded event loop to handle a large number of concurrent requests efficiently, particularly when dealing with I/O-bound operations like database queries or external API calls. For reference, see async/await pattern.

Dependency injection

FastAPI incorporates a Dependency Injection (DI) system to manage and provide services to HTTP endpoints. This mechanism allows developers to declare components such as database sessions or authentication logic as function parameters. FastAPI automatically resolves these dependencies for each request, injecting the necessary instances.[9] from fastapi import Depends, HTTPException, statusfrom db import DbSession

  1. --- Dependency for Database Session ---

def get_db: db = DbSession try: yield db finally: db.close

@app.post("/items/", status_code=status.HTTP_201_CREATED)def create_item(name: str, description: str, db: DbSession = Depends(get_db)): new_item = Item(name=name, description=description) db.add(new_item) db.commit db.refresh(new_item) return

@app.get("/items/")def read_item(item_id: int, db: DbSession = Depends(get_db)): item = db.query(Item).filter(Item.id

item_id).first if item is None: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Item not found") return item

WebSockets support

WebSockets allow full-duplex communication between a client and the server. This capability is fundamental for applications requiring continuous data exchange, such as instant messaging platforms, live data dashboards, or multiplayer online games. FastAPI leverages the underlying Starlette implementation, allowing for efficient management of connections and message handling.[10]

  1. You must have 'websockets' package installed

from fastapi import WebSocket

@app.websocket("/ws")async def websocket_endpoint(websocket: WebSocket): await websocket.accept while True: data = await websocket.receive_text await websocket.send_text(f"Message text was: ")

Background tasks

FastAPI enables the execution of background tasks after an HTTP response has been sent to the client. This allows the API to immediately respond to user requests while simultaneously processing non-critical or time-consuming operations in the background. Typical applications include sending email notifications, updating caches, or performing data post-processing.[11] import timeimport shutilfrom fastapi import BackgroundTasks, UploadFile, Filefrom utils import generate_thumbnail

@app.post("/upload-image/")async def upload_image(image: UploadFile = File(...), background_tasks: BackgroundTasks): file_location = f"uploaded_images/" # Save uploaded image with open(image_path, "wb") as f: contents = await file.read f.write(contents)

# Add thumbnail generation as a background task background_tasks.add_task(generate_thumbnail, file_location, "200x200")

return

Example

The following code shows a simple web application that displays "Hello, World!" when visited:

  1. Import FastAPI class from the fastapi package

from fastapi import FastAPI

  1. Create an instance of the FastAPI app

app = FastAPI

  1. Define a GET route for the root URL ("/")

@app.get("/")async def read_root -> str: # Return a plain text response return "Hello, World!"

See also

Notes and References

  1. Web site: fastapi repo . . 2018-12-05.
  2. Web site: FastAPI . 2024-04-10 . fastapi.tiangolo.com.
  3. Book: Lubanovic . Bill . Introducing Python: Modern Computing in Simple Packages . 2019-11-06 . O'Reilly Media, Inc . 9781492051367 . 397, 418 . 2nd .
  4. Web site: Why use Pydantic - Pydantic . 2023-09-21 . docs.pydantic.dev.
  5. Web site: Request Body - FastAPI . 2025-06-30 . fastapi.tiangolo.com . en.
  6. Web site: Starlette . 2023-09-21 . www.starlette.io.
  7. Web site: Restarting 'uvicorn' Workers with the 'SIGHUP' Signal . 2024-06-17 . bugfactory.io.
  8. Web site: OpenAPI docs - FastAPI .
  9. Web site: Dependencies . 30 June 2025.
  10. Web site: WebSockets - FastAPI . 2025-06-30 . fastapi.tiangolo.com . en.
  11. Web site: Background Tasks - FastAPI . 2025-06-30 . fastapi.tiangolo.com . en.