Creating the first endpoint and running it locally
FastAPI is a framework that aims at being 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:
chapter3_first_endpoint_01.py
from fastapi import FastAPI app = 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
. This will be the main application object that will wire all of the API routes.
Then, we simply define a coroutine that contains our route logic, the path operation function. Its return value is automatically...