Webhooks
Webhooks allow extensions to stay in sync with the Fynd Commerce data or perform actions after a specific event occurs in a store. Webhooks are a performant alternative to continuously polling for changes to store data.
Refer to Webhooks for the full list of available webhook events and configuration details.
Register for Webhook Events
Webhook events help you handle tasks when certain events occur on platform. You can subscribe to such events by passing webhook_config
in setupFdk function.
let fdkExtension = setupFdk({
api_key: process.env.EXTENSION_API_KEY,
api_secret: process.env.EXTENSION_API_SECRET,
callbacks: {
auth: async function (data) { return '/' },
uninstall: async function (data) { },
},
storage: new RedisStorage(redis),
access_mode: 'offline',
webhook_config: {
api_path: '/api/webhooks',
notification_email: 'test@abc.com',
subscribe_on_install: false,
subscribed_saleschannel: 'specific',
event_map: {
'company/location/update': {
version: '1',
handler: handleLocationUpdate,
},
'application/coupon/create': {
version: '1',
topic: 'coupon_create_kafka_topic',
provider: 'kafka'
}
}
},
});
A POST API path on the given api_path
to receive webhook call. The API must call the fdkExtension.webhookRegistry.processWebhook()
method with the request object. This method validates the payload with a signature and calls individual handlers for the event passed in the webhook config.
const fdkExtension = setupFdk({ ... });
app.post('/api/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(500).json({"success": false});
}
});
How Webhook Registery Subscribes to Webhooks on Fynd Commerce
After the webhook config is passed to setupFdk
, whenever an extension is launched to a company where an extension is installed or to be installed, webhook config data is used to create a webhook subscriber on the Fynd Commerce for that company.
Any update to the webhook config will not automatically update subscriber data on Fynd Commerce for a company until the extension is opened at least once after the update.