# Routing

### Defining a Route

To create a route, use the `@app.route()` decorator:

{% code overflow="wrap" %}

```python
from slush.app import Slush

app = Slush()

@app.route("/hello", methods=["GET"])
def hello(request):
    return {"message": "Hello from Slush!"}
```

{% endcode %}

### Multiple HTTP Methods

You can allow multiple HTTP methods for a single route:

{% code overflow="wrap" %}

```python
@app.route("/book/<int:id>", methods=["GET", "POST"])
def get_book(id, request):
    return {"book_id": id}
```

{% endcode %}

### Dynamic Path Parameters

Slush supports dynamic routing using typed parameters:

{% code overflow="wrap" %}

```python
@app.route("/user/<int:id>", methods=["GET"])
def get_user(id, request):
    return {"user_id": id}
```

{% endcode %}

#### Supported types

* `int` → `/user/<int:id>`
* `str` → `/user/<str:name>`

These values are automatically extracted and passed into your function.

### Route Logic

Inside a route, you can access:

* `request.query_params`
* `request.headers`
* `request.cookies`
* `request.json`
* `request.files()`

Example:

{% code overflow="wrap" %}

```python
@app.route("/search", methods=["GET"])
def search(request):
    query = request.query_params.get("q", "")
    return {"query": query}
```

{% endcode %}

### Modular Routing with Routers

You can organize routes using `Router`:

{% code overflow="wrap" %}

```python
from slush.core.routing import Router

user_router = Router()

@user_router.route("/<int:id>", methods=["GET"])
def get_user(id, request):
    return {"user_id": id}
```

{% endcode %}

### Register Router with the Application

Attach the router to your main app using `add_router()`:

{% code title="app.py" overflow="wrap" %}

```python
from slush.app import Slush
from users import user_router

app = Slush()

app.add_router(user_router, prefix="/user/")
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://slush.gitbook.io/docs/documentation/basics/routing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
