Receiving Webhooks
The receiver is whatever answers at the webhook's URL: your ERP's integration endpoint, a middleware or integration platform, a small service of your own, or a Facilio Vibe app. Facilio does not care which, as long as it accepts an HTTPS POST with a JSON body.
Checklist for a Receiver
- Answer quickly. Store the event (a queue, a table, a file) and answer
2xxright away. Do the real work, such as calling your ERP, afterwards. Facilio gives up on an attempt after 10 seconds of silence and retries it later. - Verify the signature. Check
X-Facilio-Signaturewith the webhook's signing secret before trusting the body (see Verifying the Signature). - Ignore duplicates. Delivery is at least once: after a network problem the same event can arrive twice. Keep the
id(also inX-Facilio-Delivery) of the events you have processed, and skip one you have seen. - Skip tests. Do not act on events with
"test": true(headerX-Facilio-Test: true), or on"event": "ping". - Route by
eventandtrigger.linkName. One URL can serve several webhooks and modules. - Use the latest data.
datais the record as it is when the event is sent. If you need more, for example related records, read them with the V5 API using the IDs in the event. - Answer errors honestly. If you cannot take the event right now, answer
503(or any5xx) and Facilio retries it. Answer4xxonly when retrying would never help, because a4xxmarks the event as failed at once.
A Minimal Receiver
A small Node.js (Express) receiver that does all of the above:
const express = require('express');
const crypto = require('crypto');
const SECRET = process.env.FACILIO_WEBHOOK_SECRET; // whsec_...
const seen = new Set(); // use a database in production
const app = express();
// Keep the raw body: the signature is computed over it.
app.post('/hooks/facilio', express.raw({ type: 'application/json' }), (req, res) => {
const raw = req.body.toString('utf8');
const parts = Object.fromEntries((req.get('X-Facilio-Signature') || '').split(',').map((p) => p.split('=', 2)));
const expected = crypto.createHmac('sha256', SECRET).update(`${parts.t}.${raw}`).digest('hex');
const fresh = Math.abs(Date.now() / 1000 - Number(parts.t)) <= 300;
if (!fresh || !parts.v1 || expected !== parts.v1) {
return res.status(401).send('bad signature');
}
const event = JSON.parse(raw);
if (event.test || event.event === 'ping' || seen.has(event.id)) {
return res.json({ ok: true });
}
seen.add(event.id);
res.json({ ok: true }); // answer first ...
setImmediate(() => handle(event)); // ... then do the work
});
function handle(event) {
switch (event.event) {
case 'invoice.created':
// create the invoice in the ERP from event.data
break;
case 'workorder.updated':
// event.changes says what changed, event.data is the work order
break;
default:
break;
}
}
app.listen(8080);
Receiving Webhooks in a Facilio Vibe App
A Facilio Vibe app can be the receiver, so you can build the logic that runs on each event without hosting anything yourself. The app's function can read and write Facilio records, call the other system through its connections, and keep its own data.
In the Vibe app, write a server function handler for the events. It receives the request body as text (
body) and the request headers as JSON text (headers); declare both:import StudioFunctions from "@facilio/studio-functions";
const server = new StudioFunctions({ name: "facilio-events" });
server.addHandler({
name: "onFacilioEvent",
description: "Receives Facilio webhook events",
parameters: {
body: { type: "string", description: "Raw request body" },
headers: { type: "string", description: "Request headers, JSON-encoded" },
},
execute: async ({ body, headers }) => {
const event = JSON.parse(body);
if (event.test || event.event === "ping") {
return { ignored: event.event }; // acknowledge tests
}
// de-duplicate on event.id (for example in the app's database), then act on the event
return { handled: event.id };
},
});Create an incoming webhook for that handler:
facilio vibe webhooks create facilio-events --function <function> --handler onFacilioEvent. The command prints a Preview and a Production URL.Register a Facilio webhook (in Setup or with the API) with the Preview URL, and use Send test to try it. The app's request log (
facilio vibe webhooks logs facilio-events --last) shows exactly what arrived.Publish the Vibe app, then change the Facilio webhook's URL to the Production URL.
How the two fit together:
- If the handler returns normally, the app answers
200and Facilio marks the event Succeeded. If the handler throws an error, the app answers502, and Facilio retries the event on its usual schedule. - The Vibe URL ends in a secret token, so only Facilio (which has the URL) can call it. Treat the URL like a password. To also check
X-Facilio-Signature, the handler needs an HMAC-SHA256 implementation bundled with the function. - Keep the handler quick. For long work, record the event and let a scheduled job of the app finish it.