Webhook
Webhook Configuration
To receive webhook events, you need to do the following:
- Subscribe to events with an API path.
- Create the POST API endpoint to receive those events.
If your extension needs to stay in sync with Fynd Commerce, always replace your local copy of any store data with the webhook payload.
You can subscribe to webhook events by sending a webhook_config
configuration to the setupFdk
function from our FDK Extension Library. The following are the allowed configurations for webhooks:
Key | Type | Description |
api_path | String | Webhook POST API endpoint path |
notification_email | String | Email to be notified if any webhook fails. |
subscribe_on_install (optional) | Boolean default true |
|
subscribed_saleschannel (optional) | String ("all" | "specific" )default "all" |
|
event_map | Object / List
| List of events to listen to.
|
Whenever the extension is launched in a company (during installation or after), the webhook configuration passed to setupFdk
is used to subscribe to events for that company. Any updates to the webhook configuration will not automatically reflect on Fynd Commerce until the extension is opened at least once after the update.
Manually subscribing to company and application events
Company
If you set subscribe_on_install: false
, then you can enable or disable webhook event subscriptions for each company it is installed on by calling the function syncEvents
.
await fdkExtension.webhookRegistry.syncEvents(platformClient, config, enable)
Parameters
Parameters | Description |
platformClient | The platform client available in the request object. |
config Object default: null | Webhook subscription config object mentioned earlier. If null then the same config sent to setupFdk is used. |
enable Boolean | true : enable webhooks for the current Company.false : disable webhooks for the current Company. |
Application
If you set subscribed_saleschannel: "specific"
, then you can enable and disable webhook events for a sales channel/application using the functions enableSalesChannelWebhook
and disableSalesChannelWebhook
.
// Enableawait fdkExtension.webhookRegistry.enableSalesChannelWebhook(platformClient, '<application_id>')// Disableawait fdkExtension.webhookRegistry.disableSalesChannelWebhook(platformClient, '<application_id>')
Parameters
Parameters | Description |
platformClient | The platform client available in the request object. |
<application_id> String | The id of the sales channel we're subscribing or unsubscribing to. |
POST API endpoint
Create a POST API endpoint to receive webhook calls. Once the call is received, you should call the webhookRegistry.processWebhook
function with the request. This function validates the event payload with signature and calls the individual handlers from config if the event passes the validation.
Example
1// SUBSCRIBE2let fdkExtension = setupFdk({3api_key: '<API_KEY>',4api_secret: '<API_SECRET>',5callbacks: extensionHandler,6storage: new RedisStorage(redis, 'example_app'),7access_mode: 'offline',8cluster: 'https://api.fynd.com',9webhook_config: {10 api_path: '/api/v1/webhooks',11 notification_email: '[email protected]',12 subscribe_on_install: false,13 subscribed_saleschannel: 'specific',14 event_map: {15 'company/product/create': {16 version: '1',17 handler: handleProductCreateEvent18 },19 'application/coupon/create': {20 version: '1',21 handler: handleCouponCreateEvent22 },23 }24},25});2627// POST API endpoint28app.post('/api/v1/webhooks', async (req, res, next) => {29try {30 await fdkExtension.webhookRegistry.processWebhook(req);31 return res.status(200).json({ success: true });32} catch(err) {33 logger.error(err);34 return res.status(400).json({ success: false });35}36});