ABUC 2026

FastAPI

Based on https://fastapi.tiangolo.com/#example

Requirements

FastAPI requires Python 3.8 or higher.

To run a FastAPI application in a production environment, you need an ASGI (Asynchronous Server Gateway Interface) server. A common choice is Uvicorn, a high-performance ASGI server that enables FastAPI to handle multiple concurrent requests efficiently.

Installation

python3 -m pip install fastapi
python3 -m pip install "uvicorn[standard]"

First basic example

The basic “Hello world” example (file main0.py) looks like this:

from fastapi import FastAPI
import random

app = FastAPI()

# Fake in-memory database with 5 sensors
sensors = {
    1: {"name": "Temperature Sensor", "value": 22.5},
    2: {"name": "Humidity Sensor", "value": 55.2},
    3: {"name": "Pressure Sensor", "value": 1013.2},
    4: {"name": "Light Sensor", "value": 300.0},
    5: {"name": "CO2 Sensor", "value": 420.5},
}

@app.get("/")
def read_root():
    return {"Hello": "World"}
    
@app.get("/sensors/{sensor_id}")
def read_sensor(sensor_id: int):
    if sensor_id not in sensors:
        return {
            "sensor_id": sensor_id,
            "sensor_value": -1
        }

    return {
        "sensor_id": sensor_id,
        "sensor_name": sensors[sensor_id]["name"],
        "sensor_value": sensors[sensor_id]["value"]
    }

Run the server with: uvicorn main:app

About the command:

You’ll get something like:

INFO:     Will watch for changes in these directories: ['/Users/pietro/Library/...']
INFO:     Uvicorn running on <http://127.0.0.1:8000> (Press CTRL+C to quit)
INFO:     Started reloader process [3319] using WatchFiles
INFO:     Started server process [3321]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

Open your browser at http://127.0.0.1:8000/

You will see the JSON response as:

{
Hello: "World"
}

This is clearly the basic option offered by: