How does FastAPI speak REST?
Let’s create a minimal FastAPI application – a classic Hello World example – and start examining how FastAPI structures the endpoints. I use the term endpoint to specify a unique combination of an URL (which will always be the same – in our case, our development server – that is, localhost:8000
), a path (the part after the slash), and an HTTP method. In a new folder named Chapter3
, for example, create a new Python file using Visual Studio Code:
chapter3_first_endpoint.py
from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello FastAPI"}
In just a few lines of code, we were able to accomplish several things. So, let’s break down what each part does.
In the first line of chapter3_first_endpoint.py
, we imported the FastAPI class from the fastapi
package. Then, we instantiated an application object (we called...