Flask
web development
routing
Python
API

Get list of all routes defined in the Flask app

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When a Flask application grows beyond a few endpoints, it becomes useful to inspect the full route table. A route listing helps with debugging, documentation, permission reviews, and confirming that blueprints were registered the way you expected.

The Route Table Lives in app.url_map

Flask stores registered URL rules in app.url_map. The easiest programmatic way to list routes is to iterate over those rules and print the path, endpoint name, and allowed methods.

python
1from flask import Flask, jsonify
2
3app = Flask(__name__)
4
5@app.get("/")
6def home():
7    return "ok"
8
9@app.post("/users")
10def create_user():
11    return jsonify(created=True), 201
12
13@app.get("/users/<int:user_id>")
14def get_user(user_id):
15    return jsonify(user_id=user_id)
16
17for rule in app.url_map.iter_rules():
18    methods = ", ".join(sorted(rule.methods - {"HEAD", "OPTIONS"}))
19    print(f"{rule.endpoint:20} {methods:10} {rule.rule}")

Each rule object gives you useful metadata:

  • 'rule.rule is the URL pattern.'
  • 'rule.endpoint is the internal endpoint name, usually the view function name.'
  • 'rule.methods is the set of HTTP methods allowed for that route.'

That is enough for most debugging and internal documentation tasks.

Example Output and What It Means

A route table from the code above might look like this:

text
1static               GET        /static/<path:filename>
2home                 GET        /
3create_user          POST       /users
4get_user             GET        /users/<int:user_id>

Notice the static route. Flask adds it automatically unless static serving is disabled. Developers often forget about it and then wonder why an unexpected route appears in the output.

Using the Flask CLI

If you want a quick route listing without writing any code, Flask also provides a CLI command:

bash
flask --app app routes

This is often the fastest option while developing locally. It prints a table of routes after Flask imports the application. For app factory setups, pass the correct import path so Flask can create the app instance first.

Filtering Routes for Documentation

In a real project, the full route table may include internal endpoints, static files, or methods you do not care about. Filtering the rules is simple:

python
1for rule in app.url_map.iter_rules():
2    if rule.endpoint == "static":
3        continue
4
5    methods = sorted(rule.methods - {"HEAD", "OPTIONS"})
6    if "GET" in methods:
7        print(rule.rule)

This kind of filtering is useful when generating internal docs or checking only API endpoints.

Blueprints Work the Same Way

If your application uses blueprints, their routes still end up in the main app's URL map after registration. Blueprint endpoints are usually prefixed, which makes them easy to identify.

For example, a route inside a blueprint named admin may appear with an endpoint like admin.dashboard. That prefix helps you group routes by module when printing or exporting them.

Common Pitfalls

One common surprise is that HEAD and OPTIONS appear even when you only declared GET. Flask adds some methods automatically, so filter them out if you want a cleaner list.

Another issue is forgetting that the CLI must be able to import your app. If flask routes shows nothing useful, make sure the --app argument points to the correct module or factory.

Be aware that listing routes does not tell you everything about access control. A route can exist while still requiring authentication, role checks, or other middleware-style guards inside the view logic.

Finally, do not confuse endpoint names with URLs. rule.endpoint is the internal identifier used by url_for, while rule.rule is the actual URL pattern.

Summary

  • Flask keeps registered routes in app.url_map.
  • Use app.url_map.iter_rules() to inspect endpoints, methods, and URL patterns programmatically.
  • The flask --app ... routes command is a quick built-in way to print the route table.
  • Expect automatic entries such as the static endpoint and methods like HEAD and OPTIONS.
  • Blueprint routes appear in the same URL map after registration, usually with prefixed endpoint names.

Course illustration
Course illustration

All Rights Reserved.