Skip to content
IT glossary
BackendUpdated December 3, 2026

FastAPI

FastAPI is a Python framework for building web APIs. It reads type hints to validate requests and generate documentation that clients can use to try the API.

What FastAPI does

A web API turns requests like GET /items/42 into calls to application code. With FastAPI, you register a Python function for an HTTP method and path, then declare what input the function expects. FastAPI handles the plumbing: extracting values from the request, checking them, and turning the function’s result into an HTTP response, usually JSON.

FastAPI builds on Starlette for web handling and uses Pydantic for data validation. It is the application framework, not the server: an ASGI server such as Uvicorn accepts connections and passes requests to the app.

From type hints to an API contract

In a route like /items/{item_id}, the declaration item_id: int tells FastAPI to take that part of the URL and parse it as an integer. A parameter typed as a Pydantic model is normally read from the request body. When request data fails validation, FastAPI normally sends a 422 response with details instead of calling the handler.

Those declarations also feed into the generated OpenAPI schema. Client developers can use the schema to see which fields are required and try endpoints in Swagger UI, which is served at /docs by default. A declared response_model can validate and filter the output, making the public API contract independent of the full internal object.

Example: a path value and a query option

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
def read_item(item_id: int, include_stock: bool = False):
    item = {"id": item_id}
    if include_stock:
        item["stock"] = 7
    return item

Calling GET /items/42?include_stock=true produces {"id": 42, "stock": 7}. The path supplies the ID, and the query string supplies the flag. A request for /items/abc fails validation, but a numeric ID does not prove that an item exists. A real handler would still need to look it up and return 404 if it was missing.

Where it fits and what to watch for

FastAPI is used for APIs that serve frontends, internal services, or machine learning models. It does not require a particular database or ship with an ORM or admin interface. Its Depends system lets routes reuse steps such as getting a database session or resolving the current user. Tests can replace those dependencies without rewriting the route.

Use async def when the handler awaits nonblocking I/O, such as a network call through an async client. A blocking call made directly inside that handler holds up the event loop. FastAPI runs plain def handlers in a thread pool, which is useful with synchronous libraries. Neither choice makes CPU-heavy work faster on its own.

What interviewers look for

Be ready to trace a request from the URL to the handler and explain which errors the framework handles for you. A common follow-up is to show an async route that calls a synchronous client and ask why unrelated requests start taking longer.