# How a case starts

> The four ways a case starts, inside the app, from another app, on a schedule or on another case's outcome, and what each one becomes.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://cases.docs.sypnia.com/map/starts/

For agents: starts (required), typed: in_app (a person in a place; may have where) | { external: webhook|api, idempotency_key: <input field>, respond: outcome (default for api: wait and answer with the outcome) | accepted (202 + run_id at once; default for webhook), signature: { header, secret } (HMAC of the body) or a case key (Authorization: Bearer syp_rck_…, one case only) } → a public endpoint POST /hooks/<case-id> with the input schema; a resend with the same key within 24 h returns the first answer and runs nothing; in_app → a route of the area's service POST /_cases/<case-id> called with the person's Sypnia Data session, run under RLS (401 without one; role = app_metadata.role, else [cases] default_role of sypnia.toml; 403 for a role not in allowed, or an Origin other than the service's own host or [cases] origins); a case someone listens to always starts there, never from the browser; case keys: secret SYPNIA_CASE_KEY_<CASE_ID>, printed by `npx @sypnia/cases key <case>`; every <n>m|h is an elapsed interval aligned to the zone's standard time, every <n>d runs at local midnight every n days counted from 1970-01-01 | { scheduled: 'every 2h' | '<cron>', timezone: IANA (default UTC) } → a job | { event: <area.case.Outcome.vN> } | { event: { on: [a, b] (any), filter: { field: value | { gte, gt, lte, lt, in, exists } } } } | { event: { on_all: [a, b], key: <payload field>, within: <duration> } } (waits for all with the same key inside the window; otherwise WindowExpired in Telemetry, case not started); order_by: <field> delivers same-key events one at a time in order. in_app yes/no is derived from the kind. A case with no start is an orphan (check error). Only public outcomes of other areas can be listened to; a case never listens to its own outcome.

Every case says how it starts, with `starts`. There are four kinds, and each one becomes something real when the app runs: a place on a screen, an endpoint, a job or a subscription.

`La Espiga, four ways to start`

```yaml
# cases/orders/order-for-tomorrow.case.yaml
starts: in_app
where: shop.order-for-tomorrow

# cases/orders/rappi-order.case.yaml
starts: { external: webhook, idempotency_key: rappi_order_id }

# cases/weather/check-weather.case.yaml
starts: { scheduled: 'every 2h', timezone: America/Bogota }

# cases/deliveries/pause-deliveries.case.yaml
starts: { event: weather.check-weather.RainExpected.v1 }
```

| Kind        | Who starts it                                          | What it becomes                                                |
| ----------- | ------------------------------------------------------ | -------------------------------------------------------------- |
| `in_app`    | a person, in a place of the app                        | a button or form on that place's screen; its path in the docs  |
| `external`  | another app: a webhook it sends, or a call to your API | an HTTP endpoint with its input schema and its idempotency key |
| `scheduled` | the clock                                              | a job, on the time zone you name                               |
| `event`     | another case's outcome                                 | a subscription with its own queue, retries and dead letters    |

Whether a case happens inside the app is never written separately: it is `in_app` or it is not.

## `in_app`

A person starts it from a place of the app. The case may say which place with [`where`](https://cases.docs.sypnia.com/map/navigation); its `actor` is a person, and `allowed` says which roles may run it. The runtime gives it a route of your app, `POST /_cases/<case-id>`, that your screen calls with the person's [Sypnia Data](https://data.docs.sypnia.com/) session: no session gets `401`, a role that is not allowed gets `403`. The case runs under the project's row-level security, as that person.

The person's role is `app_metadata.role` of their Data session; when the session has none, it is the `default_role` of the `[cases]` table of `sypnia.toml`. `allowed` is compared with that role. The route answers only your own pages: a request whose `Origin` is not the service's own host, or one of the `origins` listed in the same table, gets `403`.

`sypnia.toml`

```toml
[cases]
default_role = "customer"
origins = ["https://la-espiga.example"]
```

A case whose outcome some case listens to always starts here, on the server, so its outbox row commits with its data ([Runners](https://cases.docs.sypnia.com/sypnorm/runners#a-case-someone-listens-to-runs-on-the-server)).

## `external`

Another app starts it: Rappi creating an order, a payment provider confirming a payment, a partner's system calling your API.

`cases/orders/rappi-order.case.yaml`

```yaml
id: rappi-order
title: An order arrives from Rappi
actor: rappi
goal: Bake and hand over what Rappi's customer ordered
starts:
  external: webhook
  idempotency_key: rappi_order_id
  signature: { header: X-Rappi-Signature, secret: RAPPI_SECRET }
input:
  rappi_order_id: string
  items: [{ sku: string, qty: int }]
  pickup_at: datetime
outcomes:
  OrderPlaced: { order_id: uuid, ready_at: datetime }
failures:
  SoldOut: { when: a product is sold out, payload: { sku: string } }
```

| Field             | What it does                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `external`        | `webhook` (the other app sends and does not wait for the result) or `api` (the other app calls and waits for it)                                                                                                                                                                                                                                                                                                                                                                       |
| `idempotency_key` | the input field that identifies one request. A resend with the same key within 24 hours runs nothing and gets the first answer back, so a resent order is not baked twice. For `api` starts, the caller may send the key in the `Idempotency-Key` header instead                                                                                                                                                                                                                       |
| `respond`         | `outcome`: answer with the case's outcome (the default for `api`). `accepted`: answer `202 Accepted` with a `run_id` at once and run the case from a queue (the default for `webhook`)                                                                                                                                                                                                                                                                                                 |
| `signature`       | how the caller proves who it is: an HMAC-SHA256 of the body in the `header` you name, with a `secret` you keep in your app's secrets. Without `signature`, the caller sends a **case key** instead (`Authorization: Bearer syp_rck_…`), good for that one case only: `npx @sypnia/cases key <case>` prints a new one, which your app keeps in the secret `SYPNIA_CASE_KEY_<CASE_ID>` (`SYPNIA_CASE_KEY_RAPPI_ORDER`). A request that fails either gets `401` and never starts the case |

The endpoint is `POST /hooks/<case-id>` on your app's address (`POST /hooks/rappi-order`), and its body is the case's `input`. The input is checked before the case runs: a bad body gets `422` with the field that is wrong. With `respond: outcome`, a declared failure answers `422` with its name and payload, and an outcome answers `200` with its payload.

## `scheduled`

The clock starts it.

`cases/weather/check-weather.case.yaml`

```yaml
id: check-weather
title: Check the weather
actor: clock
goal: Know in time when rain is coming
starts: { scheduled: 'every 2h', timezone: America/Bogota }
outcomes:
  RainExpected: { from: datetime }
  ClearSkies: {}
```

`scheduled` takes `every <n>m|h|d` or a cron line (`'0 6 * * 1-6'`, six in the morning, Monday to Saturday). `every <n>m` and `every <n>h` are an elapsed interval aligned to the zone's standard time (`every 2h` runs at 0:00, 2:00, 4:00…); `every <n>d` runs at local midnight every `n` days, counted from 1970-01-01. `timezone` is a name from the time-zone database, `UTC` when left out, and changes of daylight saving time are handled for you. A run still going when the next one is due makes the next one skip, unless the start says `overlap: allow`. Like every schedule, it runs at least once per time, so the case must do no harm if it runs twice.

## `event`

Another case's outcome starts it. The name of an outcome is `<area>.<case>.<Outcome>.v<version>` ([Outcomes and events](https://cases.docs.sypnia.com/map/outcomes-and-events)).

`One outcome`

```yaml
starts: { event: weather.check-weather.RainExpected.v1 }
```

**Any of several**, with `on`:

`cases/kitchen/plan-the-bake.case.yaml`

```yaml
starts:
  event:
    on: [orders.order-for-tomorrow.OrderPlaced.v1, orders.rappi-order.OrderPlaced.v1]
```

**Only some**, with a filter on the payload:

`cases/payments/tell-the-owner.case.yaml`

```yaml
starts:
  event:
    on: [payments.take-payment.PaymentTaken.v1]
    filter: { amount: { gte: 100000 } }
```

**All of several**, with `on_all`: the case starts once every outcome with the same correlation key has arrived, inside a window of time.

`cases/deliveries/send-out.case.yaml`

```yaml
starts:
  event:
    on_all: [payments.take-payment.PaymentTaken.v1, kitchen.bake.Baked.v1]
    key: order_id
    within: 4h
```

| Field      | What it does                                                                                                                                                                                                                                         |
| ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `on`       | starts on any of these outcomes                                                                                                                                                                                                                      |
| `on_all`   | starts when all of them arrived with the same `key` value; the case receives every payload                                                                                                                                                           |
| `key`      | the payload field that joins them (the correlation key), present in every outcome listed                                                                                                                                                             |
| `within`   | how long to wait for the rest. If the window closes first, the case does not start: the wait ends as `WindowExpired`, which Telemetry shows among the case's endings, with what arrived ([Telemetry](https://cases.docs.sypnia.com/links/telemetry)) |
| `filter`   | starts only when the payload matches: a value, or `gt`, `gte`, `lt`, `lte`, `in`, `exists`                                                                                                                                                           |
| `order_by` | a payload field: events with the same value are delivered one at a time, in order. Without it, events may arrive in any order                                                                                                                        |

`filter` is called `filter`, never `where`: `where` is always the case's place in the app.

Two rules keep areas apart and events honest:

- A case can listen to another area's outcome only if that area made it **public**. Outcomes inside one area can be listened to by any case of that area ([Actors, areas and journeys](https://cases.docs.sypnia.com/map/actors-areas-journeys#areas)).
- A case never listens to its own outcome. Cycles between cases are refused unless they say how they stop ([Loops](https://cases.docs.sypnia.com/runtime/loops)).

## A case with no start

A case without `starts` is an **orphan**: nothing can ever run it. The check reports it as an error, so every case on the map has a way in.
