
# Workflows and webhooks

A workflow is a **trigger** and the **steps** it leads to. The trigger can be a form being sent, a contact being added, a payment, a page being published, a collection entry changing, a webhook call, a schedule, or a Run button. The API can do everything the Automate workspace in the dashboard does.

Reading needs `workflows:read`; creating, changing, publishing, testing, running and deleting needs `workflows:write`. Publishing, turning a workflow on or off, deleting it and rotating its webhook secret are for owners and admins.

## The graph

```json
{
  "nodes": [
    { "id": "trigger", "type": "trigger.form", "config": { "form": "contact" } },
    { "id": "add", "type": "people.upsert", "config": { "email": "{{trigger.contact.email}}", "tags": "website" } }
  ],
  "edges": [{ "from": "trigger", "port": "next", "to": "add" }]
}
```

- `nodes`: up to 100. `id` is lowercase letters, digits and `_`, starting with a letter. `type` comes from the node catalog, and `config` holds its fields.
- `edges`: up to 300. `port` is one of the source node's ports: `next` for most nodes, `true` or `false` after `logic.if`, `pass` after `logic.filter`.
- Exactly one trigger, nothing leading into it, and no loops.
- Text fields are templates. `{{trigger.contact.email}}` reads the trigger's output, `{{steps.<id>.…}}` an earlier step's, and `{{site.name}}` the site. Conditions are data, never code: nothing is ever evaluated.

### The node catalog

`GET /workflow-nodes` lists every node with its fields (key, kind, whether it is required, options) and outputs, the paths later steps can read. Write graphs against it.

| Kind | Types |
|---|---|
| Triggers | `trigger.form`, `trigger.contact`, `trigger.payment`, `trigger.page`, `trigger.entry`, `trigger.webhook`, `trigger.schedule`, `trigger.manual` |
| Logic | `logic.if`, `logic.filter`, `logic.wait` |
| People | `people.upsert`, `people.stage`, `people.note` |
| Data | `data.create`, `data.update`, `data.find`, `data.http` |
| Messages | `message.email`, `message.whatsapp`, `message.slack` |
| AI | `ai.sort`, `ai.summarise`, `ai.draft` |
| Respond | `respond.json` (webhook workflows only) |

## From draft to running

1. **Create.** `POST /sites/{siteId}/workflows` with `{ name, draft }`, or `{ templateId }` to start from a template. `GET …/workflow-templates` lists them: `new-enquiry`, `new-booking`, `weekly-digest` and `payment-slack`.
2. **Test.** `POST …/workflows/{workflowId}/test` runs the draft once with the `trigger` payload you send, or the trigger's sample. It answers `{ runId }`. Problems that would stop the graph running come back as `400` with `details.problems`.
3. **Publish.** `POST …/workflows/{workflowId}/publish` makes the draft the graph that runs. The first publish turns the workflow on. A webhook workflow with secret access gets its signing secret in `hookSecret`, once.
4. **On and off.** `PATCH …/workflows/{workflowId}` with `{ "enabled": false }` or `true`. Only a published workflow can be on. Changing `draft` doesn't change what runs until you publish again, so editing never breaks a live workflow.
5. **Run it yourself.** `POST …/workflows/{workflowId}/run` with `{ "input": { … } }` runs the published graph now, as its Run button does. A graph without a wait answers `{ runId, status, output }`; one with a wait answers `{ runId, status: "queued" }` and carries on in the background.

## Runs

Every run keeps the frozen graph, the trigger payload and each step's input, output and error.

- `GET /sites/{siteId}/runs` lists recent runs of every workflow. Filter with `workflow` and `status` (`queued`, `running`, `waiting`, `succeeded`, `failed`, `cancelled`).
- `GET …/workflows/{workflowId}/runs` lists one workflow's runs.
- `GET /sites/{siteId}/runs/{runId}` shows one run with each step.

## Webhooks

A published, enabled workflow whose trigger is `trigger.webhook` answers at `/api/hooks/<name>` on the site's own address, for any method. `name` is the trigger's `name` field:

```text
https://harbour-bakery.unvop.dev/api/hooks/new-booking
```

### Who may call it

The trigger's `access` field decides:

| `access` | The caller sends |
|---|---|
| `secret` (the default) | `X-Unvop-Signature: sha256=<hex>`, the HMAC-SHA256 of the raw body with the workflow's signing secret |
| `key` | `Authorization: Bearer <key>`: a site key of this site, or a platform key. Account keys aren't accepted here. |
| `public` | Nothing. Anyone may call it. |

### Sign a request

Sign the exact bytes you send. Serialise the body once and sign that string:

```js tab="JavaScript"
import { createHmac } from 'node:crypto';

const body = JSON.stringify({ name: 'Sam Rivera', date: '2026-12-04' });
const signature = 'sha256=' + createHmac('sha256', process.env.UNVOP_HOOK_SECRET).update(body).digest('hex');

await fetch('https://harbour-bakery.unvop.dev/api/hooks/new-booking', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json', 'X-Unvop-Signature': signature },
  body,
});
```

```python tab="Python"
import hashlib, hmac, json, os, requests

body = json.dumps({"name": "Sam Rivera", "date": "2026-12-04"})
digest = hmac.new(os.environ["UNVOP_HOOK_SECRET"].encode(), body.encode(), hashlib.sha256).hexdigest()

requests.post(
    "https://harbour-bakery.unvop.dev/api/hooks/new-booking",
    data=body,
    headers={"Content-Type": "application/json", "X-Unvop-Signature": f"sha256={digest}"},
)
```

```sh tab="curl"
BODY='{"name":"Sam Rivera","date":"2026-12-04"}'
SIG=$(printf '%s' "$BODY" | openssl dgst -sha256 -hmac "$UNVOP_HOOK_SECRET" | sed 's/^.* //')
curl https://harbour-bakery.unvop.dev/api/hooks/new-booking \
  -H "Content-Type: application/json" \
  -H "X-Unvop-Signature: sha256=$SIG" \
  -d "$BODY"
```

Rotate the secret with `POST …/workflows/{workflowId}/hook-secret`. The old one stops working at once and the new one is shown once.

### What the workflow sees

`trigger.body` (JSON, form-encoded fields, or text), `trigger.query`, `trigger.headers` and `trigger.method`. Headers never include `Authorization`, cookies, the signature or Cloudflare's own headers.

### What the caller gets back

- A graph without a wait runs while the caller waits. With a `respond.json` step, the answer is that step's status and body; without one, `200 { "ok": true }`.
- A graph with a wait, or a run still going after 25 seconds, answers `202 { "runId": "…" }` and carries on.
- A failed run answers `502 { error, code: "failed" }`, or `503 { error, code: "not_configured" }` when a connection it calls has no key yet or its key was refused. Signed and keyed callers also get the `runId`; a public caller never does.

### Limits

Bodies up to 64 KB (`413` above that). 60 calls a minute per endpoint; a public endpoint also takes 10 a minute from one address and 300 an hour in all. Over a limit the answer is `429 { error, code: "rate_limited", retryAfter }` with `Retry-After`.

To put a webhook workflow in front of visitors without writing any code, use the [Action form](https://developers.unvop.com/guides/action-form.md).
