All framework guides

Python

Get an OpenAPI spec from Flask

Plain Flask has no spec. flask-smorest adds one, but routes must use its blueprints.

Tool
flask-smorest
Time
15 minutes or more
You get
Live at /openapi.json, plus an openapi.json file

How it works

Flask itself does not describe your API. flask-smorest adds OpenAPI support, but only for routes written with its Blueprint and marshmallow schemas. Routes written with plain @app.route are not included.

If you have many plain Flask routes and do not want to change them, skip this guide: use From code (GitHub) in MCPEasy, which reads your routes directly, or export a Postman collection.

Step by step

  1. Step 1: Install flask-smorest

    It brings marshmallow, which describes your inputs.

    Terminal
    pip install flask-smorest
  2. Step 2: Set up the Api object

    Where you create your Flask app:

    app.py
    from flask import Flask
    from flask_smorest import Api
    
    app = Flask(__name__)
    app.config["API_TITLE"] = "My API"
    app.config["API_VERSION"] = "v1"
    app.config["OPENAPI_VERSION"] = "3.0.3"
    app.config["OPENAPI_URL_PREFIX"] = "/"
    
    api = Api(app)
  3. Step 3: Write routes as blueprints with schemas

    Each route says what it accepts (@blp.arguments) and returns (@blp.response). Register the blueprint with api.register_blueprint, not app.register_blueprint, or it will not be in the spec.

    bookings.py
    import marshmallow as ma
    from flask_smorest import Blueprint
    
    class BookingSchema(ma.Schema):
        service_id = ma.fields.String(required=True)
        start_time = ma.fields.DateTime(required=True)
    
    blp = Blueprint("bookings", __name__, url_prefix="/bookings", description="Bookings")
    
    @blp.route("/", methods=["POST"])
    @blp.arguments(BookingSchema)
    @blp.response(201, BookingSchema)
    def create_booking(data):
        """Create a booking
    
        Books an appointment in a free slot.
        """
        # data is the validated request body
        return data
    app.py
    from bookings import blp as bookings_blp
    
    api.register_blueprint(bookings_blp)
  4. Step 4: Get the spec

    While the app runs, the spec is at /openapi.json (because OPENAPI_URL_PREFIX is "/"). To write a file, use the flask command that flask-smorest adds:

    Terminal
    flask --app app openapi write openapi.json
    Terminal (YAML)
    flask --app app openapi write --format=yaml openapi.yaml

    Change --app app to your module name if your app is not in app.py.

Check that it worked

  • http://localhost:5000/openapi.json shows your blueprint routes under "paths".

Give it to MCPEasy

Have a link? Paste it (best)

In MCPEasy, create a server, choose OpenAPI URL and paste the link, for example https://api.example.com/openapi.json. Later changes to your API can be picked up with a sync.

Have a file? Upload it

Choose Upload file and drop in the .json or .yaml file (up to 5 MB). MCPEasy turns each endpoint into a tool, and you review them all before anything goes live.

If something goes wrong

/openapi.json returns 404.
Set app.config["OPENAPI_URL_PREFIX"] = "/". Without it, flask-smorest does not serve the spec.
Some routes are missing.
They use @app.route or were registered with app.register_blueprint. Move them to a flask-smorest Blueprint registered with api.register_blueprint, or use From code (GitHub) in MCPEasy.

Still stuck, or no time to set this up? In MCPEasy choose From code (GitHub): it reads your routes straight from the repository, read-only, and tests every tool against your live API before it goes live.

Official documentation

Other frameworks