4

I am trying to prefix all endpoints with /api/, but I am getting 404 in return if not providing directly in the URL or while registering blueprint.

main.py

from init import app
from modules.User import User
app.register_blueprint(User)
if __name__ == "__main__":
 app.run(debug=True)

init.py

from flask import Flask
# initiating application instance
app = Flask(__name__)
# app configurations
app.config["SCRIPT_NAME"] = "/api/"
# -- OR --
app.config["APPLICATION_ROOT"] = "/api/"
...

modules/User.py

from flask import request, Blueprint
User = Blueprint("user_blueprint", __name__)
# if "/api/" is provided directly 
@User.route("/api/", methods=["GET"]) 
def get():
 return "Called get method"
# 404 if "/" is provided
@User.route("/", methods=["GET"]) 
def get():
 return "Called get method"
jonrsharpe
123k31 gold badges278 silver badges489 bronze badges
asked Aug 12, 2025 at 12:33
2
  • 1
    Have you tried setting the url_prefix attribute for the blueprint instead of assigning a custom prefix for each route? This can be done when initializing or registering the blueprint. Commented Aug 12, 2025 at 15:48
  • 1
    Yes @Detlef, I have. But, I was trying to do it through the configurations as I work in PHP and Javascript where such things can be configured via .env and they take effect throughout the application which is quite easy. Commented Aug 13, 2025 at 5:20

1 Answer 1

3

APPLICATION_ROOT and SCRIPT_NAME define the base path where the application is mounted by the WSGI server.

They don’t automatically add /api to your routes during development, so your @User.route("/") ends up at /, not /api/, hence you get the 404.

You can achieve the desired behavior in one of the following ways:

  • use url_prefix on the blueprint

    Use:

    # modules/User.py
    User = Blueprint("user_blueprint", __name__, url_prefix="/api")
    

    Instead of:

    # modules/User.py
    User = Blueprint("user_blueprint", __name__)
    

    Code:

    # main.py
    from init import app
    from modules.User import User
    app.register_blueprint(User)
    if __name__ == "__main__":
     app.run(debug=True)
    
    # init.py
    from flask import Flask
    # initiating application instance
    app = Flask(__name__)
    
    # modules/User.py
    from flask import request, Blueprint
    User = Blueprint("user_blueprint", __name__, url_prefix="/api")
    @User.route("/", methods=["GET"])
    def get():
     return "Called get method"
    @User.route("/data", methods=["GET"])
    def data():
     return "Called data method"
    
    python main.py
    >> curl http://127.0.0.1:5000/api/
    Called get method
    >> curl http://127.0.0.1:5000/api/data
    Called data method
    
  • or use url_prefix when registering it

    Use:

    # main.py
    app.register_blueprint(User, url_prefix="/api")
    

    Instead of:

    # main.py
    app.register_blueprint(User)
    

    Code:

    # main.py
    from init import app
    from modules.User import User
    app.register_blueprint(User, url_prefix="/api")
    if __name__ == "__main__":
     app.run(debug=True)
    
    # init.py
    from flask import Flask
    # initiating application instance
    app = Flask(__name__)
    
    # modules/User.py
    from flask import Blueprint
    User = Blueprint("user_blueprint", __name__)
    @User.route("/", methods=["GET"])
    def get():
     return "Called get method"
    @User.route("/data", methods=["GET"])
    def data():
     return "Called data method"
    
    python main.py
    >> curl http://127.0.0.1:5000/api/
    Called get method
    >> curl http://127.0.0.1:5000/api/data
    Called data method
    
answered Aug 13, 2025 at 2:02
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for explaining. I am going with 2nd approach as all the modules are registered in the main file; therefore, its quite convenient.
You're welcome. Yes, that makes sense.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.