Outgoing webhooks
webhooksOutgoing webhooks: endpoints subscribed to event types, a delivery log unique per endpoint and event with leases and retries, direct dispatch, secrets in Vault with overlap while rotating, auto-disable after an endpoint keeps failing and redelivery.
Developer platformmanagedcustom
PLATFORM_JOBS
send-invoicedone
sync-crmrunning
webhook.retryqueued
sk_live_••••4f2a
Install
The command also installs updated-at, housekeeping, the modules it needs.
npx better-supabase add webhooksConfigure
The blocks entries in your config, with the default mode.
import { defineConfig } from "better-supabase/config";
export default defineConfig({
blocks: {
// Required by webhooks; add installs them too.
"updated-at": {},
housekeeping: {},
// Modes: "managed", "custom". The default is "managed".
webhooks: { mode: "managed" },
},
});Migrations
The files
npx better-supabase add webhooks writes into your declarative schema with the default layout.7 files
supabase/schemas/900_better_supabase_025_webhooks.sqlschema748 lines-- better-supabase module: webhooks
-- @bsb-block webhooks@8 managed
-- @bsb-hash sha256:6b34732266a65738e00e3d14e680ef93916474c9429848ffd58305040cb366d0
-- Outgoing webhooks: endpoints subscribed to event types, a delivery log unique per endpoint and event with leases and retries, direct dispatch, secrets in Vault with overlap while rotating, auto-disable after an endpoint keeps failing and redelivery.
-- Managed by `better-supabase add`; re-running it rewrites this file unless it was edited by hand.
-- Change it through `blocks` in better-supabase.config.ts and the module's SQL hooks.
create table "better_supabase"."webhook_endpoints" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"name" text not null constraint bsb_webhook_endpoints_name_check check (length(trim("name")) > 0),
"url" text not null constraint bsb_webhook_endpoints_url_check check ("url" ~* '^https://'),
"enabled" boolean not null default true,
"event_types" text[] not null default '{}',
"permissions" text[],
"batch" boolean not null default false,
"signing" text not null default 'v1' constraint bsb_webhook_endpoints_signing_check check ("signing" in ('v1', 'v1a')),
"format" text not null default 'cloudevents' constraint bsb_webhook_endpoints_format_check check ("format" ~ '^[a-z][a-z0-9-]{0,39}$'),
"credential_ref" jsonb constraint bsb_webhook_endpoints_credential_ref_check check ("credential_ref" is null or (jsonb_typeof("credential_ref") = 'object' and "credential_ref" ? 'provider')),
"first_failed_at" timestamptz,
"disabled_at" timestamptz,
"disabled_reason" text,
"created_by" uuid references auth.users (id) on delete set null default auth.uid(),
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now()
);
alter table "better_supabase"."webhook_endpoints" alter column "format" set default 'cloudevents';
create index webhook_endpoints_types_idx on "better_supabase"."webhook_endpoints" using gin ("event_types");
create index webhook_endpoints_tenant_idx on "better_supabase"."webhook_endpoints" ("tenant_id");
create index webhook_endpoints_created_by_idx on "better_supabase"."webhook_endpoints" ("created_by");
create trigger bsb_updated_at before update on "better_supabase"."webhook_endpoints"
for each row
execute function better_supabase.set_updated_at('updated_at');
alter table "better_supabase"."webhook_endpoints" enable row level security;
revoke all on "better_supabase"."webhook_endpoints" from public, anon, authenticated;
grant all on "better_supabase"."webhook_endpoints" to service_role;
create table "better_supabase"."webhook_endpoint_secrets" (
"id" uuid primary key default gen_random_uuid(),
"endpoint_id" uuid not null references "better_supabase"."webhook_endpoints" ("id") on delete cascade,
"tenant_id" uuid,
"vault_secret_id" uuid not null,
"expires_at" timestamptz,
"created_at" timestamptz not null default now()
);
create index webhook_endpoint_secrets_endpoint_idx on "better_supabase"."webhook_endpoint_secrets" ("endpoint_id");
alter table "better_supabase"."webhook_endpoint_secrets" enable row level security;
revoke all on "better_supabase"."webhook_endpoint_secrets" from public, anon, authenticated;
grant all on "better_supabase"."webhook_endpoint_secrets" to service_role;
create table "better_supabase"."webhook_deliveries" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid,
"endpoint_id" uuid not null references "better_supabase"."webhook_endpoints" ("id") on delete cascade,
"event_id" text,
"run_id" text,
"event_type" text not null,
"subject" text,
"payload" jsonb not null default '{}',
"status" text not null default 'pending' constraint bsb_webhook_deliveries_status_check check ("status" in ('pending', 'delivering', 'succeeded', 'retrying', 'dead', 'canceled')),
"attempt" integer not null default 0 constraint bsb_webhook_deliveries_attempt_check check ("attempt" >= 0),
"available_at" timestamptz not null default now(),
"lease_expires_at" timestamptz,
"response_status" integer,
"response_body" text,
"duration_ms" integer,
"last_error" text,
"processed_at" timestamptz,
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now()
);
create index webhook_deliveries_due_idx on "better_supabase"."webhook_deliveries" ("available_at", "lease_expires_at") where "status" in ('pending', 'retrying', 'delivering');
create index webhook_deliveries_endpoint_idx on "better_supabase"."webhook_deliveries" ("endpoint_id", "created_at" desc);
create index webhook_deliveries_open_idx on "better_supabase"."webhook_deliveries" ("endpoint_id") where "status" in ('pending', 'retrying');
create index webhook_deliveries_finished_idx on "better_supabase"."webhook_deliveries" ("created_at") where "status" in ('succeeded', 'canceled', 'dead');
create index webhook_deliveries_tenant_created_idx on "better_supabase"."webhook_deliveries" ("tenant_id", "created_at" desc, "id" desc);
create unique index webhook_deliveries_endpoint_event_idx on "better_supabase"."webhook_deliveries" ("endpoint_id", "event_id") where "event_id" is not null;
alter table "better_supabase"."webhook_deliveries" enable row level security;
revoke all on "better_supabase"."webhook_deliveries" from public, anon, authenticated;
grant all on "better_supabase"."webhook_deliveries" to service_role;
create or replace function "better_supabase"."reset_webhook_endpoint"()
returns trigger
language plpgsql
set search_path = ''
as $$
begin
if new."enabled" and not old."enabled" then
new."first_failed_at" := null;
new."disabled_at" := null;
new."disabled_reason" := null;
end if;
return new;
end;
$$;
revoke execute on function "better_supabase"."reset_webhook_endpoint"() from public, anon, authenticated;
create trigger "bsb_webhook_endpoint_reenable" before update of "enabled" on "better_supabase"."webhook_endpoints"
for each row execute function "better_supabase"."reset_webhook_endpoint"();
create or replace function "better_supabase"."drop_webhook_vault_secret"()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
begin
delete from vault.secrets vs where vs.id = old."vault_secret_id";
return null;
end;
$$;
revoke execute on function "better_supabase"."drop_webhook_vault_secret"() from public, anon, authenticated;
create trigger "bsb_webhook_secret_vault" after delete on "better_supabase"."webhook_endpoint_secrets"
for each row execute function "better_supabase"."drop_webhook_vault_secret"();
-- Queues the event for every enabled endpoint subscribed to its type:
-- the exact type, '*', or a prefix pattern such as 'invoice.*'. The
-- patterns that can match are built from the type, so && uses the GIN
-- index on the subscriptions. The same event id never queues twice for one
-- endpoint. An endpoint only gets events its owner may read: a user's
-- endpoint needs read_permission (the module's view permission when unset)
-- in the tenant; one without a user owner is a service principal's: the
-- tenant's default principal needs read_permission through its role when
-- the service-accounts module is installed and the tenant has one, else
-- the endpoint needs read_permission among its declared permissions.
create or replace function "better_supabase"."publish_webhook_event"(event_type text, payload jsonb, tenant uuid default null, event_id text default null, subject text default null, read_permission text default null)
returns integer
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
inserted integer;
segments text[] := string_to_array(publish_webhook_event.event_type, '.');
patterns text[];
v_permission text := coalesce(nullif(publish_webhook_event.read_permission, ''), 'webhooks.read');
v_held text[];
begin
if coalesce(publish_webhook_event.event_type, '') = '' then
raise exception 'An event type is required' using errcode = '22023', hint = 'WEBHOOK_TYPE_REQUIRED';
end if;
patterns := array[publish_webhook_event.event_type, '*'] || array(
select array_to_string(segments[1:i], '.') || '.*'
from generate_series(1, cardinality(segments) - 1) i
);
v_held := array[v_permission, '*'] || array(
select array_to_string(p[1:i], '.') || '.*'
from (select string_to_array(v_permission, '.') as p) parts,
generate_series(1, cardinality(parts.p) - 1) i
);
insert into "better_supabase"."webhook_deliveries" ("endpoint_id", "event_type", "payload", "tenant_id", "event_id", "subject")
select d."id", publish_webhook_event.event_type, coalesce(publish_webhook_event.payload, '{}'), d."tenant_id", publish_webhook_event.event_id::text, publish_webhook_event.subject
from "better_supabase"."webhook_endpoints" d
where d."enabled"
and d."tenant_id" is not distinct from publish_webhook_event.tenant
and d."event_types" && patterns
and (case when d."created_by" is not null then false else (publish_webhook_event.read_permission is null or coalesce(d."permissions" && v_held, false)) end)
on conflict ("endpoint_id", "event_id") where "event_id" is not null do nothing;
get diagnostics inserted = row_count;
return inserted;
end;
$$;
revoke execute on function "better_supabase"."publish_webhook_event"(text, jsonb, uuid, text, text, text) from public, anon, authenticated;
grant execute on function "better_supabase"."publish_webhook_event"(text, jsonb, uuid, text, text, text) to service_role;
-- publish_webhook_event for a batch of events in one call (the outbox
-- sink): each element takes its argument names as keys, keeps its checks
-- and returns {"count"} or {"error"} in order.
create or replace function "better_supabase"."publish_webhook_events"(events jsonb)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
batch_event jsonb;
results jsonb := '[]';
batch_result integer;
failed_state text;
failed_message text;
failed_hint text;
begin
if pg_catalog.jsonb_typeof(events) is distinct from 'array' then
raise exception '"publish_webhook_events" takes a jsonb array of events'
using errcode = '22023', hint = 'WEBHOOK_BATCH_NOT_ARRAY';
end if;
if pg_catalog.jsonb_array_length(events) > 1000 then
raise exception '"publish_webhook_events" got % events, more than 1000', pg_catalog.jsonb_array_length(events)
using errcode = '22023', hint = 'WEBHOOK_BATCH_TOO_LARGE';
end if;
for batch_event in select e from pg_catalog.jsonb_array_elements(events) e loop
begin
batch_result := "better_supabase"."publish_webhook_event"(
event_type => batch_event ->> 'event_type',
payload => coalesce(nullif(batch_event -> 'payload', 'null'::jsonb), '{}'),
tenant => (batch_event ->> 'tenant')::uuid,
event_id => batch_event ->> 'event_id',
subject => batch_event ->> 'subject',
read_permission => batch_event ->> 'read_permission'
);
results := results || pg_catalog.jsonb_build_array(pg_catalog.jsonb_build_object('count', batch_result));
exception when others then
get stacked diagnostics
failed_state = returned_sqlstate,
failed_message = message_text,
failed_hint = pg_exception_hint;
results := results || pg_catalog.jsonb_build_array(pg_catalog.jsonb_build_object(
'error', pg_catalog.jsonb_build_object('code', failed_state, 'message', failed_message, 'hint', nullif(failed_hint, ''))
));
end;
end loop;
return results;
end;
$$;
revoke execute on function "better_supabase"."publish_webhook_events"(jsonb) from public, anon, authenticated;
grant execute on function "better_supabase"."publish_webhook_events"(jsonb) to service_role;
-- Sends one event to one endpoint, outside its subscriptions, e.g. from a
-- workflow step. With an event id, dispatching it again returns the first delivery.
create or replace function "better_supabase"."dispatch_webhook"(endpoint uuid, event_type text, payload jsonb, run_id text default null, event_id text default null)
returns uuid
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_tenant uuid;
v_enabled boolean;
v_id uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if coalesce(dispatch_webhook.event_type, '') = '' then
raise exception 'An event type is required' using errcode = '22023', hint = 'WEBHOOK_TYPE_REQUIRED';
end if;
select d."tenant_id", d."enabled" into v_tenant, v_enabled
from "better_supabase"."webhook_endpoints" d where d."id" = dispatch_webhook.endpoint;
if not found then
raise exception 'Webhook endpoint not found' using errcode = 'P0002', hint = 'WEBHOOK_ENDPOINT_NOT_FOUND';
end if;
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) and not false then
raise exception 'Not allowed to manage this webhook endpoint' using errcode = '42501', hint = 'WEBHOOK_FORBIDDEN';
end if;
if not v_enabled then
raise exception 'Webhook endpoint is disabled' using errcode = '55000', hint = 'WEBHOOK_ENDPOINT_DISABLED';
end if;
if dispatch_webhook.event_id is not null then
select v."id" into v_id from "better_supabase"."webhook_deliveries" v
where v."endpoint_id" = dispatch_webhook.endpoint and v."event_id" = dispatch_webhook.event_id::text;
if v_id is not null then
return v_id;
end if;
end if;
insert into "better_supabase"."webhook_deliveries" ("endpoint_id", "event_type", "payload", "tenant_id", "run_id", "event_id")
values (dispatch_webhook.endpoint, dispatch_webhook.event_type, coalesce(dispatch_webhook.payload, '{}'), v_tenant, dispatch_webhook.run_id::text, dispatch_webhook.event_id::text)
on conflict ("endpoint_id", "event_id") where "event_id" is not null do nothing
returning "id" into v_id;
if v_id is null then
-- Another transaction dispatched the same event first.
select v."id" into v_id from "better_supabase"."webhook_deliveries" v
where v."endpoint_id" = dispatch_webhook.endpoint and v."event_id" = dispatch_webhook.event_id::text;
end if;
return v_id;
end;
$$;
revoke execute on function "better_supabase"."dispatch_webhook"(uuid, text, jsonb, text, text) from public, anon, authenticated;
grant execute on function "better_supabase"."dispatch_webhook"(uuid, text, jsonb, text, text) to authenticated, service_role;
create or replace function "better_supabase"."webhook_deliveries_has_work"()
returns boolean
language sql
stable
set search_path = ''
as $$
select exists (
select 1 from "better_supabase"."webhook_deliveries" v
where v."status" in ('pending', 'retrying', 'delivering')
and v."available_at" <= now()
and (v."lease_expires_at" is null or v."lease_expires_at" <= now())
)
$$;
revoke execute on function "better_supabase"."webhook_deliveries_has_work"() from public, anon, authenticated;
grant execute on function "better_supabase"."webhook_deliveries_has_work"() to service_role;
-- Leases due deliveries to one worker and counts the attempt, so a worker
-- that dies mid-send still uses one up. The attempt is the lease token that
-- complete_webhook_delivery checks. Deliveries of disabled endpoints are
-- canceled instead, a lease that ran out is claimed again, and one that ran
-- out on the last attempt is dead-lettered.
create or replace function "better_supabase"."claim_webhook_deliveries"(max_items integer default 25, lease interval default '2 minutes', max_attempts integer default 8)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
result jsonb;
begin
update "better_supabase"."webhook_deliveries" v
set "status" = 'canceled', "lease_expires_at" = null, "last_error" = 'Endpoint is disabled', "processed_at" = now(), "updated_at" = now()
from "better_supabase"."webhook_endpoints" d
where d."id" = v."endpoint_id"
and not d."enabled"
and v."status" in ('pending', 'retrying');
update "better_supabase"."webhook_deliveries" v
set "status" = 'dead', "lease_expires_at" = null, "last_error" = 'The lease ran out on the last attempt', "processed_at" = now(), "updated_at" = now()
where v."status" = 'delivering'
and v."lease_expires_at" <= now()
and v."attempt" >= greatest(1, coalesce(claim_webhook_deliveries.max_attempts, 8));
with due as (
select v."id" as id
from "better_supabase"."webhook_deliveries" v
where v."status" in ('pending', 'retrying', 'delivering')
and v."available_at" <= now()
and (v."lease_expires_at" is null or v."lease_expires_at" <= now())
order by v."available_at", v."created_at"
for update skip locked
limit greatest(1, least(coalesce(claim_webhook_deliveries.max_items, 25), 100))
),
claimed as (
update "better_supabase"."webhook_deliveries" v
set "status" = 'delivering',
"attempt" = v."attempt" + 1,
"lease_expires_at" = now() + coalesce(claim_webhook_deliveries.lease, '2 minutes'), "updated_at" = now()
from due
where v."id" = due.id
returning v.*
)
select coalesce(jsonb_agg(jsonb_build_object(
'id', c."id",
'endpoint_id', c."endpoint_id",
'type', c."event_type",
'payload', c."payload",
'attempt', c."attempt",
'url', d."url",
'created_at', c."created_at", 'tenant', c."tenant_id", 'event_id', c."event_id", 'run_id', c."run_id", 'subject', c."subject", 'owner', d."created_by", 'permissions', d."permissions", 'batch', d."batch", 'signing', d."signing", 'format', d."format", 'credential_ref', d."credential_ref"
) order by c."available_at"), '[]')
into result
from claimed c
join "better_supabase"."webhook_endpoints" d on d."id" = c."endpoint_id";
return result;
end;
$$;
revoke execute on function "better_supabase"."claim_webhook_deliveries"(integer, interval, integer) from public, anon, authenticated;
grant execute on function "better_supabase"."claim_webhook_deliveries"(integer, interval, integer) to service_role;
-- Records an attempt for the worker that holds the lease: outcome.attempt
-- must be the attempt the claim returned, and a delivery claimed again in
-- the meantime returns 'stale' unchanged. 'retrying' retries at retry_at, and
-- an endpoint that keeps failing for disableAfter is disabled.
create or replace function "better_supabase"."complete_webhook_delivery"(delivery uuid, outcome jsonb)
returns text
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_status text := complete_webhook_delivery.outcome ->> 'status';
v_attempt integer := (complete_webhook_delivery.outcome ->> 'attempt')::integer;
v_endpoint uuid;
v_tenant uuid;
v_since timestamptz;
v_result text := 'ok';
begin
if v_status is null or v_status not in ('succeeded', 'retrying', 'dead', 'canceled') then
raise exception 'status must be succeeded, retrying, dead or canceled' using errcode = '22023', hint = 'WEBHOOK_STATUS_UNKNOWN';
end if;
if v_attempt is null then
raise exception 'outcome.attempt must be the attempt the claim returned' using errcode = '22023', hint = 'WEBHOOK_ATTEMPT_REQUIRED';
end if;
update "better_supabase"."webhook_deliveries" v
set "status" = v_status,
"attempt" = case when v_status = 'canceled' then greatest(v."attempt" - 1, 0) else v."attempt" end,
"available_at" = case when v_status = 'retrying' then coalesce((complete_webhook_delivery.outcome ->> 'retry_at')::timestamptz, now()) else v."available_at" end,
"lease_expires_at" = null,
"response_status" = (complete_webhook_delivery.outcome ->> 'response_status')::integer,
"response_body" = complete_webhook_delivery.outcome ->> 'response_body',
"duration_ms" = (complete_webhook_delivery.outcome ->> 'duration_ms')::integer,
"last_error" = complete_webhook_delivery.outcome ->> 'error',
"processed_at" = case when v_status = 'retrying' then null else now() end,
"updated_at" = now()
where v."id" = complete_webhook_delivery.delivery
and v."attempt" = v_attempt
and v."status" = 'delivering'
returning v."endpoint_id" into v_endpoint;
if not found then
if exists (select 1 from "better_supabase"."webhook_deliveries" v where v."id" = complete_webhook_delivery.delivery) then
return 'stale';
end if;
raise exception 'Webhook delivery not found' using errcode = 'P0002', hint = 'WEBHOOK_DELIVERY_NOT_FOUND';
end if;
if v_status = 'succeeded' then
update "better_supabase"."webhook_endpoints" d set "first_failed_at" = null
where d."id" = v_endpoint and d."first_failed_at" is not null;
elsif v_status in ('retrying', 'dead') then
update "better_supabase"."webhook_endpoints" d set "first_failed_at" = coalesce(d."first_failed_at", now())
where d."id" = v_endpoint
returning d."first_failed_at", d."tenant_id" into v_since, v_tenant;
if v_since <= now() - '5 days'::interval then
update "better_supabase"."webhook_endpoints" d set "enabled" = false, "disabled_at" = now(), "disabled_reason" = format('Deliveries failed since %s', v_since), "updated_at" = now()
where d."id" = v_endpoint and d."enabled";
if found then
v_result := 'disabled';
null;
end if;
end if;
end if;
declare
v_hook regprocedure := to_regprocedure('"public"."after_webhook_delivery"(uuid, text)');
begin
if v_hook is not null then
execute format('select %s($1::uuid, $2::text)', v_hook::oid::regproc)
using complete_webhook_delivery.delivery, v_status;
end if;
end;
return v_result;
end;
$$;
revoke execute on function "better_supabase"."complete_webhook_delivery"(uuid, jsonb) from public, anon, authenticated;
grant execute on function "better_supabase"."complete_webhook_delivery"(uuid, jsonb) to service_role;
-- Queues a finished delivery again, e.g. after the receiver fixed a bug.
create or replace function "better_supabase"."redeliver_webhook"(delivery uuid)
returns uuid
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_endpoint uuid;
v_status text;
v_tenant uuid;
v_enabled boolean;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select v."endpoint_id", v."status" into v_endpoint, v_status
from "better_supabase"."webhook_deliveries" v where v."id" = redeliver_webhook.delivery;
if not found then
raise exception 'Webhook delivery not found' using errcode = 'P0002', hint = 'WEBHOOK_DELIVERY_NOT_FOUND';
end if;
select d."tenant_id", d."enabled" into v_tenant, v_enabled
from "better_supabase"."webhook_endpoints" d where d."id" = v_endpoint;
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) and not false then
raise exception 'Not allowed to manage this webhook endpoint' using errcode = '42501', hint = 'WEBHOOK_FORBIDDEN';
end if;
if v_status in ('pending', 'delivering') then
raise exception 'The delivery is still in progress' using errcode = 'PT409', hint = 'WEBHOOK_DELIVERY_IN_PROGRESS';
end if;
if not v_enabled then
raise exception 'Webhook endpoint is disabled' using errcode = '55000', hint = 'WEBHOOK_ENDPOINT_DISABLED';
end if;
update "better_supabase"."webhook_deliveries" v
set "status" = 'pending', "attempt" = 0, "available_at" = now(), "lease_expires_at" = null, "processed_at" = null, "last_error" = null, "updated_at" = now()
where v."id" = redeliver_webhook.delivery;
return redeliver_webhook.delivery;
end;
$$;
revoke execute on function "better_supabase"."redeliver_webhook"(uuid) from public, anon, authenticated;
grant execute on function "better_supabase"."redeliver_webhook"(uuid) to authenticated, service_role;
-- One delivery as the caller may read it (a member with the read permission, or
-- the service), or null; the job source of dispatch_webhook and redeliver_webhook.
create or replace function "better_supabase"."get_webhook_delivery"(delivery uuid)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_endpoint uuid;
v_tenant uuid;
v_enabled boolean;
begin
select v."endpoint_id" into v_endpoint
from "better_supabase"."webhook_deliveries" v where v."id" = get_webhook_delivery.delivery;
if not found then
return null;
end if;
select d."tenant_id", d."enabled" into v_tenant, v_enabled
from "better_supabase"."webhook_endpoints" d where d."id" = v_endpoint;
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) and not false then
raise exception 'Not allowed to manage this webhook endpoint' using errcode = '42501', hint = 'WEBHOOK_FORBIDDEN';
end if;
return (
select jsonb_build_object('id', v."id", 'endpointId', v."endpoint_id", 'type', v."event_type", 'status', v."status", 'attempt', v."attempt", 'responseStatus', v."response_status", 'durationMs', v."duration_ms", 'lastError', v."last_error", 'processedAt', v."processed_at", 'createdAt', v."created_at")
from "better_supabase"."webhook_deliveries" v where v."id" = get_webhook_delivery.delivery
);
end;
$$;
revoke execute on function "better_supabase"."get_webhook_delivery"(uuid) from public, anon, authenticated;
grant execute on function "better_supabase"."get_webhook_delivery"(uuid) to authenticated, service_role;
-- Deletes up to batch finished deliveries (and dead ones with include_dead)
-- created before older_than. A purged event id can be queued again for the
-- same endpoint, so keep older_than above the window you republish in.
-- Nightly with pg_cron: select cron.schedule('purge-webhook-deliveries', '45 3 * * *', 'select "better_supabase"."purge_webhook_deliveries"()');
create or replace function "better_supabase"."purge_webhook_deliveries"(older_than interval default '30 days', include_dead boolean default false, batch integer default 10000)
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
v_purged integer;
begin
with purged as (
delete from "better_supabase"."webhook_deliveries"
where "id" in (
select v."id" from "better_supabase"."webhook_deliveries" v
where v."created_at" < now() - purge_webhook_deliveries.older_than
and (
v."status" in ('succeeded', 'canceled')
or (purge_webhook_deliveries.include_dead and v."status" = 'dead')
)
limit purge_webhook_deliveries.batch
)
returning 1
)
select count(*)::integer into v_purged from purged;
return v_purged;
end;
$$;
revoke execute on function "better_supabase"."purge_webhook_deliveries"(interval, boolean, integer) from public, anon, authenticated;
grant execute on function "better_supabase"."purge_webhook_deliveries"(interval, boolean, integer) to service_role;
-- A new signing secret, returned once. Older secrets keep signing for
-- overlap, so receivers can switch without dropping deliveries.
create or replace function "better_supabase"."rotate_webhook_secret"(endpoint uuid, overlap interval default '24 hours', secret text default null)
returns text
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_tenant uuid;
v_enabled boolean;
v_secret text := coalesce(
rotate_webhook_secret.secret,
'whsec_' || encode(extensions.gen_random_bytes(32), 'base64')
);
v_vault uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select d."tenant_id", d."enabled" into v_tenant, v_enabled
from "better_supabase"."webhook_endpoints" d where d."id" = rotate_webhook_secret.endpoint;
if not found then
raise exception 'Webhook endpoint not found' using errcode = 'P0002', hint = 'WEBHOOK_ENDPOINT_NOT_FOUND';
end if;
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) and not false then
raise exception 'Not allowed to manage this webhook endpoint' using errcode = '42501', hint = 'WEBHOOK_FORBIDDEN';
end if;
if length(v_secret) < 16 then
raise exception 'A webhook secret needs at least 16 characters' using errcode = '22023', hint = 'WEBHOOK_SECRET_TOO_SHORT';
end if;
delete from vault.secrets vs using "better_supabase"."webhook_endpoint_secrets" s where vs.id = s."vault_secret_id" and s."endpoint_id" = rotate_webhook_secret.endpoint and s."expires_at" <= now();
delete from "better_supabase"."webhook_endpoint_secrets" s where s."endpoint_id" = rotate_webhook_secret.endpoint and s."expires_at" <= now();
update "better_supabase"."webhook_endpoint_secrets" s set "expires_at" = now() + coalesce(rotate_webhook_secret.overlap, '24 hours')
where s."endpoint_id" = rotate_webhook_secret.endpoint and s."expires_at" is null;
v_vault := vault.create_secret(
v_secret,
'webhook:' || rotate_webhook_secret.endpoint::text || ':' || gen_random_uuid()::text,
'better-supabase outgoing webhook signing secret'
);
insert into "better_supabase"."webhook_endpoint_secrets" ("endpoint_id", "vault_secret_id", "tenant_id")
values (rotate_webhook_secret.endpoint, v_vault, v_tenant);
null;
return v_secret;
end;
$$;
revoke execute on function "better_supabase"."rotate_webhook_secret"(uuid, interval, text) from public, anon, authenticated;
grant execute on function "better_supabase"."rotate_webhook_secret"(uuid, interval, text) to authenticated, service_role;
-- The secrets that sign an endpoint's deliveries: the current one first, then by age.
create or replace function "better_supabase"."webhook_secrets"(endpoint uuid)
returns text[]
language sql
stable
security definer
set search_path = ''
as $$
select coalesce(array_agg(x.secret order by x.expires desc nulls first, x.created desc), '{}')
from (
select ds.decrypted_secret as secret, s."expires_at" as expires, s."created_at" as created
from "better_supabase"."webhook_endpoint_secrets" s
join vault.decrypted_secrets ds on ds.id = s."vault_secret_id"
where s."endpoint_id" = webhook_secrets.endpoint
and (s."expires_at" is null or s."expires_at" > now())
) x
$$;
revoke execute on function "better_supabase"."webhook_secrets"(uuid) from public, anon, authenticated;
grant execute on function "better_supabase"."webhook_secrets"(uuid) to service_role;
-- Adds an endpoint to tenant, from fields such as {"url", "eventTypes",
-- "name"}; returns it. A field it leaves out gets the column default.
create or replace function "better_supabase"."create_webhook_endpoint"(tenant uuid, fields jsonb)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_tenant uuid := create_webhook_endpoint.tenant;
v_id uuid;
v_endpoint jsonb;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) and not false then
raise exception 'Not allowed to manage this webhook endpoint' using errcode = '42501', hint = 'WEBHOOK_FORBIDDEN';
end if;
if jsonb_typeof(coalesce(create_webhook_endpoint.fields, '{}')) <> 'object' or exists (
select 1 from jsonb_each(coalesce(create_webhook_endpoint.fields, '{}')) e
where case e.key when 'name' then jsonb_typeof(e.value) <> 'string' when 'url' then jsonb_typeof(e.value) <> 'string' when 'enabled' then jsonb_typeof(e.value) <> 'boolean' when 'eventTypes' then (jsonb_typeof(e.value) <> 'array' or exists (select 1 from jsonb_array_elements(e.value) a where jsonb_typeof(a) <> 'string')) when 'permissions' then (jsonb_typeof(e.value) <> 'null' and (jsonb_typeof(e.value) <> 'array' or exists (select 1 from jsonb_array_elements(e.value) a where jsonb_typeof(a) <> 'string'))) when 'batch' then jsonb_typeof(e.value) <> 'boolean' when 'signing' then jsonb_typeof(e.value) <> 'string' when 'format' then jsonb_typeof(e.value) <> 'string' when 'credentialRef' then jsonb_typeof(e.value) not in ('object', 'null') else true end
) then
raise exception 'A webhook endpoint field is unknown or has the wrong type' using errcode = '22023', hint = 'WEBHOOK_ENDPOINT_INVALID';
end if;
if create_webhook_endpoint.fields ->> 'url' is null or create_webhook_endpoint.fields ->> 'url' !~* '^https://' then
raise exception 'A webhook endpoint URL must start with https://' using errcode = '22023', hint = 'WEBHOOK_ENDPOINT_URL';
end if;
insert into "better_supabase"."webhook_endpoints" ("tenant_id", "url", "enabled", "event_types", "name")
values (create_webhook_endpoint.tenant, create_webhook_endpoint.fields ->> 'url', coalesce((create_webhook_endpoint.fields ->> 'enabled')::boolean, true), coalesce(array(select jsonb_array_elements_text(create_webhook_endpoint.fields -> 'eventTypes')), '{}'), create_webhook_endpoint.fields ->> 'name')
returning "id" into v_id;
if create_webhook_endpoint.fields ?| array['permissions', 'batch', 'signing', 'format', 'credentialRef'] then
update "better_supabase"."webhook_endpoints" d set "permissions" = case when create_webhook_endpoint.fields ? 'permissions' then case when jsonb_typeof(create_webhook_endpoint.fields -> 'permissions') = 'null' then null else array(select jsonb_array_elements_text(create_webhook_endpoint.fields -> 'permissions')) end else d."permissions" end, "batch" = case when create_webhook_endpoint.fields ? 'batch' then (create_webhook_endpoint.fields ->> 'batch')::boolean else d."batch" end, "signing" = case when create_webhook_endpoint.fields ? 'signing' then create_webhook_endpoint.fields ->> 'signing' else d."signing" end, "format" = case when create_webhook_endpoint.fields ? 'format' then create_webhook_endpoint.fields ->> 'format' else d."format" end, "credential_ref" = case when create_webhook_endpoint.fields ? 'credentialRef' then nullif(create_webhook_endpoint.fields -> 'credentialRef', 'null'::jsonb) else d."credential_ref" end, "updated_at" = now()
where d."id" = v_id;
end if;
select jsonb_build_object('id', d."id", 'tenant', d."tenant_id", 'name', d."name", 'url', d."url", 'enabled', d."enabled", 'eventTypes', d."event_types", 'permissions', d."permissions", 'batch', d."batch", 'signing', d."signing", 'format', d."format", 'credentialRef', d."credential_ref", 'failingSince', d."first_failed_at", 'disabledAt', d."disabled_at", 'disabledReason', d."disabled_reason", 'createdBy', d."created_by", 'createdAt', d."created_at", 'updatedAt', d."updated_at") into v_endpoint
from "better_supabase"."webhook_endpoints" d where d."id" = v_id;
return v_endpoint;
end;
$$;
revoke execute on function "better_supabase"."create_webhook_endpoint"(uuid, jsonb) from public, anon, authenticated;
grant execute on function "better_supabase"."create_webhook_endpoint"(uuid, jsonb) to authenticated, service_role;
-- Changes the fields of an endpoint that fields names; returns it. Turning
-- enabled back on clears the failure streak.
create or replace function "better_supabase"."update_webhook_endpoint"(endpoint uuid, fields jsonb)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_tenant uuid;
v_endpoint jsonb;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select d."tenant_id" into v_tenant
from "better_supabase"."webhook_endpoints" d where d."id" = update_webhook_endpoint.endpoint;
if not found then
raise exception 'Webhook endpoint not found' using errcode = 'P0002', hint = 'WEBHOOK_ENDPOINT_NOT_FOUND';
end if;
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) and not false then
raise exception 'Not allowed to manage this webhook endpoint' using errcode = '42501', hint = 'WEBHOOK_FORBIDDEN';
end if;
if jsonb_typeof(coalesce(update_webhook_endpoint.fields, '{}')) <> 'object' or exists (
select 1 from jsonb_each(coalesce(update_webhook_endpoint.fields, '{}')) e
where case e.key when 'name' then jsonb_typeof(e.value) <> 'string' when 'url' then jsonb_typeof(e.value) <> 'string' when 'enabled' then jsonb_typeof(e.value) <> 'boolean' when 'eventTypes' then (jsonb_typeof(e.value) <> 'array' or exists (select 1 from jsonb_array_elements(e.value) a where jsonb_typeof(a) <> 'string')) when 'permissions' then (jsonb_typeof(e.value) <> 'null' and (jsonb_typeof(e.value) <> 'array' or exists (select 1 from jsonb_array_elements(e.value) a where jsonb_typeof(a) <> 'string'))) when 'batch' then jsonb_typeof(e.value) <> 'boolean' when 'signing' then jsonb_typeof(e.value) <> 'string' when 'format' then jsonb_typeof(e.value) <> 'string' when 'credentialRef' then jsonb_typeof(e.value) not in ('object', 'null') else true end
) then
raise exception 'A webhook endpoint field is unknown or has the wrong type' using errcode = '22023', hint = 'WEBHOOK_ENDPOINT_INVALID';
end if;
if update_webhook_endpoint.fields ? 'url' then
if update_webhook_endpoint.fields ->> 'url' is null or update_webhook_endpoint.fields ->> 'url' !~* '^https://' then
raise exception 'A webhook endpoint URL must start with https://' using errcode = '22023', hint = 'WEBHOOK_ENDPOINT_URL';
end if;
end if;
update "better_supabase"."webhook_endpoints" d set "name" = case when update_webhook_endpoint.fields ? 'name' then update_webhook_endpoint.fields ->> 'name' else d."name" end, "url" = case when update_webhook_endpoint.fields ? 'url' then update_webhook_endpoint.fields ->> 'url' else d."url" end, "enabled" = case when update_webhook_endpoint.fields ? 'enabled' then (update_webhook_endpoint.fields ->> 'enabled')::boolean else d."enabled" end, "event_types" = case when update_webhook_endpoint.fields ? 'eventTypes' then array(select jsonb_array_elements_text(update_webhook_endpoint.fields -> 'eventTypes')) else d."event_types" end, "permissions" = case when update_webhook_endpoint.fields ? 'permissions' then case when jsonb_typeof(update_webhook_endpoint.fields -> 'permissions') = 'null' then null else array(select jsonb_array_elements_text(update_webhook_endpoint.fields -> 'permissions')) end else d."permissions" end, "batch" = case when update_webhook_endpoint.fields ? 'batch' then (update_webhook_endpoint.fields ->> 'batch')::boolean else d."batch" end, "signing" = case when update_webhook_endpoint.fields ? 'signing' then update_webhook_endpoint.fields ->> 'signing' else d."signing" end, "format" = case when update_webhook_endpoint.fields ? 'format' then update_webhook_endpoint.fields ->> 'format' else d."format" end, "credential_ref" = case when update_webhook_endpoint.fields ? 'credentialRef' then nullif(update_webhook_endpoint.fields -> 'credentialRef', 'null'::jsonb) else d."credential_ref" end, "updated_at" = now()
where d."id" = update_webhook_endpoint.endpoint;
select jsonb_build_object('id', d."id", 'tenant', d."tenant_id", 'name', d."name", 'url', d."url", 'enabled', d."enabled", 'eventTypes', d."event_types", 'permissions', d."permissions", 'batch', d."batch", 'signing', d."signing", 'format', d."format", 'credentialRef', d."credential_ref", 'failingSince', d."first_failed_at", 'disabledAt', d."disabled_at", 'disabledReason', d."disabled_reason", 'createdBy', d."created_by", 'createdAt', d."created_at", 'updatedAt', d."updated_at") into v_endpoint
from "better_supabase"."webhook_endpoints" d where d."id" = update_webhook_endpoint.endpoint;
return v_endpoint;
end;
$$;
revoke execute on function "better_supabase"."update_webhook_endpoint"(uuid, jsonb) from public, anon, authenticated;
grant execute on function "better_supabase"."update_webhook_endpoint"(uuid, jsonb) to authenticated, service_role;
-- The tenant's endpoints, newest first, without their secrets.
create or replace function "better_supabase"."list_webhook_endpoints"(tenant uuid)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_tenant uuid := list_webhook_endpoints.tenant;
v_endpoints jsonb;
begin
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) and not false then
raise exception 'Not allowed to manage this webhook endpoint' using errcode = '42501', hint = 'WEBHOOK_FORBIDDEN';
end if;
select coalesce(jsonb_agg(jsonb_build_object('id', d."id", 'tenant', d."tenant_id", 'name', d."name", 'url', d."url", 'enabled', d."enabled", 'eventTypes', d."event_types", 'permissions', d."permissions", 'batch', d."batch", 'signing', d."signing", 'format', d."format", 'credentialRef', d."credential_ref", 'failingSince', d."first_failed_at", 'disabledAt', d."disabled_at", 'disabledReason', d."disabled_reason", 'createdBy', d."created_by", 'createdAt', d."created_at", 'updatedAt', d."updated_at") order by d."created_at" desc, d."id"), '[]')
into v_endpoints
from "better_supabase"."webhook_endpoints" d
where d."tenant_id" = list_webhook_endpoints.tenant;
return v_endpoints;
end;
$$;
revoke execute on function "better_supabase"."list_webhook_endpoints"(uuid) from public, anon, authenticated;
grant execute on function "better_supabase"."list_webhook_endpoints"(uuid) to authenticated, service_role;
-- Deletes an endpoint with its secrets and deliveries.
create or replace function "better_supabase"."delete_webhook_endpoint"(endpoint uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_tenant uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select d."tenant_id" into v_tenant
from "better_supabase"."webhook_endpoints" d where d."id" = delete_webhook_endpoint.endpoint;
if not found then
raise exception 'Webhook endpoint not found' using errcode = 'P0002', hint = 'WEBHOOK_ENDPOINT_NOT_FOUND';
end if;
if not (coalesce(nullif((select auth.jwt()) ->> 'role', ''), session_user::text) in ('service_role', 'postgres', 'supabase_admin')) and not false then
raise exception 'Not allowed to manage this webhook endpoint' using errcode = '42501', hint = 'WEBHOOK_FORBIDDEN';
end if;
delete from "better_supabase"."webhook_endpoints" d
where d."id" = delete_webhook_endpoint.endpoint;
return true;
end;
$$;
revoke execute on function "better_supabase"."delete_webhook_endpoint"(uuid) from public, anon, authenticated;
grant execute on function "better_supabase"."delete_webhook_endpoint"(uuid) to authenticated, service_role;
Client
This block has no React entry yet. Its TypeScript side is in better-supabase/blocks/webhooks, which runs on the server.
Events
With the outbox installed, the module writes these in the same transaction as the change.
- webhook_endpoint.disabled
- webhook_endpoint.secret_rotated
Dependencies
- Required: the install command adds it too
- Works with: optional
Recipes
More in Developer platform
Incoming webhook endpointsincoming-webhooks Per-tenant trigger URLs with a hashed token, optional Standard Webhooks or HMAC verification, a body size limit, receive counters and the last status; deliveries go to the webhook inbox with the endpoint's tenant. API keysapi-keys Hashed API keys for tenants and users with scopes, expiry, rotation with a grace period, a per-key rate limit and throttled last-used tracking. verify_api_key() backs apiKeyResolver; has_scope() and api_key_tenant() go in policies.React hooks Credentials in Vaultcredentials Third-party tokens and API keys in Supabase Vault as bs:cred:<provider>:<name>, behind security definer credential_get, credential_set and credential_delete functions only the service role may call. vaultCredentials() in better-supabase/credentials resolves credential references through them. Service principalsservice-accounts Non-human tenant members backed by auth.users rows that can't sign in, with roles through memberships. Organization-owned workflow versions and service webhook endpoints run as the tenant's default principal. Signing keyssigning-keys Ed25519 or ES256 signing keys per owner: the private JWK in Supabase Vault, one active key per owner, rotation that keeps a retired kid in the JWKS until the latest exp a signer noted, and a purge. Only the service role calls it; createSigningKeys() in better-supabase/blocks/signing-keys generates keys and signs with WebCrypto. Shared signalssecurity-events OpenID Shared Signals streams: stream configuration and status per receiver, push delivery through a webhooks endpoint with format 'set' (RFC 8935), a poll queue (RFC 8936), SETs held while a stream is paused, and a receiver replay table. Only the service role calls it; createSsfTransmitter() and createSsfReceiver() in better-supabase/blocks/security-events mint and verify the SETs.