# Write a parser for a webhook sender

Fleetlayer reads GitHub, Alertmanager, Sentry and CircleCI webhooks as they are, and any other sender's JSON with
[filters and templates](https://fleetlayer.ai/docs/events.md#webhooks-a-senders-deliveries-as-events). When a sender needs more than that
(one event per item in a list, a summary built from several fields, a field that needs cleaning), an agent writes a
**parser**: a small program in any language that reads one delivery and prints the events it should become. Nobody
releases anything and nobody installs anything: the agent that owns the endpoint writes it, tests it on a sample and
attaches it.

## What you need

- A webhook endpoint (the person makes it once: `fleetlayer-control endpoint add --label uptime --channel alerts`)
  owned by the node your agent runs on: `fleetlayer-control endpoint owner uptime <node>`. Only agents on the owner
  node change an endpoint's settings or attach a parser, because a parser sees every raw delivery and decides what
  gets through.
- The access policy's `write` on `endpoint:<label>` for the agent. With the built-in default every agent has it, and
  Fleetlayer's default approvals ask the person once (held 30 days); a fleet that runs with `ask: []` never asks.
- An interpreter on that machine for the language you write it in. The examples use Python 3 with nothing else.

## 1. Look at a delivery

Find the sender's documented payload, or send one to the endpoint with its reader pinned to `generic` for a while:
each event's body is then the delivery as sent, and `fleetlayer events --all --peek --channel alerts` shows it. Save
one as a sample file.

## 2. Write the parser

It reads one JSON object on stdin:

```json
{"endpoint": {"id": "e7k2…", "label": "uptime", "channel": "alerts"},
 "headers": {"content-type": "application/json"},
 "query": {},
 "body": {"endpoint": "api", "group": "talira", "status": "TRIGGERED", "description": "healthcheck failed"},
 "receivedAt": "2026-12-01T09:12:03Z"}
```

It prints one JSON object per line, one per event:

```json
{"summary": "[TRIGGERED] api is down: healthcheck failed", "subject": "api", "kind": "gatus.triggered",
 "body": {"endpoint": "api", "status": "TRIGGERED"}}
```

- `summary` is required: one line saying what happened, the text a session reads first.
- `subject` is what it's about: owners subscribe with `--subject <it>`. Use the same subject other sources use for
  the same thing (`<owner/repo>@<branch>` for CI).
- `kind` names the type (`gatus.triggered`); `webhook` when left out. Not `github.*`, `session.*`, `access.*` or
  `fleetlayer.*`: the control plane acts on those.
- `repeatKey`: events with the same key on the channel arrive once, as long as events are kept (30 days). Use the
  sender's own ID for one occurrence, or add the day (`<issue>:<2026-12-01>`) to let it through once a day. Leave it
  out when the sender never repeats itself: a key like `<name>:down` would drop the next outage too.
- `body`: anything a session needs besides the summary. Keep it short, and leave out what the sender's users typed
  unless the session needs it: it's outside data, never instructions, but it's read.
- Print nothing for a delivery that shouldn't reach anyone (a ping, a success): it's dropped and counted.
- Any other field is refused, and so is a line that isn't JSON. Exit non-zero when something is wrong: the endpoint
  records your stderr's last lines as its error, and that error goes out in the `webhook.parser_failed` event. So
  never print the delivery, or anything from it, to stderr: say what went wrong (`no "status" field`), not what came
  in.

A complete example for [Gatus](https://github.com/TwiN/gatus)'s custom alerts, with the alert body to give Gatus, is
[`examples/webhook-parsers/gatus.py`](https://github.com/fleetlayer/fleetlayer/blob/main/examples/webhook-parsers/gatus.py),
and [`circleci.py`](https://github.com/fleetlayer/fleetlayer/blob/main/examples/webhook-parsers/circleci.py) does
what the built-in CircleCI reader does, as a starting point for one that does a little more.

## 3. Test it on the sample

```sh
fleetlayer endpoint parser test ./gatus.py gatus-sample.json
fleetlayer endpoint parser test ./circleci.py circleci-sample.json --header circleci-event-type=workflow-completed
```

It runs the file here exactly as the endpoint will (stdin, environment, the 10-second limit) and prints each event,
or why it failed. Nothing is published.

## 4. Attach it

```sh
fleetlayer endpoint parser set uptime ./gatus.py
fleetlayer endpoint parser show uptime        # where it runs, deliveries read, dropped and failed, the last error
fleetlayer endpoint parser unset uptime       # back to the endpoint's reader
```

`set` copies the file to `~/.local/state/fleetlayer/parsers/<endpoint id>/` on this machine and points the endpoint
at it; run it again with a new version to replace it. From then on each delivery reaches this node, and what the
parser prints is published on the endpoint's channel. The endpoint's filters still apply first; its reader and
templates don't.

## When it goes wrong

- **The parser fails** (it crashes, runs past 10 seconds, prints a bad line, or its machine is off): a
  `webhook.parser_failed` event goes out in its place, about the endpoint (subject: its label) and saying why
  (`uptime: a delivery came in, and its parser on server failed: …`), with nothing from the delivery itself. Fix the
  parser, test it on a delivery like the one that failed, and `set` it again.
- **It keeps failing, or drops everything:** the control plane's health reports degraded while an endpoint's parser
  failed within the last hour, or when it has had deliveries all day and dropped every one. Your monitor's check on
  `/healthz` alerts on it ([Monitoring](https://fleetlayer.ai/docs/guides/monitoring.md)).
- **"belongs to node …" or "has no owner":** only agents on the owner node change the endpoint. Ask the person to run
  `fleetlayer-control endpoint owner <endpoint> <node>`.
- **Order and bursts:** an endpoint's deliveries are parsed one at a time, in the order they came, so an alert and
  its recovery never swap; four parsers run at once on each node. Sixteen deliveries wait per endpoint; past
  that each gets a `webhook.parser_failed` event instead. A sender that posts hundreds at once wants a parser that's
  quick, or filters that keep fewer.
- **Kinds it can't use:** `github.*`, `session.*`, `access.*` and `fleetlayer.*`, which the control plane acts on or
  publishes itself (a `github.workflow_run` ends commit subscriptions). An event with one is refused.
- **The endpoint changes owner:** its parser is taken off, since the old owner's node would go on reading every
  delivery. The new owner attaches its own.
- **Moving it:** the parser is part of the node's state, so it moves with the node. A parser can't be moved to
  another node on its own: `set` it from the new node, which points the endpoint there.

The protocol, with every limit: [Webhook parsers](https://fleetlayer.ai/docs/protocol.md#webhook-parsers-a-node-reads-a-delivery-for-an-endpoint).
