Skip to main content

FastAPI

Pronunciation
FAST ay-pee-EYE
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/fastapi

In short

FastAPI is a modern Python API framework that uses standard type hints to validate requests, convert data and generate OpenAPI documentation automatically.

What is FastAPI?

FastAPI was created by Sebastián Ramírez and released in 2018. Its key idea is that the type hints you already write in Python describe your API. A path function declared with item_id: int and a body typed as a Pydantic model tells FastAPI what to expect, and it validates every incoming request against that, returning clear errors when the data doesn't fit.

The same types produce documentation. Every FastAPI app serves an OpenAPI schema and interactive docs at /docs, where you can read every endpoint and try it from the browser. Editors also understand the types, so autocompletion works throughout the code. Dependency injection with Depends handles shared pieces such as database sessions and authentication.

FastAPI is built on Starlette and the ASGI standard, so it supports async endpoints, WebSockets and background tasks, and it runs on servers such as Uvicorn. It has become one of the most popular Python web frameworks, especially for machine learning model APIs and backends that serve JSON to a separate front end.

A common misconception is that FastAPI is automatically faster than everything else. It is among the fastest Python frameworks thanks to async I/O, but blocking code inside an async function, such as a synchronous database driver, stalls the server; ordinary def endpoints run in a thread pool for exactly that reason.

Key takeaways

  • FastAPI builds APIs in Python using standard type hints.
  • Types drive request validation through Pydantic models.
  • Interactive OpenAPI docs are generated automatically at /docs.
  • It is async-first, built on Starlette and ASGI, and run with Uvicorn.
  • Blocking calls inside async endpoints slow the whole server down.

Example

A typed endpoint with validationpython
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field

app = FastAPI()

class BookIn(BaseModel):
    title: str = Field(min_length=1)
    year: int = Field(ge=1450)

books: dict[int, BookIn] = {}

@app.post("/books", status_code=201)
def create_book(book: BookIn):          # body validated against BookIn
    book_id = len(books) + 1
    books[book_id] = book
    return {"id": book_id, **book.model_dump()}

@app.get("/books/{book_id}")
def get_book(book_id: int):            # "abc" is rejected with a 422 error
    if book_id not in books:
        raise HTTPException(status_code=404, detail="Not found")
    return books[book_id]

# Run with:  uvicorn main:app --reload   → docs at http://localhost:8000/docs

Readers ask

FastAPI or Django?

FastAPI is focused on APIs, with type-driven validation and async support. Django is a full framework with an ORM, admin panel, templates and authentication built in. FastAPI suits JSON APIs and services; Django suits full web applications.

FastAPI or Flask?

Both are lightweight. FastAPI adds automatic validation, async support and generated docs from type hints. Flask is older and simpler, with a huge ecosystem of extensions.

What is Pydantic?

A Python library that validates data using type hints. FastAPI uses Pydantic models to check request bodies and to shape responses.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings