// This example uses Express to receive webhooks
const app = require('express')();
// Use body-parser to retrieve the raw body as a buffer
const bodyParser = require('body-parser');
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
const event = request.body;
// Add idempotency using the ORM being used by your app.
// Handle the event
switch (event.type) {
case 'link.credentials_changed':
const linkId = event.data.id;
// Then define and call a method to handle the ceredentilas changed event.
break;
case 'link.refresh_intent.succeeded':
const linkId = event.data.id;
// Then define and call a method to handle the link refreshed event.
break;
case 'account.refresh_intent.succeeded':
const accountId = event.data.id;
// Then define and call a method to handle the account refreshed event.
break;
// ... handle other event types
default:
// Unexpected event type
console.log(`Unhandled event type ${event.type}`);
}
// Return a response to acknowledge receipt of the event
response.json({received: true});
});
app.listen(8000, () => console.log('Running on port 8000'));