FastAPI is also easy to use because it resembles Flask, which should make you feel at home if you’re familiar with that framework. Additionally, FastAPI lets you write concise and expressive code using modern Python syntax, making maintenance cheap.
To better understand how FastAPI works, let’s take a closer look at an example:
# Importing necessary modules
from fastapi import FastAPI # Importing FastAPI module
import uvicorn # Importing uvicorn module for running the server
# Creating an instance of FastAPI
app = FastAPI() # Creating an instance of FastAPI and assigning it to the variable "app"
# Defining a route and its corresponding function
@app.get("/items/{item_id}") # Defining a route with a path parameter "item_id"
async def read_item(item_id: int): # Defining a function to handle the request with the path parameter "item_id" as an integer
return {"item_name": "Foo", "item_id": item_id} # Returning a dictionary with the item name and the value of the path parameter
# Running the server
if __name__ == "__main__": # Checking if the script is being run directly
uvicorn.run("main:app") # Running the server with the "main" module and the "app" instance of FastAPI
# Explanation:
# The script starts by importing the necessary modules, FastAPI and uvicorn.
# Then, an instance of FastAPI is created and assigned to the variable "app".
# Next, a route is defined using the decorator "@app" and the corresponding function is defined to handle the request.
# The function takes in the path parameter "item_id" as an integer and returns a dictionary with the item name and the value of the path parameter.
# Finally, the server is run using uvicorn with the "main" module and the "app" instance of FastAPI.
# This allows the server to handle requests to the defined route and return the desired response.
In this example, we’re using FastAPI to create a simple API that returns an object with the name and ID of an item when given its ID as input. The `FastAPI()` function creates a new instance of our application, which is then used to define routes for handling requests. In this case, we’ve defined one route called `/items/{item_id}` using the `@app.get(“/items/{item_id}”)` decorator.
The `read_item()` function is responsible for handling GET requests to that route and returning a JSON response with the requested item data. The `async def` syntax indicates that this function will be executed asynchronously, which can improve performance in high-traffic scenarios.
To run our application, we’re using uvicorn (which is included as part of FastAPI) to start a server and listen for incoming requests on port 8000. If you want to learn more about how FastAPI works or see some examples, check out the official documentation at https://fastapi.tiangolo.com/.
In terms of performance, independent TechEmpower benchmarks show that FastAPI applications running under Uvicorn are one of the fastest Python frameworks available, only behind Starlette and Uvicorn themselves (which are used internally by FastAPI). To learn more about this, see the section Benchmarks in the official documentation.
Overall, if you’re looking for a modern and efficient web framework for building APIs with Python that combines the best features of existing components like Starlette and Pydantic, then FastAPI is definitely worth checking out!