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

Step 1 | Subscribe to a webhook

To subscribe to a webhook:

  1. Select an app from the Custom Apps page in your Wix Studio workspace.
  2. In the side menu, click Webhooks.
  3. Click + Create Webhook.
  4. Select an API Category. For example, eCommerce.
  5. Choose a webhook event from the available options. For example, Order Canceled.
  6. Enter your server Callback URL. This is where Wix sends 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. Select your app from the Custom Apps page in your Wix Studio workspace.

  2. In the side menu, click Webhooks.

  3. Click Get Public Key.

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

Step 3 | Handle the event

The following steps demonstrate how to handle a webhook event using the JavaScript SDK:

  1. Set up an Express server to handle incoming webhook requests:

    コピー
    1
  2. Create a WixClient with the AppStrategy. Our client is initialized with the orders module, which allows us to access webhooks related to order events:

    コピー
    1

    Tip: Find your app ID on the OAuth page of your app's dashboard.

  3. を使用する。 onOrderCanceled method to register a callback function to handle order cancellation events:

    コピー
    1
  4. 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:

    コピー
    1

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

Here's the complete code example:

コピー
1

See also

役に立ちましたか?
はい
いいえ