← Back to Tutorials

33. Flask Mount

pip install flask
from fastapi import FastAPI
from flask import Flask, jsonify

app = FastAPI()

# Create a Flask app
flask_app = Flask(__name__)

@flask_app.route("/flask/hello")
def flask_hello():
    return jsonify({"message": "Hello from Flask!"})

@flask_app.route("/flask/items/")
def flask_item(item_id):
    return jsonify({"item_id": item_id, "source": "Flask"})

# Mount Flask app under a prefix
app.mount("/legacy", flask_app)

# FastAPI endpoint alongside Flask
@app.get("/fastapi/hello")
def fastapi_hello():
    return {"message": "Hello from FastAPI!"}

# The Flask app is served at /legacy/flask/...
# The FastAPI app serves /fastapi/...

When to Mount Flask

⚠️ Note: Flask runs via WSGI while FastAPI is ASGI. FastAPI automatically wraps Flask for ASGI compatibility when mounting.
✏️ Exercise: Create a Flask app with 3 endpoints (users, posts, comments). Mount it on a FastAPI app. Add one new FastAPI endpoint alongside it.