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.
Remember

If your extension needs to stay in sync with Fynd Commerce, always replace your local copy of any store data with the webhook payload.


Subscribe to events

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:

KeyTypeDescription
api_pathStringWebhook POST API endpoint path
notification_emailStringEmail to be notified if any webhook fails.
subscribe_on_install
(optional)
Boolean
default true

true: Subscribe to all the company events mentioned in event_map when installed.
false: Do not subscribe to any company events.

subscribed_saleschannel
(optional)
String ("all" | "specific")
default "all"

all: Subscribe to all the application events mentioned in event_map when installed.
specific: Do not subscribe to any application events.

event_map

Object / List

{
  [event-key]: {
    version: String,
    handler: Function
  },
  ...
}

List of events to listen to.

  • event-key: Key of the webhook event.
  • version: Version for that event.
  • handler: Function to be called when that event is triggered.

note

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

ParametersDescription
platformClient

The platform client available in the request object.
eg., const { platformClient } = req;

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.

// Enable
await fdkExtension.webhookRegistry.enableSalesChannelWebhook(platformClient, '<application_id>')
// Disable
await fdkExtension.webhookRegistry.disableSalesChannelWebhook(platformClient, '<application_id>')

Parameters

ParametersDescription
platformClient

The platform client available in the request object.
eg., const { platformClient } = req;

<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// SUBSCRIBE
2let 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: handleProductCreateEvent
18 },
19 'application/coupon/create': {
20 version: '1',
21 handler: handleCouponCreateEvent
22 },
23 }
24},
25});
26
27// POST API endpoint
28app.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});