---
url: /runcobo/guide/actions.md
description: >-
  Anatomy of a Runcobo action, the request lifecycle, and what's available
  inside call.
---

# Actions

An **action** handles one endpoint. It's a class that inherits from your application's [`Blog::BaseAction`](/guide/project-structure#blog-baseaction) (which itself inherits from Runcobo's `BaseAction`), declares its routes, and implements `call`.

```crystal [src/actions/posts/update_post.cr]
class Posts::UpdatePost < Blog::BaseAction
  patch "/posts/:id"                       # 1. routes

  before authenticate              # 2. filters

  call do                                  # 3. the handler
    input = validate(UpdatePostContract)   # 4. typed input
    if post = store.update_post_title?(id: input.id, title: input.title)
      render_json({id: post.id, title: post.title})  # 5. a response
    else
      render_json({error: "post_not_found"}, status_code: 404)
    end
  end

  private def authenticate
    # ...
  end
end
```

A new action instance is created for every request, so instance variables are request-scoped and never shared between requests.

## The `call` block

`call do ... end` defines the action's handler. Write it as a block, not as `def call` — the macro wraps your code with filters, validation error handling and cleanup.

The block normally starts by reading its input with [`validate`](/guide/contracts) and ends by producing a response with one of the [render methods](/guide/responses). If it doesn't write anything, the client receives an empty `200 OK`.

::: warning A `call` block can't be empty
A block containing only comments doesn't compile. Put at least one expression in it, even while sketching.
:::

## The request lifecycle

For every matching request, Runcobo runs these steps in order:

If a filter halts the request (see [Filters](/guide/filters)) or validation fails, the remaining steps — including `after` filters — are skipped. Uploaded temporary files are always cleaned up at the end, whatever happened.

## What's available inside an action

| Method | Description |
| --- | --- |
| `validate(Contract)` | Check the request against a contract and return typed input (see [Request Contracts](/guide/contracts)) |
| `store` | Your typed database queries (see [Database Access](/guide/database)) |
| `request` | The `HTTP::Request` |
| `context` | The `HTTP::Server::Context` — the response, and dependencies set by your route handler |
| `request.route_params` | Raw, percent-decoded path parameters as `Hash(String, String)` |
| `raw_body` | The request body as a `String`, if it has been read |
| `upload(name)`, `uploads(name)` | Uploaded files (see [Uploads](/guide/uploads)) |
| `request.remote_ip`, `request.base_url`, ... | Client and host information (see [Proxies](/guide/proxies)) |
| `request.request_id` | The request ID, also sent as `X-Request-Id` |

Prefer `validate` over reading `request.route_params` or `request.query_params` directly: the contract converts and checks the values.

## Organizing actions

### Shared behavior in `Blog::BaseAction`

Put what every action needs into `Blog::BaseAction` — filters, helpers, access to dependencies:

```crystal [src/actions/base_action.cr]
abstract class Blog::BaseAction < ::BaseAction
  before authenticate
  before read_input

  protected def store : Blog::Store::Queries
    context.blog_store
  end

  private def authenticate
    # ...
  end

  private def not_found
    render_json({error: "not_found"}, status_code: 404)
  end
end
```

```crystal [src/actions/posts/show_post.cr]
class Posts::ShowPost < Blog::BaseAction
  get "/posts/:id"

  call do
    input = validate(ShowPostContract)
    if post = store.get_post?(id: input.id)
      render_json({id: post.id, title: post.title})
    else
      not_found
    end
  end
end
```

Filters and [layouts](/guide/views#layouts) are inherited by subclasses. For a group of actions with extra needs — an admin area, say — add another abstract class that inherits from `Blog::BaseAction`. Helpers used by only one business area belong in that area's `support.cr` instead.

### One endpoint per class

Keep each action focused on one endpoint. Shared logic belongs in plain Crystal objects (services, presenters) that actions call — not in deep action inheritance chains.

## Dependencies

Dependencies travel on the request context. The application's `Blog::RouteHandler` puts the store on each request's context, and `Blog::BaseAction#store` reads it back — see [Database Access](/guide/database#_4-give-actions-a-store). Other dependencies, such as an HTTP client or settings, are passed the same way — see [Dependency injection](/guide/server#dependency-injection).
