Python FastAPI
Python FastAPI Logo.
Introduction
I started in a new interesting project. The project implements an AI solution for one of our corporation’s customers. The cloud infrastructure will be built on Azure, and the AI solution will be implemented using Azure AI services. Most of the AI models have already been implemented by our corporation’s data scientists, but I was asked to join the team to implement the Azure infrastructure and a simple web application that our customer’s employees can use to interact with the AI model.
I was told that the backend of the web application should be be implemented using Python, there were no other restrictions. I have been using Python for a long time, and I have used Flask in my previous projects. But I wanted to try something new. I heard about FastAPI and I wanted to try it. In this blog post, I will tell you about my experiences with FastAPI.
If you are interested in my previous Python blog posts, you can read them here:
- 2017-01-30: Python Rocks!
- 2018-11-01: Java Man Converts to Python
- 2023-04-30: Python REPL
- 2024-04-11: Python Experiences
- 2024-04-18: Using Python and MOTO with AWS Testing
FastAPI
FastAPI is a modern and fast web framework for building APIs with Python. It advertises itself as one of the fastest web frameworks available. The reason for this is that it implements the ASGI standard, which allows it to handle more requests per second than traditional WSGI-based frameworks like Flask.
I’m not going to repeat the FastAPI key features here, you can read more about FastAPI from the FastAPI documentation.
Always Remember to Consult the Experts
When I started the project and heard that I should use Python, I consulted Koodiklinikka experts in the #python
channel. They actually recommended FastAPI for the project. One of the experts even provided me a 36 lines of code example that demonstrated a simple API for a backend using FastAPI. I also examined the full-stack-fastapi-template to get an idea how to structure a bit more complex project.
I didn’t study the FastAPI documentation that much, but I just started to implement the backend using FastAPI. I was surprised how easy it was to implement the backend using FastAPI. I was able to implement the backend in a couple of days.
Some Examples
This is the core of the FastAPI backend:
app.py
:
...
app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json",
generate_unique_id_function=custom_generate_unique_id,
)
app.include_router(api_router, prefix=settings.API_V1_STR)
# Separately since it's not part of the API.
static_path = pathlib.Path(__file__).parent.parent / "genclientstatic"
app.mount("/client", StaticFiles(directory=static_path), name="genclientstatic")
...
Just a couple of lines of code to create the FastAPI app and include the API router. The API router gathers all the API endpoints from the different modules. I also mounted a static directory for the client application, that the backend serves.
The API endpoints are really simple to implement:
import logging
from fastapi import APIRouter
logger = logging.getLogger(__name__)
router = APIRouter()
@router.get("/testing")
def testing() -> str:
logger.debug("Testing api")
return "Hello, World!"
Goodies
FastAPI has a lot of goodies that make it easy to implement the backend. E.g., it creates the OpenAPI documentation automatically from the code (see FastAPI - OpenAPI documentation for more information).
You can then create a frontend client stub from the OpenAPI documentation. See FastAPI - Generate Clients documentation for more information.
You can run the development version of the backend using the following command:
uv run fastapi dev --port 5181
I.e., I use uv as a package and project manager. In that command, uv tells fastAPI to run the Uvicorn server (Uvicorn is an ASGI web server implementation for Python) in development mode. The server automatically watches changes in your code and reloads the server when you save changes.
What Next?
I will use the generated frontend stub to implement a React application using TypeScript, and integrate it with the Python/FastAPI backend. I will write another blog post about those experiences later.
Maybe I will also write another blog post about the Azure infrastructure I’m about to implement for the AI solution, and also for this web application.
Conclusions
FastAPI is a new, fast, and easy-to-use web framework for building APIs with Python. I was able to implement the backend for the web application in a couple of days. I recommend FastAPI for your next Python project.
The writer is working at a major international IT corporation building cloud infrastructures and implementing applications on top of those infrastructures.
Kari Marttila
Kari Marttila’s Home Page in LinkedIn: https://www.linkedin.com/in/karimarttila/