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




