FastAPI – A modern, fast (high-performance), web framework for building APIs with Python 3.6+

Use examples when they help make things clearer.

FastAPI is a modern, fast (high-performance) web framework for building APIs with Python that uses standard modern Python types instead of some weird syntax or classes from a specific library. This makes it much easier to learn and understand than other frameworks out there. It also provides automatic interactive API documentation that can be accessed at http://127.0.0.1:8000/docs or http://127.0.0.1:8000/redoc, allowing you to easily interact with the API and see how it works without having to write any code yourself!

FastAPI is a powerful tool for building APIs that’s both fast and easy to use. It supports standard modern Python types instead of some weird syntax or classes from a specific library, which makes it much easier to learn and understand than other frameworks out there. With its automatic interactive API documentation, you can easily see how your API works without having to write any code yourself!
For example, let’s say you want to create an API that receives HTTP requests in the paths / and /items/{item_id}. Both paths take GET operations (which means they just send data back without changing anything). The path /items/{item_id} has a path parameter item_id that should be an int.
To do this, you would create a file called main.py with the following code:

# Importing necessary modules
from typing import Union # Importing Union from typing module to specify multiple types for a variable
from fastapi import FastAPI # Importing FastAPI module for creating API

# Creating an instance of FastAPI
app = FastAPI()

# Defining a route for the root path
@app.get("/")
def read_root():
    return {"Hello": "World"} # Returns a dictionary with a key-value pair

# Defining a route for the path /items/{item_id}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None): # Specifying the type of item_id as int and q as Union of str and None
    return {"item_id": item_id, "q": q} # Returns a dictionary with item_id and q as keys and their respective values

Then you would run the server with this command in your terminal:

bash
# This script starts a development server for a FastAPI application
# It automatically reloads when changes are made to the code

# Command to run the server
uvicorn main:app --reload  # main:app specifies the main module and app variable to be used by uvicorn

# main.py is the main module of the FastAPI application
# --reload flag enables automatic reloading when changes are made to the code
# This ensures that the server always runs with the latest code changes

# To run the server, open your terminal and enter the following command:
# uvicorn main:app --reload
# Replace "main" with the name of your main module and "app" with the name of your FastAPI application variable
# For example, if your main module is named "server" and your FastAPI application variable is named "api", the command would be:
# uvicorn server:api --reload

Now go to http://127.0.0.1:8000/items/5?q=somequery in your browser and you’ll see the JSON response as {“item_id”: 5, “q”: “somequery”}!
FastAPI also provides automatic interactive API documentation that can be accessed at http://127.0.0.1:8000/docs or http://127.0.0.1:8000/redoc (depending on your preference). This allows you to easily interact with the API and see how it works without having to write any code yourself!
Overall, FastAPI is a powerful tool for building APIs that’s both fast and easy to use. It supports standard modern Python types instead of some weird syntax or classes from a specific library, which makes it much easier to learn and understand than other frameworks out there. Plus, with its automatic interactive API documentation, you can easily see how your API works without having to write any code yourself!
In terms of performance, FastAPI applications running under Uvicorn are one of the fastest Python frameworks available, only below Starlette and Uvicorn themselves (used internally by FastAPI). To understand more about it, see the section Benchmarks.

SICORPS