Calmcode - fastapi: routes

Defining routes in FastAPI.

1 2 3 4 5 6 7 8 9

In this library you'll use decorators like @get.route("/") to describe the where the request can go while the function that follows describes how it is handled. Note that these functions are typically typed, which means that you can explicitly state what type of variables it should assume.

The app.py file at the end of our video looks like this;

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root():
    return {"message": "hello world again"}

@app.get("/users/{user_id}")
def read_user(user_id: str):
    return {"user_id": user_id}