---
url: /runcobo/guide/routing.md
description: >-
  Declare routes on actions, capture typed path parameters, and inspect the
  route table.
---

# Routing

Routes are declared inside the action that handles them. There is no central routes file.

```crystal
class Posts::ListPosts < Blog::BaseAction
  get "/posts"

  call do
    render_json(store.list_posts.map { |post| {id: post.id, title: post.title} })
  end
end
```

## HTTP methods

Use the macro named after the method:

| Macro | Method |
| --- | --- |
| `get "/path"` | `GET` |
| `post "/path"` | `POST` |
| `put "/path"` | `PUT` |
| `patch "/path"` | `PATCH` |
| `delete "/path"` | `DELETE` |
| `options "/path"` | `OPTIONS` |
| `head "/path"` | `HEAD` |

For anything else — `LINK`, `PURGE`, WebDAV methods — use `route`:

```crystal
class Cache::Purge < Blog::BaseAction
  route "PURGE", "/cache/:key"

  call do
    render_empty status_code: 204
  end
end
```

Every path must start with `/`.

::: warning HEAD is not automatic
A `get` route does not answer `HEAD` requests. If you need `HEAD`, declare it explicitly with `head "/path"` and decide what to send — see [Empty responses](/guide/responses#empty-responses).
:::

## Several routes, one action

An action can register any number of routes. This is handy for aliases or for serving the same handler on two methods:

```crystal
class Search < Blog::BaseAction
  get "/search"
  post "/search"   # for long queries sent as a form body

  call do
    # ...
  end
end
```

## Path parameters

Name a segment with `:name`, and list it in the action's contract to receive it typed:

```crystal
class Users::ShowUserContract < RequestValidation::Contract
  schema do
    required :org, String
    required :id, Int64, gt: 0
  end
end

class Users::ShowUser < Blog::BaseAction
  get "/orgs/:org/users/:id"

  call do
    input = validate(ShowUserContract)
    render_json({org: input.org, id: input.id})   # input.id is an Int64
  end
end
```

`GET /orgs/acme/users/abc` is rejected with a `422` and an `invalid_type` error on `["id"]` — the rest of `call` never runs.

### Decoding

Routes are matched against the **raw** request path, and each parameter value is percent-decoded afterwards:

| Request path | `:id` value |
| --- | --- |
| `/users/42` | `42` |
| `/users/%E5%BC%A0%E4%B8%89` | `张三` |
| `/users/a%2Fb` | `a/b` — an encoded slash stays inside the parameter |
| `/users/a+b` | `a+b` — `+` is not a space in paths |

Don't decode parameters again in your code.

### Untyped access

Path parameters are also available as strings in `request.route_params` (a `Hash(String, String)`). Prefer listing them in the contract — it documents the parameter and validates it.

## Matching rules

* A request matches on **method and path**. The query string is ignored for matching.
* A trailing slash is tolerated: `/posts/` matches a route declared as `/posts`.
* Unmatched requests fall through to the next handler — by default a plain `404` (see [Error Handling](/guide/errors#not-found)).

## Built-in route

Runcobo registers `GET /healthcheck`, which returns the current UTC time as plain text. Use it for load balancer and container health checks.

## Listing routes

The [`cobo routes`](/cli/#cobo-routes) command compiles your application entry and prints every registered route:

```sh
bin/cobo routes
```

```text
Routes
╭────────┬──────────────┬──────────────────────╮
│ METHOD │ PATH         │ ACTION               │
├────────┼──────────────┼──────────────────────┤
│ GET    │ /healthcheck │ Runcobo::Healthcheck │
│ POST   │ /posts       │ Posts::CreatePost    │
│ GET    │ /posts/:id   │ Posts::ShowPost      │
╰────────┴──────────────┴──────────────────────╯
3 routes
```
