To handle events with webhooks, you need to subscribe to the webhook and create a webhook handler. When you subscribe to a webhook in the app dashboard, you specify the endpoint where Wix should send webhook data. Then, when the event occurs, Wix sends a POST request to your handler containing event data and proceeds based on your response.
This article outlines the process of subscribing to and processing the order canceled webhook from Wix eCommerce.
To subscribe to a webhook:
Your app is set up to receive the webhook.
Webhook payloads are sent in JSON web token (JWT) format. Your public key allows you to verify the signature in any webhook you receive.
To get your public key:
Select your app from the Custom Apps page in your Wix Studio workspace.
In the side menu, click Webhooks.
Click Get Public Key.
Save the public key in a secure location.
You need to use this public key in your handler to verify that the request is from Wix.
The following steps demonstrate how to handle a webhook event using the JavaScript SDK:
Set up an Express server to handle incoming webhook requests:
Create a WixClient
with the AppStrategy
. Our client is initialized with the orders
module, which allows us to access webhooks related to order events:
Tip: Find your app ID on the OAuth page of your app's dashboard.
Use the onOrderCanceled
method to register a callback function to handle order cancellation events:
Define a POST
endpoint /webhook
on your Express server to receive incoming webhook payloads. Within the endpoint, process the incoming webhook payload using the process
method provided by the SDK:
Important: You must return a 200 response upon successful receipt of the webhook.
Here's the complete code example: