Separate routes in different files in Flask

I want to separate my /api endpoints from some others in another file.

Imagine I have this two files:

app.py

from flask import Flask
from routes.api_routes improt api
    
app = Flask(__name__)
app = app.register_blueprint(api)
    
app.route("/")
def index():
    return "This is Index"

app.route("/about")
def about():
    return "About page"

api_routes.py

from flask import Blueprint

api = Blueprint('api', __name__)

@api.route('/users')
def users():
    return getUsers()

@api.route('/posts')
def posts():
    return getPosts()

Let's say I want to access /api/users and /api/posts but I don't want to write the prefix "/api" before all of the endpoints that are on api_routes.py

How could I automatically add the prefix like you could do on Express.JS?

app.use("/api", apiRoute);

I thought on adding a PREFIX constant at the beggining of the file and appending the rest of the endpoint after it:

PREFIX = "/api"
...
@api.route(PREFIX+'/users')

Solution 1:

What you can do is to set the Prefix when you are registering your blueprint like so:

app = app.register_blueprint(api, url_prefix='api')