Skip to content

Website Change Alerts in a Supabase Edge Function

Receive AyeWatch website change alerts in a Supabase Edge Function: turn off JWT checks for that one function, verify the HMAC with Web Crypto, and insert each update_id once.

By AyeWatch Team4 min read

Short answer: To receive website change alerts in Supabase, deploy an Edge Function with verify_jwt = false in supabase/config.toml, because AyeWatch sends no JWT. In the function, read await req.arrayBuffer(), verify X-AyeWatch-Signature with crypto.subtle.verify, insert the alert with update_id as the primary key, and return 200 within 10 seconds.

First: turn off the JWT check for this function

Supabase's docs say that by default Edge Functions require a valid JWT in the authorization header. AyeWatch sends only Content-Type and X-AyeWatch-Signature, so with the default in place every delivery gets a 401 before your code runs, and AyeWatch retries until it gives up.

Turn the check off for this one function in supabase/config.toml:

[functions.ayewatch-webhook]
verify_jwt = false

The HMAC check then does the job the JWT did. Leave the JWT check on for your other functions.

The table

create table public.ayewatch_alerts (
  update_id   bigint primary key,
  topic_id    bigint not null,
  topic_name  text not null,
  headline    text,
  content     text,
  created_at  timestamptz not null,
  received_at timestamptz not null default now()
);

alter table public.ayewatch_alerts enable row level security;

Row Level Security with no policies keeps the table closed to the public API keys. The function writes with the service role key, which bypasses RLS and never leaves the server. Add a select policy later if signed-in users should read alerts in your app.

The function

Sample rule: Alert me when the city of Lakemoor's permits page posts new rules or fee changes for short-term rentals. Ignore meeting agendas and job postings.

// supabase/functions/ayewatch-webhook/index.ts
import { createClient } from "npm:@supabase/supabase-js@2";

const supabase = createClient(Deno.env.get("SUPABASE_URL")!, Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!);
const secret = Deno.env.get("AYEWATCH_SECRET")!;

async function verify(raw: ArrayBuffer, header: string, secret: string): Promise<boolean> {
  const hex = header.startsWith("sha256=") ? header.slice(7) : "";
  if (!/^[0-9a-f]{64}$/.test(hex)) return false;
  const sig = new Uint8Array(32);
  for (let i = 0; i < 32; i++) sig[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
  const key = await crypto.subtle.importKey(
    "raw",
    new TextEncoder().encode(secret),
    { name: "HMAC", hash: "SHA-256" },
    false,
    ["verify"]
  );
  return crypto.subtle.verify("HMAC", key, sig, raw);
}

Deno.serve(async (req) => {
  if (req.method !== "POST") return new Response("Method not allowed", { status: 405 });

  // 1. Raw body, 2. signature
  const raw = await req.arrayBuffer();
  if (!(await verify(raw, req.headers.get("x-ayewatch-signature") ?? "", secret))) {
    return new Response("Bad signature", { status: 401 });
  }

  // 3. Dedupe and act: 23505 is Postgres's unique violation, so this update_id is stored already
  const alert = JSON.parse(new TextDecoder().decode(raw));
  const { error } = await supabase.from("ayewatch_alerts").insert({
    update_id: alert.update_id,
    topic_id: alert.topic_id,
    topic_name: alert.topic_name,
    headline: alert.headline ?? null,
    content: alert.content ?? null,
    created_at: alert.created_at,
  });
  if (error?.code === "23505") return new Response("Already handled");
  if (error) return new Response("Insert failed", { status: 500 });

  // 4. Answer
  return new Response("ok");
});

Supabase sets SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY for every function. You add AYEWATCH_SECRET yourself. Names that start with SUPABASE_ are reserved, so pick another prefix.

Deploy, and where slow work goes

supabase secrets set AYEWATCH_SECRET=paste-the-signing-secret
supabase functions deploy ayewatch-webhook

The URL is https://YOUR_PROJECT_REF.supabase.co/functions/v1/ayewatch-webhook. Before you paste it into AyeWatch, call it once with curl -i -X POST and no signature. A 401 with the text Bad signature proves the JWT check is off and your code ran. A 401 that mentions a missing authorization header means verify_jwt is still on.

The insert is quick, so the function acts before it answers and a failed insert gets a retry. For slower follow-ups, such as calling another API, start them with EdgeRuntime.waitUntil(promise) just before the return and do not await them. AyeWatch has its 200 by then, so failures there are yours to catch. Inside your own app, a Realtime subscription on the table can update screens as alerts arrive.

Limits and cost

  • Time. Supabase allows 2 seconds of CPU time and a wall clock of 150 seconds on Free and 400 on paid plans, as of December 2026. AyeWatch still needs the reply within 10 seconds.
  • One webhook per account. Store topic_id and filter on it in your app.
  • Service role key. It bypasses RLS. Keep it in the function, never in client code.
  • Local runs. supabase functions serve runs the function on your machine, but AyeWatch cannot reach a local address. The first real alert arrives after the deploy.
  • Public pages only. AyeWatch reads public pages and cannot sign in.

AyeWatch Pro is $9/mo with 50 topics and hourly checks; Pro+ is $19/mo with 200 topics and ASAP mode. Every plan starts with a 7-day free trial. Four city pages in separate web page monitors, checked daily, use 4 × 30 = 120 credits a month.

From empty project to stored alerts

  1. Run the table SQL in the SQL editor.
  2. Create the function with supabase functions new ayewatch-webhook and paste the code above.
  3. Add verify_jwt = false for it in supabase/config.toml and deploy.
  4. In AyeWatch, open Notification settings, Webhook, paste the function URL and select Save Webhook. Reveal and copy the signing secret.
  5. Run supabase secrets set AYEWATCH_SECRET=... with that secret.
  6. Wait for a real update and check the table and the function's logs.
SymptomCauseFix
401 before your code runsJWT check is onverify_jwt = false for this function
401 Bad signatureWrong secret, or body parsed before checkingVerify req.arrayBuffer() with the current secret
Duplicate rowsNo unique keyupdate_id as the primary key
Insert failedMissing service role client or tableCheck the logs and the table name

Start a monitor in under a minute

Describe a topic or paste a URL. Cancel before the trial ends and you pay nothing.

Try it free for 7 days

The bottom line

Turn off the JWT check for this one function, let the HMAC do the guarding, and let the primary key handle repeats. The alerts then sit in your own Postgres, ready for your app to read.

FAQ

Why does my Supabase Edge Function return 401 for webhooks?

Edge Functions require a valid JWT by default. Webhook senders like AyeWatch do not send one, so set verify_jwt = false for that function in supabase/config.toml and check the HMAC signature instead.

How do I verify an HMAC signature in a Supabase Edge Function?

Read req.arrayBuffer(), import the secret with crypto.subtle.importKey as an HMAC SHA-256 key, and call crypto.subtle.verify with the decoded hex signature.

How do I dedupe webhook retries in Supabase?

Make update_id the primary key. A second insert fails with Postgres code 23505, which the function treats as already handled.

Where do I store the webhook secret?

Run supabase secrets set AYEWATCH_SECRET=... or use the dashboard, and read it with Deno.env.get. Names that start with SUPABASE_ are reserved.

Can the function keep working after it replies?

Yes, with EdgeRuntime.waitUntil. AyeWatch will not retry a failure in that work, because it already got a 200.

website change alerts supabase edge functionsupabase edge function webhook verify_jwtsupabase edge function hmac signaturesupabase webhook 401 missing authorization headerdeno webhook signature verification

Related articles