hide_table_of_contents: false title: Webhook Configuration sidebar_label: Webhook Configuration custom_edit_url: null

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.

Step 1: Subscribe to 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

<CodeBlock title="app/fdk/fdk.js" language="javascript" code={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: "[email protected]", // 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 } } }, });`}>

<CodeBlock title="app/fdk/fdk.js" language="python" code={fdk_extension = setup_fdk({ "api_key": "<api_key>", "api_secret": "<api_secret>", "callbacks": extension_handler, "storage": RedisStorage(redis_connection, "example_app"), "access_mode": "offline", "cluster": "https://api.fynd.com", "webhook_config": { "api_path": "/api/v1/webhooks", # required "notification_email": "[email protected]", # required "subscribe_on_install": False, # optional. default \True` "subscribed_saleschannel": "specific", # optional. default `all` "event_map": { # required 'company/location/update': { "version": '1', "handler": handle_location_event }, "company/product/create": { "version": '1', "handler": handle_product_event }, "application/coupon/create": { "version": '1', "handler": handle_coupon_create } } } })`}>

<CodeBlock title="/src/main/resources/application.yml" language="java" code={ext : api_key : '<api_key>' api_secret : '<api_secret>' scopes: 'company/saleschannel' access_mode : 'offline' cluster: "https://api.fynd.com" webhook: api_path: "/api/v1/webhooks" # required notification_email: "[email protected]" # required subscribe_on_install: false # optional. default \true` subscribed_saleschannel: 'all' event_map: # required - name: 'location/update' handler: handleLocationEvent category: 'company' version: 1 - name: 'product/create' handler: handleProductEvent category: 'company' version: 1 - name: 'coupon/create' handler: handleCouponCreate category: 'application' version: 1 `}>

By default, all webhook events are subscribed to for all companies when they are installed. To disable this behavior, set subscribe_on_install to false, in which case, you will need to manually enable webhook event subscriptions by calling the syncEvents method of the webhookRegistry.

Step 2: Create a POST API endpoint

Create a POST API endpoint to receive webhook calls. Once the call is receieved, you should call the webhookRegistry.processWebhook method available under fdkClient. The processWebhook validates the payload with signature and calls the individual handlers (from config) if the event passes.

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

<CodeBlock language="python" code={`async def webhook_handler(request): try: await fdk_extension.webhook_registry.process_webhook(request) return response.json({"success": True}, 200) except Exception as e: logger.exception(e) return response.json({"success": False}, 400)

register route

app.add_route(webhook_handler, "/api/v1/webhooks", methods=["POST"]) `}>

<CodeBlock language="java" code={`@CrossOrigin(origins = "*") @RestController @RequestMapping() @Slf4j public class WebhookController {

@Autowired
WebhookService webhookService;

private static final Logger log = LoggerFactory.getLogger(SampleEventHandler.class);

@PostMapping(path = "/api/v1/webhooks")
public ResponseEntity<Object> receiveWebhookEvents(HttpServletRequest httpServletRequest) {
    try {
        webhookService.processWebhook(httpServletRequest);
        return new ResponseEntity<>(Collections.singletonMap("success", true), HttpStatus.OK);
    } catch (Exception e) {
        log.error("Exception occurred", e);
        return new ResponseEntity<>(Collections.singletonMap("success", false), HttpStatus.BAD_REQUEST);
    }
}

}`}>

Step 3: Configure your webhook settings

The default value here is "all" and the event will be subscribed for all sales channels. For enabling and disabling events manually for a sales channel, use the functions enableSalesChannelWebhook and disableSalesChannelWebhook.

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

After the webhook configuration is passed to setupFdk, whenever an extension is launched in any company where it is installed or being installed, the configuration is used to create a webhook subscriber on the Fynd Platform for that company. Any updates to the webhook configuration will not automatically reflect in the subscriber data on the Fynd Platform until the extension is opened at least once after the update. Alternatively, you can manually update the webhook configuration for a company by calling the syncEvents function of the webhookRegistry.