# Webhooks and notifications

Durable outboxes own delivery and retries across interruptions.

## Outbound webhooks [#outbound-webhooks]

An Endpoint belongs to a User, not an Organization: it receives the signals of every Organization the User is a Member of. Endpoint registration, rotation, update, deletion, the test delivery and the deliveries list are `webhooks.service.ts` programs behind `webhooks.procedures.ts` (mounted as `integrations.webhook`), with their failures in `webhooks.errors.ts`.

Webhook creation and delivery are separate. Domain changes record durable payloads through `webhooks.events.ts`, and `webhooks.delivery.ts` owns the rest: it claims rows with a lease before making an attempt, signs the payload, applies bounded transport and SSRF checks, then settles delivery and endpoint health together under the current lease. The cron round evaluates the time-based triggers for a batch of Endpoints with one `INSERT … SELECT` per trigger over the Endpoints' current Member rows.

Destination checks enforce HTTPS on port 443 with no credentials, no IP literals, and no blocked hostnames, then screen every DNS answer against the IANA IPv4 and IPv6 special-purpose registries (`webhooks.ssrf.ts`). Delivery defaults to `WEBHOOK_TRANSPORT=pinned` (Node HTTPS pinned to the screened IP addresses with TLS SNI and hostname verification), so DNS rebinding cannot redirect the request. Deployed Cloudflare Workers set `WEBHOOK_TRANSPORT=platform` at deploy time to use platform `fetch` over Cloudflare's public-only egress; local `wrangler dev --local` leaves it unset and fails closed rather than using Miniflare's private egress. Redirects are never followed.

A webhook POST receives one attempt per round. Persistent policy decides when to retry. Respect Retry-After without making a request hold the whole cron tick indefinitely. Test a lost response and expired lease; a network timeout does not prove that the receiver rejected the event.

## Push reminders [#push-reminders]

The notification outbox records reminder work. Expo push sending and receipt reads have different retry semantics: receipt GETs can use the idempotent read helper. Enrollment, foreground presentation and tap responses each validate current User/Organization scope.

Mobile suppresses unauthorized or stale presentation and navigates only to a fixed authorized destination. Cancelling a local listener cannot recall a notification already displayed by the operating system.

## Extend [#extend]

Add event contracts to the existing event definitions and enqueue work in the domain transaction. Keep secrets and private payloads out of logs. Test duplicate delivery, lease fencing, disabled configuration and identity transitions. Device and provider delivery need separate verification from the local transport suite.

## Key files [#key-files]

- [`packages/api/src/features/webhooks/webhooks.service.ts`](https://github.com/CarlosZiegler/fofo/blob/main/packages/api/src/features/webhooks/webhooks.service.ts)
- [`packages/api/src/features/webhooks/webhooks.delivery.ts`](https://github.com/CarlosZiegler/fofo/blob/main/packages/api/src/features/webhooks/webhooks.delivery.ts)
- [`packages/api/src/features/webhooks/webhooks.events.ts`](https://github.com/CarlosZiegler/fofo/blob/main/packages/api/src/features/webhooks/webhooks.events.ts)
- [`packages/api/src/features/webhooks/webhooks.attempt.ts`](https://github.com/CarlosZiegler/fofo/blob/main/packages/api/src/features/webhooks/webhooks.attempt.ts)
- [`packages/api/src/features/notifications/notifications.outbox.ts`](https://github.com/CarlosZiegler/fofo/blob/main/packages/api/src/features/notifications/notifications.outbox.ts)
- [`packages/api/src/features/notifications/notifications.jobs.ts`](https://github.com/CarlosZiegler/fofo/blob/main/packages/api/src/features/notifications/notifications.jobs.ts)
- [`apps/mobile/src/features/notifications/notifications.push-presentation.ts`](https://github.com/CarlosZiegler/fofo/blob/main/apps/mobile/src/features/notifications/notifications.push-presentation.ts)
