What is FastAPI?

According to its official website,

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

Hello World with FastAPI

Installation

Requirements

Python3.8+

Install FastAPI

1
pip install fastapi

Install ASGI Server

1
pip install "uvicorn[standard]"

Define a FastAPI app

Create a file main.py with:

1
2
3
4
5
from typing import Union

from fastapi import FastAPI

app = FastAPI()

Create API Endpoints

In main.py add the following 2 endpoints implementation

1
2
3
4
5
6
7
8
@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}

API Endpoints with Automatic Validation

final code looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel


app = FastAPI()


class Book(BaseModel):
    name: str
    price: float
    is_offer: Union[bool, None] = None


@app.get("/")
def home():
    return {"Hello world": "Dec 1st 2023"}


@app.get("/book/{book_id}")
def read_item(book_id: int, q: Union[str, None] = None):
    return {"book_id": book_id, "q": q}


@app.put("/book/{book_id}")
def update_item(book_id: int, book: Book):
    return {"book_id": book_id,  "book_name": book.name}

Test

Run the server with

1
uvicorn main:app --reload

INFO: Will watch for changes in these directories: [’/Users/xxx/PycharmProjects/pythonProject’]
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [99265] using WatchFiles
INFO: Started server process [99267]
INFO: Waiting for application startup.
INFO: Application startup complete.

Open it in the browser and test the endpoints defined above.

API Documentation

FastAPI has interactive API docs built-in by default.

Interactive Docs Powered by Swagger UI

Go to /docs, we can the interactive API docs:

Automatic API Documentation

Automatic API doc with ReDoc

Go to /redoc, we can see another automatic API documentation:

Automatic API Documentation

General Thoughts

FastAPI is generally a lightweight framework to develop APIs with Python. Some attractive advantages of FastAPI for me compared to a comprehensive framework like Django:

  1. fast to set up; fast to build APIs, prototypes, and POCs
  2. automatic validation with Pydantic
  3. Automatic API docs

I would definitely consider FastAPI to be my first option if I want to test some ideas quick, and honest I think it’s also quite attractive to build production systems as well, given more and more web applications are single page applications with React, Vue, or Angular that only requires APIs from backend. Django is a little too heavy for such case.