Tips and Tricks


Mount a Flask or Django app inside a FastAPI application

FastAPI tip:

You can use WSGIMiddleware to mount WSGI applications (like Flask and Django) to your FastAPI API.


from fastapi import FastAPI
from fastapi.middleware.wsgi import WSGIMiddleware
from flask import Flask, escape, request

flask_app = Flask(__name__)

def flask_main():
    name = request.args.get("name", "World")
    return f"Hello, {escape(name)} from Flask!"

app = FastAPI()

def read_main():
    return {"message": "Hello World"}

app.mount("/v1", WSGIMiddleware(flask_app))