Series | Web frameworks in Python - FastAPI
FastAPI is a modern, fast (high-performance), a web framework for building APIs with Python 3.6+ based on standard Python-type hints.

full stack dev | SaaS | AI | maker - builder
Search for a command to run...
FastAPI is a modern, fast (high-performance), a web framework for building APIs with Python 3.6+ based on standard Python-type hints.

full stack dev | SaaS | AI | maker - builder
No comments yet. Be the first to comment.
In this series, we will cover Web frameworks, tools and libraries from Python eco-system.
Bottle, a micro-framework built with Python Standard Library and no additional dependency!
Because sometimes your models need to gossip with each other

Whether you are a SysAdmin or Causal Linux (Ubuntu/Debian) User, here is a Guide to Rescue Your Development Environment Without Breaking a Sweat

Because Not All Code Guards Need to Break the Bank

Harnessing the Power of Pydantic for Seamless AI Integration

From Simple Chat-bots to Autonomous Digital Assistants

Here are some of the main features of FastAPI, along with examples of how they can be used:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
from fastapi import FastAPI, Depends
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
app = FastAPI()
def validate_item(item: Item):
if item.tax is None:
item.tax = item.price * 0.1
return item
@app.post("/items/")
def create_item(item: Item = Depends(validate_item)):
return item
from fastapi import FastAPI
app = FastAPI()
@app.get("/users/")
async def read_users():
return await fetch_users_from_database()
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
def read_item(item_id: int):
return {"item_id": item_id}
These are just a few examples of the features of FastAPI. There are many other features available as well, such as support for authentication and authorization, built-in support for WebSockets, and more.
Here is an example of how you might use FastAPI to create a simple API endpoint:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
The FastAPI class is the main entry point of the framework. An instance of this class is created and is passed to functions decorated with app.route as the first argument. The @app.route decorator creates an endpoint for your application. The first endpoint is a simple "Hello World" greeting, accessible at the "/" path. The second endpoint is for retrieving an item by ID, and takes an optional query parameter q.
Uvicorn is a high-performance web server that is often used to run FastAPI applications in production. It is built on top of the asyncio library, which allows it to handle a large number of concurrent connections efficiently.
One of the key features of Uvicorn is its built-in support for the ASGI (Asynchronous Server Gateway Interface) specification. This allows Uvicorn to work seamlessly with a wide variety of web frameworks, including FastAPI.
Here's an example of how you might use Uvicorn to run a FastAPI application:
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)
You can access the endpoints via browser by visiting http://127.0.0.1:8000/ and http://127.0.0.1:8000/items/42?q=test
In this example, the uvicorn.run() function is used to start the Uvicorn server, passing in the FastAPI app as the first argument. The host and port arguments are used to configure the server to listen on all available network interfaces (0.0.0.0) on port 8000.
You can also configure uvicorn to run as a daemon, logging, workers and other options with uvicorn command.
uvicorn main:app --workers 4 --host 0.0.0.0 --port 8000
Uvicorn is known for its high performance and can handle a large number of concurrent connections, which makes it well-suited for running production-grade applications. Also, it has built-in support for ASGI, it can be used with other frameworks like Starlette, Django Channels, etc.
It's worth mentioning that you could use other servers like Gunicorn as well with FastAPI, but Uvicorn is specially designed for running async web applications, so it's been widely adopted for running a fastapi application.
FastAPI is my go-to framework for building a web / RESTfUL API server; be it a backend for web & mobile apps or hosting ML models on the cloud, FastAPI is a simple yet powerful tool to build for such use cases.