> ## Documentation Index
> Fetch the complete documentation index at: https://docs.backant.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Routing

> How Flask Blueprints and URL routing work in a BackAnt project.

BackAnt uses Flask's Blueprint system to organize routes. Each resource gets its own Blueprint registered at a URL prefix.

## How routes are structured

Every route file defines a Blueprint, registers it at a URL prefix, and exposes endpoint functions that call the service layer:

```python theme={null}
from flask import Blueprint, jsonify
from services.users_service import myUsersService

users_bp = Blueprint("users", __name__, url_prefix="/users")

@users_bp.get("")
def get_users():
    response = myUsersService.get_users()
    return jsonify(response)
```

The Blueprint is then registered in `api/app.py`:

```python theme={null}
from routes.users_route import users_bp
app.register_blueprint(users_bp)
```

When you run `ant generate route users`, both the route file and the `app.py` registration are created and wired automatically.

## HTTP methods

Flask Blueprints support all standard HTTP methods as decorators:

```python theme={null}
@users_bp.get("")          # GET /users
@users_bp.post("/create")  # POST /users/create
@users_bp.put("/update")   # PUT /users/update
@users_bp.delete("/remove") # DELETE /users/remove
```

## Subroutes

A subroute is an additional endpoint appended to an existing Blueprint. When you run `ant generate subroute users profile`, the following is added to `users_route.py`:

```python theme={null}
@users_bp.get("/profile")
def users_profile_route():
    response = myUsersService.get_profile()
    return jsonify(response)
```

And a corresponding method is added to `users_service.py` and `users_repository.py`.

## URL structure

Given a route named `users` with subroutes `profile` and `create`:

| Method | URL              | Handler               |
| ------ | ---------------- | --------------------- |
| GET    | `/users`         | `get_users()`         |
| GET    | `/users/profile` | `get_profile()`       |
| POST   | `/users/create`  | `create_create(data)` |

## Generating routes and subroutes

```bash theme={null}
ant generate route users
ant generate subroute users profile
ant generate subroute users create --type POST
```

See the [CLI Reference](/cli/generate-route) for all options.

## Naming rules

Route names must be valid Python identifiers:

```
✅ users, user_posts, products_v2, api_keys
❌ my-route, user posts, 1users, user.posts
```

The route name determines the URL prefix, file names, class names, and singleton names — see [Architecture](/architecture/overview) for the full naming table.

## Accessing request data

Inside a route function, use Flask's `request` object:

```python theme={null}
from flask import request

@users_bp.post("/create")
def create_user():
    data = request.get_json()
    response = myUsersService.create_user(data)
    return jsonify(response)
```

See [Requests](/the-basics/requests) for full details.
