Receive webhooks
Use this guide when your server should act on events as they happen, such as closing a ticket in another system when a request is resolved. To read events when you choose instead, query the history as the EventWolf quickstart does. Deliveries explains how EventWolf sends, signs, and retries.
The receiver here is a Node HTTP server on port 8787, or on PORT when it is set. receiverUrl must reach it from the internet over HTTPS, such as through your hosting provider’s routing or a tunnel.
Before you start
- The
apihelper, from Platform quickstart. - A
devsecret key,key, for a project that adopted EventWolf, from Create a project and issue a key. - A public HTTPS URL,
receiverUrl, that reaches the port where the receiver listens, from EventWolf deliveries.
This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.
1. Create an endpoint
An endpoint is where deliveries go. Creating one answers with the endpoint and its signing secret, secret, whsec_…, shown this once:
const { endpoint, secret } = await api('POST', '/v1/events/endpoints', key, {
url: receiverUrl,
description: 'Support desk: resolved requests',
});
console.log(endpoint.status, secret.slice(0, 6)); // active whsec_
Keep secret with your server’s other secrets. Rotate a signing secret replaces it without missing a delivery: the old secret keeps signing beside the new one for the expiresIn you send, up to 7d, while you switch your receiver.
2. Verify each delivery
Compute the HMAC over <webhook-id>.<webhook-timestamp>.<raw body> using the delivery signature format. Before parsing the body, reject timestamps more than 5 minutes from your clock and compare the HMAC with each space-separated v1,<base64> signature. Accept the signature if any entry matches.
Answer 2xx within the config’s delivery.timeout. Agentpost starts projects at 15 seconds. A retry or a replay can bring the same event again, so act on each source and id once.
Run this block after step 1 in the same .mts script with Node 24. If you run or restart the receiver in a separate process, load the endpoint’s saved whsec_… signing secret as secret before starting it. Keep the existing endpoint and subscription.
import { createHmac, timingSafeEqual } from 'node:crypto';
import { createServer, type IncomingHttpHeaders } from 'node:http';
function verified(secret: string, headers: IncomingHttpHeaders, body: Buffer) {
const id = headers['webhook-id'];
const timestamp = headers['webhook-timestamp'];
const signatures = headers['webhook-signature'];
if (
typeof id !== 'string' ||
typeof timestamp !== 'string' ||
typeof signatures !== 'string' ||
!/^\d+$/.test(timestamp) ||
Math.abs(Date.now() / 1000 - Number(timestamp)) > 300
) {
return false;
}
const expected = createHmac(
'sha256',
Buffer.from(secret.slice('whsec_'.length), 'base64'),
)
.update(`${id}.${timestamp}.`)
.update(body)
.digest();
return signatures.split(' ').some((signature) => {
const [version, value = ''] = signature.split(',');
const given = Buffer.from(value, 'base64');
return (
version === 'v1' &&
given.length === expected.length &&
timingSafeEqual(given, expected)
);
});
}
const handled = new Set<string>();
const receiver = createServer(async (request, response) => {
const chunks: Buffer[] = [];
for await (const chunk of request) chunks.push(chunk);
const body = Buffer.concat(chunks);
if (!verified(secret, request.headers, body)) {
response.writeHead(401).end();
return;
}
const event = JSON.parse(body.toString('utf8'));
const eventKey = `${event.source} ${event.id}`;
if (!handled.has(eventKey)) {
handled.add(eventKey);
// Act on the event here, such as closing the ticket for `event.subject`.
}
response.writeHead(200).end();
});
receiver.listen(Number(process.env.PORT ?? 8787));
Any answer but 2xx in time is a failed attempt, and EventWolf retries it on its schedule, except a 410, which disables the endpoint. Keep handled in your database so it survives a restart.
3. Subscribe the endpoint
A subscription sends the events that match its filter to the endpoint. This one sends every resolved request:
const { subscription } = await api('POST', '/v1/events/subscriptions', key, {
name: 'Resolved requests to the support desk',
filter: { types: ['request.resolved'] },
target: { kind: 'webhook', endpointId: endpoint.id },
});
console.log(subscription.status, subscription.version); // active 1
It matches events accepted from now on. To send events from before it existed, replay them.
Check it worked
Send the endpoint a test. EventWolf delivers a tools.agentpost.events.endpoint.test event at once, signed like any delivery, and answers with the attempt:
const ping = await api('POST', `/v1/events/endpoints/${endpoint.id}/test`, key);
const [attempt] = ping.attempts;
console.log(
ping.delivery.status,
attempt.response?.status ?? attempt.error?.code,
); // succeeded 200
If the attempt fails, ping.attempts[0] holds its response status or an error such as connection. The delivery is pending if a retry is scheduled. Fix failed deliveries reads each cause; send another test once it is fixed.