Handle Events with Webhooks

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 Wix Dev Center, 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.

Step 1 | Subscribe to a webhook

To subscribe to a webhook:

  1. Open your app in the Wix Dev Center.
  2. In the side menu, click Webhooks.
  3. Click + Create Webhook.
  4. Select an API Category. For example, Stores.
  5. Choose a webhook event from the available options. For example, Cart created.
  6. Enter your server Callback URL. This is where we send the event data.
  7. Add the relevant Permissions.
  8. Click Subscribe.

Your app is set up to receive the webhook.

Step 2 | Save your public key

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:

  1. Open your app in the Dev Center.

  2. In the side menu, click Webhooks.

  3. Click Get Public Key.

You need to use this public key in your handler to verify that the request is from Wix.

Step 3 | Handle the event

Create a server to handle the event that returns a 200 response.

For example, the following code sample shows a Node.js Express server logging the contents of the Cart Created event:

Copy
1
const jwt = require("jsonwebtoken");
2
const express = require("express");
3
const app = express();
4
5
6
const PUBLIC_KEY = `<YOUR_PUBLIC_KEY`;
7
8
9
app.post('/webhook', express.text(), (request, response) => {
10
let event;
11
let eventData;
12
13
14
try {
15
const rawPayload = jwt.verify(request.body, PUBLIC_KEY);
16
event = JSON.parse(rawPayload.data);
17
eventData = JSON.parse(event.data);
18
} catch (err) {
19
console.error(err);
20
response.status(400).send(`Webhook error: ${err.message}`);
21
return;
22
}
23
24
25
switch (event.eventType) {
26
case "wix.ecom.v1.cart_created":
27
console.log(`wix.ecom.v1.cart_created event received with data:`, eventData);
28
break;
29
default:
30
console.log(`Received unknown event type: ${event.eventType}`);
31
break;
32
}
33
34
35
response.status(200).send();
36
37
38
});
39
40
41
app.listen(3000, () => console.log("Server started on port 3000"));

Important: You must return a 200 response upon successful receipt of the webhook.

See also

Was this helpful?
Yes
No