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
POSTwith a JSON body, - keep the raw body as it arrived, because the signature covers those exact bytes,
- answer with a
2xxstatus within 10 seconds, then do the slow work, - handle each event
idonly 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
- 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.
- Open Developers → Webhooks and click Add endpoint.
- Fill in the form:
| Field | What to enter |
|---|---|
| Address we call | An https address of your own, with no username, password or port. Calls go to port 443 only. |
| Format | Keep Current. Pick Old format (v1) only if your code already reads the API v0 payload. You cannot change this later. |
| What it hears about | At 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. |
- 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=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bdtis the Unix time in seconds when the call was signed.v2is the HMAC-SHA256, in hex, oft, a dot, and the raw body, keyed with your signing secret (the wholewhsec_...string).- While you are swapping secrets, the header carries two
v2values, one for each secret.
To check a call:
- Split the header on
,and readtand everyv2value. Ignore keys you do not know. - Refuse the call if
tis more than 5 minutes away from your clock. - Compute the HMAC of
`${t}.`followed by the raw body, with your secret. - Accept the call if it matches any
v2value. 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.
| Action | What happens |
|---|---|
| Edit | Change 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 secret | We show a new secret once. The old one keeps working for a day, so you have time to swap it. |
| Turn off / Turn on | An endpoint that is off receives nothing. Events that happen while it is off are not sent later. |
| Send a test | See Test webhooks. |
| History | The recent calls to this address, see Test webhooks. |
| Delete endpoint | Type the address to confirm. There is no undo. |
Troubleshooting
| Problem | What to check |
|---|---|
| Nothing arrives | The endpoint is On, picks the event, and is in the same mode (live or test) as the event. |
| Signature does not match | You are hashing the raw body, with the whole secret, and t. in front. |
| Calls time out | Answer 2xx first and do the work afterwards. |
| The same event twice | Expected: skip any id you have already handled. |
Next steps
Test webhooks
Send a test event and read what your endpoint answered
Last updated