Creating a first endpoint and running it locally
FastAPI is a framework that is easy to use and quick to write. In the following example, you’ll realize that this is not just a promise. In fact, creating an API endpoint involves just a few lines:
chapter03_first_endpoint_01.py
from fastapi import FastAPIapp = FastAPI() @app.get("/") async def hello_world(): return {"hello": "world"}
In this example, we define a GET
endpoint at the root path, which always returns the {"hello": "world"}
JSON response. To do this, we first instantiate a FastAPI object, app
. It will be the main application object that will wire all the API routes.
Then, we simply define a coroutine that contains our route logic, the path operation function. Its return value is automatically...