Webhooks

Set Up Webhooks

Add a webhook endpoint in the dashboard and check that each call really came from Fungies.

Write the handler

An address on your server that accepts a POST and answers 2xx

Add the endpoint

In the dashboard, under Developers → Webhooks

Check the signature

With the signing secret the dashboard shows you once

1. Write the handler

Your endpoint needs to:

  • accept a POST with a JSON body,
  • keep the raw body as it arrived, because the signature covers those exact bytes,
  • answer with a 2xx status within 10 seconds, then do the slow work,
  • handle each event id only once, since an event can arrive more than once.
import express from "express";

const app = express();

app.post("/webhooks/fungies", express.raw({ type: "application/json" }), (req, res) => {
  // Check the signature first, see step 3.
  const event = JSON.parse(req.body.toString("utf8"));

  switch (event.type) {
    case "order.paid":
      // deliver what was bought
      break;
    case "subscription.cancelled":
      // end access
      break;
    default:
      // an event you did not pick for this handler
      break;
  }

  res.sendStatus(200);
});

app.listen(3000);

2. Add the endpoint

  1. In the dashboard, check the mode you are in. An endpoint added in test mode gets test events only; add a second one in live mode when you go live.
  2. Open Developers → Webhooks and click Add endpoint.
  3. Fill in the form:
FieldWhat to enter
Address we callAn https address of your own, with no username, password or port. Calls go to port 443 only.
FormatKeep Current. Pick Old format (v1) only if your code already reads the API v0 payload. You cannot change this later.
What it hears aboutAt least one event. Select all on a group picks every event in it today; events added to the group later are not picked for you.
  1. Click Add endpoint.

Keep the signing secret

Right after you add the endpoint, the dashboard shows its signing secret, starting whsec_.

This is the only time the secret is shown. Copy it into your server's secret store before you close the dialog. If you lose it, give the endpoint a new secret (below).

3. Check the signature

Every call has an Fngs-Signature header:

Fngs-Signature: t=1762351391,v2=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bd
  • t is the Unix time in seconds when the call was signed.
  • v2 is the HMAC-SHA256, in hex, of t, a dot, and the raw body, keyed with your signing secret (the whole whsec_... string).
  • While you are swapping secrets, the header carries two v2 values, one for each secret.

To check a call:

  1. Split the header on , and read t and every v2 value. Ignore keys you do not know.
  2. Refuse the call if t is more than 5 minutes away from your clock.
  3. Compute the HMAC of `${t}.` followed by the raw body, with your secret.
  4. Accept the call if it matches any v2 value. Compare in constant time.
import crypto from "node:crypto";

export function verifyFungiesSignature(
  rawBody: Buffer,
  header: string | undefined,
  secret: string,
  toleranceSeconds = 300,
): boolean {
  if (!header) return false;
  let timestamp: number | null = null;
  const signatures: string[] = [];
  for (const part of header.split(",")) {
    const [key, value] = part.split("=", 2).map((s) => s.trim());
    if (key === "t" && /^\d+$/.test(value)) timestamp = Number(value);
    else if (key === "v2" && /^[0-9a-f]{64}$/.test(value)) signatures.push(value);
  }
  if (timestamp === null || signatures.length === 0) return false;
  if (Math.abs(Date.now() / 1000 - timestamp) > toleranceSeconds) return false;

  const expected = crypto
    .createHmac("sha256", secret)
    .update(`${timestamp}.`)
    .update(rawBody)
    .digest();
  return signatures.some((candidate) => {
    const given = Buffer.from(candidate, "hex");
    return given.length === expected.length && crypto.timingSafeEqual(given, expected);
  });
}

In the handler from step 1:

if (!verifyFungiesSignature(req.body, req.header("Fngs-Signature"), process.env.FUNGIES_WEBHOOK_SECRET!)) {
  return res.sendStatus(400);
}

Check the signature on the raw body. Parsing the JSON and writing it out again changes the bytes, and the check fails.

Manage an endpoint

Open the endpoint from the list, or use the row's menu.

ActionWhat happens
EditChange the address, the events, or both. The secret stays the same. Calls still waiting for a retry go to the new address; events already on their way still arrive.
New secretWe show a new secret once. The old one keeps working for a day, so you have time to swap it.
Turn off / Turn onAn endpoint that is off receives nothing. Events that happen while it is off are not sent later.
Send a testSee Test webhooks.
HistoryThe recent calls to this address, see Test webhooks.
Delete endpointType the address to confirm. There is no undo.

Troubleshooting

ProblemWhat to check
Nothing arrivesThe endpoint is On, picks the event, and is in the same mode (live or test) as the event.
Signature does not matchYou are hashing the raw body, with the whole secret, and t. in front.
Calls time outAnswer 2xx first and do the work afterwards.
The same event twiceExpected: skip any id you have already handled.

Next steps

Test webhooks

Send a test event and read what your endpoint answered

Last updated