Flask
Python
Web Development
API
Endpoints

What is an 'endpoint' in Flask?

Master System Design with Codemia

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

Introduction

In Flask, an endpoint is the internal name Flask uses for a route target. It is not the URL itself, although the two are often discussed together. Understanding the difference matters because URL matching is done by path, while URL generation with url_for is done by endpoint name.

Route, View Function, and Endpoint

A Flask route has three related pieces:

  • the URL rule, such as /users/<id>
  • the view function that handles the request
  • the endpoint name that identifies that view inside Flask

Simple example:

python
1from flask import Flask
2
3app = Flask(__name__)
4
5@app.route("/hello")
6def hello():
7    return "Hello"

Here:

  • URL rule is /hello
  • view function is hello
  • endpoint is also hello

By default, Flask uses the function name as the endpoint name.

Why Endpoints Matter

Endpoints are most visible when you call url_for.

python
1from flask import Flask, url_for
2
3app = Flask(__name__)
4
5@app.route("/hello")
6def hello():
7    return "Hello"
8
9with app.test_request_context():
10    print(url_for("hello"))

url_for("hello") does not look up the string "/hello" directly. It looks up the endpoint named hello, then generates the corresponding URL.

That indirection is important because it decouples your application code from hardcoded URL strings.

Dynamic Routes Still Use One Endpoint Name

A dynamic URL can still map to a single endpoint.

python
1from flask import Flask, url_for
2
3app = Flask(__name__)
4
5@app.route("/users/<int:user_id>")
6def user_detail(user_id):
7    return f"User {user_id}"
8
9with app.test_request_context():
10    print(url_for("user_detail", user_id=42))

The endpoint is user_detail, and url_for fills in the variable part of the route using the keyword arguments.

Custom Endpoint Names

You can override the default endpoint name when registering a route.

python
1from flask import Flask
2
3app = Flask(__name__)
4
5@app.route("/status", endpoint="healthcheck")
6def status():
7    return "ok"

Now the route is reached at /status, but the endpoint name is healthcheck, not status.

That means:

python
1from flask import url_for
2
3with app.test_request_context():
4    print(url_for("healthcheck"))

This is useful when you want the internal route name to stay stable even if the function name changes.

Blueprints Prefix Endpoints

Blueprints help organize larger Flask apps, and they change endpoint naming by prefixing the blueprint name.

python
1from flask import Blueprint, Flask, url_for
2
3app = Flask(__name__)
4admin = Blueprint("admin", __name__)
5
6@admin.route("/dashboard")
7def dashboard():
8    return "Admin dashboard"
9
10app.register_blueprint(admin, url_prefix="/admin")
11
12with app.test_request_context():
13    print(url_for("admin.dashboard"))

The endpoint becomes admin.dashboard, not just dashboard. This avoids collisions between similarly named views in different parts of the app.

Inspect Registered Endpoints

Flask keeps an internal mapping of endpoint names to view functions.

python
1from flask import Flask
2
3app = Flask(__name__)
4
5@app.route("/hello")
6def hello():
7    return "Hello"
8
9print(app.view_functions)

This can be useful when debugging duplicate endpoint registration or understanding how blueprints expanded route names.

Common Source of Confusion

People often use "endpoint" to mean the public API path, especially in REST conversations. In Flask-specific terms, though, the endpoint is the internal routing name.

So these two uses are related but not identical:

  • "The API endpoint is /users/42" is common API language.
  • "The Flask endpoint is user_detail" is framework-specific routing language.

Both are understandable in context, but only the second matches Flask internals exactly.

Why url_for Is Better Than Hardcoding Paths

If you hardcode "/hello" everywhere, changing the route later means manual edits across templates and view code.

Using endpoints keeps URL generation centralized:

python
1from flask import Flask, redirect, url_for
2
3app = Flask(__name__)
4
5@app.route("/login")
6def login():
7    return "Login"
8
9@app.route("/go-login")
10def go_login():
11    return redirect(url_for("login"))

If the URL rule changes later, code using url_for("login") can keep working.

Common Pitfalls

One common mistake is assuming the endpoint name is always the same as the URL path. In Flask, it is usually the function name unless overridden.

Another mistake is forgetting blueprint prefixes and calling url_for("dashboard") when the real endpoint is admin.dashboard.

Developers also create duplicate endpoint names accidentally when reusing function names across modules without blueprints or explicit endpoint control.

Finally, hardcoding URLs instead of using url_for makes refactoring routes much harder than it needs to be.

Summary

  • In Flask, an endpoint is the internal name associated with a route target.
  • By default, the endpoint name is the view function name.
  • 'url_for generates URLs by endpoint name, not by raw path string.'
  • Blueprints prefix endpoint names to avoid collisions.
  • Understanding endpoints makes routing, refactoring, and URL generation much clearer.

Course illustration
Course illustration

All Rights Reserved.