Skip to the content.

FastAPI

Information

Introduction

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.8+ based on standard Python type hints. It is one of the fastest Python frameworks available, only slower than Starlette and Uvicorn themselves.

Key Features

Similar Frameworks

Installation

Prerequisites

FastAPI requires Python 3.8+.

Development Environment Setup (venv)

Following the standard usage in this project, it is recommended to use a virtual environment (venv) to isolate your project dependencies.

Windows:

REM Create a virtual environment
py -m venv .venv

REM Activate the virtual environment
.venv\Scripts\activate

Unix/Linux/macOS:

# Create a virtual environment
python3 -m venv .venv

# Activate the virtual environment
source .venv/bin/activate

Installing FastAPI and Uvicorn

Once the virtual environment is activated, install FastAPI and uvicorn (which serves as the ASGI server).

pip install fastapi
pip install "uvicorn[standard]"

Usage, tips and tricks

Basic REST API Example

Create a file named main.py:

from typing import Union
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: Union[str, None] = None):
    return {"item_id": item_id, "q": q}

Running the API

Run the server with:

uvicorn main:app --reload

Integrating MCP support

If you are developing a microservice with FastAPI, you can easily add Model Context Protocol (MCP) support using FastMCP. This allows AI models to interact with your service logic.

See the FastMCP guide: Adding MCP to an Existing FastAPI Microservice for a complete example and configuration.

Interactive API Documentation

Once the server is running, you can access the automatic interactive API documentation:

See also