liveBook · Manning

FastAPI

As explained in Road to Kubernetes

FastAPI is a Python web framework for building APIs.

FastAPI is a Python web framework for building HTML-driven or API-driven websites without reimplementing common web features. It combines a small feature set with familiar Python decorators and functions. section 2.1

Create an application by initializing FastAPI, mapping functions to HTTP routes and methods, and returning JSON-serializable Python dictionaries. Its API-first and asynchronous approach supports REST APIs while keeping the basic code concise. section 2.1.2

A minimal development setup uses:

  • Python 3.8 or higher with a virtual environment for isolated project dependencies. section 2.1
  • Uvicorn to handle asynchronous web traffic and run the application during development. section 2.1.2
  • Additional route decorators such as @app.get("/api/v1/hello-world/") for distinct URL and HTTP-method handlers. section 2.1.3

Run a first API

Define app = FastAPI(), decorate read_index with @app.get("/"), and return {"Hello": "World"}. Start it with uvicorn src.main:app --reload --port 8011; --reload restarts the application after relevant file changes. section 2.1.2

Listing 2.7 Creating src/main.py
# Import the FastAPI class from the fastapi module.
from fastapi import FastAPI

# Declare an instance of the FastAPI class.
app = FastAPI()

# use the app instance as a decorator to handle an
# HTTP route and HTTP method.
@app.get("/")
def read_index():
    """
    Return a Python Dictionary that supports JSON serialization.
    """
    return {"Hello": "World"}

Shows the minimal FastAPI module: import the class, initialize the application, declare a GET route, and return a dictionary. section 2.1.2

How Road to Kubernetes covers it

How deep it goes
Explains setup, application structure, JSON responses, route handlers, and development serving, but defers production deployment details.
Examples
Demonstrates a Hello World endpoint, a versioned hello-world route, and Uvicorn running on port 8011.
What it assumes
Assumes Python is installed and teaches the initial virtual-environment, package, application, and server setup.
What you can do after
Build it from scratch by creating a FastAPI application, adding route handlers, returning JSON, and running it with Uvicorn.
How it connects
Connects Python, virtual environments, Uvicorn, asynchronous programming, REST APIs, JSON, HTTP methods, and Gunicorn.

Also explained in

Compare how 5 books explain FastAPI →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.