Skip to content

Rule.io SDK


Rule.io SDK / client/src / parseWebhookEvent

Function: parseWebhookEvent()

parseWebhookEvent(body): WebhookEvent

Defined in: client/src/webhooks/parse.ts:161

Parse a Rule.io webhook body into a typed WebhookEvent.

Accepts either a JSON string or a parsed object. The discriminator on the returned event is a synthetic type literal the SDK derives from which top-level keys are present in the body — Rule.io's payloads have no event_type field of their own.

Tag webhooks (add and remove) are returned as 'subscriber.tag-changed' because the apidoc body shapes are identical. Use markTagDirection to commit the direction once you know which URL received the request.

Parameters

body

string | object

Returns

WebhookEvent

Throws

On invalid JSON, on payload shapes the parser does not recognise, and on the documented preference payloads (currently unsupported).

Example

typescript
import { parseWebhookEvent } from '@rule/client';

app.post('/webhooks/rule', (req, res) => {
  const event = parseWebhookEvent(req.body);

  switch (event.type) {
    case 'subscriber.opted-in':
      // event.subscriber is typed
      break;
    case 'subscriber.bounced':
      // event.bounce is typed
      break;
  }

  res.status(200).end();
});