# Schedules and webhooks

> Start a published Hollow workflow on a schedule or from a webhook: cron rules, time zones, the webhook URL and secret, test calls and the live revision rule.

A workflow can start by itself. A schedule runs it at set times, and a webhook runs it when another service sends it a request. Both are set on the workflow's Trigger step and take effect when you publish.

To start a run from your own code with an API key instead, see [Run workflows over the API](https://platform.projecthollow.com/docs/workflow-api.md).

## Only the live revision runs

Each workflow has one live revision, the one you published last. Schedules and the webhook always start that revision.

- Publishing a new revision moves the schedules to it. The schedules of the previous revision are switched off in the same step, so a workflow never runs twice for one time.
- Schedules and the webhook are part of the published revision. Changing the Trigger step in the editor changes nothing until you publish.
- The webhook URL and secret stay the same when you publish.
- A schedule that is switched off in the Trigger step does not run.

## Schedules

A schedule is a cron rule and a time zone. A workflow can have up to 5 schedules, and each one can be switched on or off.

**Schedule rules**

| Part | Rule |
| --- | --- |
| Rule | Five fields: minute, hour, day of the month, month and day of the week. For example, `0 9 * * 1-5`. |
| Time zone | An IANA time zone name, such as `Europe/London` or `America/New_York`. The rule is read in that zone, so it follows daylight saving time. |
| Frequency | At least 5 minutes between runs. A rule that fires more often is refused when you publish. |
| Number | Up to 5 on one workflow. |

**Example rules**

```text
0 9 * * 1-5     Every weekday at 09:00
*/15 * * * *    Every 15 minutes
30 6 1 * *      At 06:30 on the first day of each month
```

A scheduled run carries no input, so a workflow with a required input cannot have a schedule: publishing is refused until the input is optional or the schedule is removed.

A scheduled run counts against your run quota, like any other run, and shows `schedule` as its trigger in the run history.

## Webhooks

Turn on the webhook in the Trigger step and publish. Hollow gives the workflow a URL and a secret.

Webhook starts refuse workflows containing Call automation, Hollow action or connected-account steps, including inside a loop, with `WORKFLOW_ACTION_UNSUPPORTED`, because the invoking authority cannot yet follow those steps safely. Start those workflows manually in Platform.

**The webhook URL**

```http
POST https://api.projecthollow.com/v1/platform/hooks/HOOK_ID
```

`HOOK_ID` is a long random string that belongs to this workflow. The address does not need an API key. The secret is what proves the request is yours.

### The secret

- Send the secret as a Bearer token in the `Authorization` header, or in the `X-Hollow-Hook-Token` header. Never put it in the address: a query string is logged by proxies and browsers, so Hollow does not read it.
- The secret is shown once, when you turn the webhook on or rotate it. Hollow keeps only a one-way hash and the first few characters, so it cannot be shown again. Store it in your secret manager.
- Rotate the secret if it may have been seen. Rotation gives a new secret and shows it once.
- Turning the webhook on, rotating the secret and turning the webhook off each need a sign-in within the last 15 minutes.
- Turn the webhook off to stop accepting requests.

### Send a request

Send a JSON object as the body, up to 16 KB. The body becomes the run's trigger payload, and the steps read its fields as `trigger.payload`. It is treated as data only and cannot change what the workflow does.

**Call a webhook**

**curl**

```bash
curl -X POST 'https://api.projecthollow.com/v1/platform/hooks/HOOK_ID' \
  -H "Authorization: Bearer $HOLLOW_HOOK_SECRET" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-20417" \
  -d '{"orderId": "20417", "total": 129.5}'
```

**curl with the token header**

```bash
curl -X POST 'https://api.projecthollow.com/v1/platform/hooks/HOOK_ID' \
  -H "X-Hollow-Hook-Token: $HOLLOW_HOOK_SECRET" \
  -H "Content-Type: application/json" \
  -d '{"orderId": "20417"}'
```

**JavaScript**

```javascript
const response = await fetch("https://api.projecthollow.com/v1/platform/hooks/HOOK_ID", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.HOLLOW_HOOK_SECRET}`,
    "Content-Type": "application/json",
    "Idempotency-Key": "order-20417",
  },
  body: JSON.stringify({ orderId: "20417", total: 129.5 }),
});
if (response.status !== 202) throw new Error(`Hook refused: ${response.status}`);
const { id } = await response.json();
```

**Python**

```python
import os

import requests

response = requests.post(
    "https://api.projecthollow.com/v1/platform/hooks/HOOK_ID",
    headers={
        "Authorization": f"Bearer {os.environ['HOLLOW_HOOK_SECRET']}",
        "Idempotency-Key": "order-20417",
    },
    json={"orderId": "20417", "total": 129.5},
    timeout=30,
)
response.raise_for_status()
run_id = response.json()["id"]
```

A request that is accepted returns `202` with the run `id` and its `status`. The webhook does not wait for the run. Read the run in the run history, or by its id as described in [Run workflows over the API](https://platform.projecthollow.com/docs/workflow-api.md).

**A 202 reply**

```json
{
  "id": "run_5t9c1d",
  "status": "queued"
}
```

### Retry a request safely

Send an `Idempotency-Key` header with a value that is unique to the event, such as an order number. If the sender retries with the same key, you get the first reply back with the header `Idempotent-Replay: true` and no second run starts.

### Limits and errors

- A webhook run counts against your run quota, and a webhook accepts up to 60 starts a minute.
- A request with a missing or wrong secret is refused with HTTP 401 and the code `INVALID_HOOK_SECRET`, and a body that is not a JSON object of at most 16 KB is refused with HTTP 400. Neither starts anything.
- A workflow whose automation is paused in Cascade refuses webhook and API starts with HTTP 409 and the code `WORKFLOW_PAUSED`. Resume it in Cascade to start runs again.
- Too many starts is HTTP 429 with the code `RATE_LIMITED` and a `Retry-After` header, and a used-up run quota is HTTP 429 with the code `QUOTA_EXCEEDED`. See [Run workflows over the API](https://platform.projecthollow.com/docs/workflow-api.md#errors) for the codes.

## Test a webhook

Use the test address to see what a sender really sends, so you can pick its fields in the editor without running the workflow.

1. In the Trigger step, start listening for a test request. The editor listens for 10 minutes.
2. Send a request to the test address with the same secret and a JSON body.
3. The editor shows the body, and you can choose its fields when you fill in later steps.

**The test address**

```http
POST https://api.projecthollow.com/v1/platform/hooks/HOOK_ID/test
```

- A test request never runs the workflow and costs nothing. It returns `202` with `{ "received": true }`.
- Only the latest test body is kept, for 30 minutes. A new one replaces it.
- A test request is accepted only while the editor is listening. Outside that window it is refused with HTTP 409 and the code `HOOK_NOT_LISTENING`.

**A test reply**

```json
{
  "received": true
}
```

> **Note: Payloads and secrets**
>
> Do not send passwords, card numbers or other secrets in a webhook body. The body is kept with the run so that a run that waits can carry on with it.
