FastAPI

FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.8+ based on standard Python type hints

FastAPI Logo

Overview

FastAPI is designed for building APIs with automatic interactive documentation, high performance, and easy-to-use features

Key Features

High Performance

FastAPI is one of the fastest Python frameworks, comparable to NodeJS and Go

Automatic API Documentation

Interactive API documentation automatically generated

from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}

Documentation available at /docs (Swagger UI) and /redoc

Type Hints

Full type hint support

from fastapi import FastAPI
from pydantic import BaseModel

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

@app.post("/items/")
async def create_item(item: Item):
    return item

Async/Await Support

Native async support

@app.get("/")
async def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

Data Validation

Automatic request validation using Pydantic

from pydantic import BaseModel, EmailStr

class User(BaseModel):
    email: EmailStr
    age: int
    name: str

Installation

pip install fastapi uvicorn

Running

uvicorn main:app --reload

Common Use Cases

Advantages

Disadvantages

Official Documentation

FastAPI Documentation

Version Information