Skip to main content

Webhook Configuration

To receive the webhook events, you need to do the following:

  • Create an endpoint to receive events that are published to a webhook topic.
  • Specify the endpoint as a webhook subscription's destination.
  • If your extension needs to stay in sync with Fynd Platform, then always replace your local copy of any store data with the webhook payload.

Step 1: How to register for webhook events?

Webhook events can be helpful to handle tasks when certain events occur on the platform. You can subscribe to such events by passing webhook_config in setupFdk function in FDK Extension Library

/app/fdk/index.js
let fdkExtension = setupFdk({
api_key: "<API_KEY>",
api_secret: "<API_SECRET>",
callbacks: extensionHandler,
storage: new RedisStorage(redis, "example_app"),
access_mode: "offline",
cluster: "https://api.fynd.com",
webhook_config: {
api_path: "/api/v1/webhooks", // required
notification_email: "test@abc.com", // required
subscribe_on_install: false, // optional. default `true`
subscribed_saleschannel: 'specific', // optional. default all
event_map: { // required
'company/location/update': {
version: '1',
handler: handleLocationEvent
},
'company/product/create': {
version: '1',
handler: handleProductEvent
},
'application/coupon/create': {
version: '1',
handler: handleCouponCreate
}
}
},
});

By default, all webhook events all subscribed for all companies whenever they are installed. To disable this behavior set subscribe_on_install to false. If subscribe_on_install is set to false, you need to manually enable webhook event subscription by calling syncEvents method of webhookRegistry.

note

You can derive webhook topics to be passed in event_map for required events from event payload dictionary as follows

event_map: { // required
'<category>/<event name>/<type>': {
version: '1',
handler: handleLocationEvent
},
}

Step 2: Create a api endpoint for webhook to listen

There should be view on given API path to receive webhook calls. It should be POST API path. API view should call processWebhook method of webhookRegistry object available under fdkClient here. Here processWebhook will do payload validation with signature and calls individual handlers for event passed with webhook config.

app.post('/api/v1/webhooks', async (req, res, next) => {
try {
await fdkExtension.webhookRegistry.processWebhook(req);
return res.status(200).json({"success": true});
} catch(err) {
logger.error(err);
return res.status(400).json({"success": false});
}
});

Step 3: Configure your webhook settings

Setting subscribed_saleschannel as "specific" means, you will have to manually subscribe to sales channel/website level events for individual sales channels.

The default value here is "all" and the event will be subscribed for all sales channels. For enabling events manually use function enableSalesChannelWebhook. To disable receiving events for a sales channel use the function disableSalesChannelWebhook.

How webhook registry subscribes to webhooks on Fynd Platform?

After webhook config is passed to setupFdk whenever an extension is launched to any of companies where extension is installed or to be installed, webhook config data is used to create webhook subscriber on Fynd Platform for that company. Any update to webhook config will not automatically update subscriber data on Fynd Platform for a company until the extension is opened at least once after the update. Another way to update the webhook config manually for a company is to call the syncEvents function of webhookRegistery.