Events are triggered when specific conditions on your app or a user’s site are met. Wix Apps can respond to these events using event extensions created in the CLI. Events in the CLI are built on Javascript SDK webhooks, and event extensions subscribe your app to these webhooks behind the scenes.
For more information about events, read About Events.
Follow the instructions below to:
Once this task is complete, your app will have an event extension that is triggered when a specific event occurs on a site.
In the terminal:
Navigate to your project repo.
Run the following command:
The CLI will display a menu of extensions to generate. Select Event and hit enter to continue.
Upon completion, an extension file will be created in your project directory with the following structure:
This file is required and must contain:
The generated event.ts
file will contain example code for an event.
The event.ts
file must be in the following format:
For example, an event.ts
for the Wix CRM onContactCreated() event should look like this:
To work an event your app must have the required permissions for that event. You must configure your app in the app dashboard to request those permissions when installed.
The required permission scopes for an event are listed in the documentation for that event's webhook in the SDK reference. For example, to work with the Wix CRM onContactCreated()
, your app could request the MANAGE MEMBERS AND CONTACTS - ALL PERMISSIONS
permission scope.
To configure permissions requirements for your app:
Important: Currently, local development is not supported for all events in the CLI. If you are unable to test your event in the local development environment as described in this step, you can still see it in action by pushing your changes to production. (See Step 4 | Build and deploy your app.)
To test your event extension, you must:
Inside your event.ts
file, add a console.log()
command to one of your functions. For example:
The process for triggering an event depends on the type of event:
To trigger to the event in the local development environment, you must navigate to the site or its dashboard in your local development environment, then take an action will trigger your event.
From your app's directory in the terminal, run the following command:
Press D
to open a dashboard page of your development site in your browser.
If you can take an action to trigger your event in the dashboard, do so. Otherwise, proceed to the next step.
Navigate to the Home tab in the dashboard sidebar.
Click the three dots and then click View live site as shown in the image below.
Take an action will trigger your event. This action depends on your specific event.
Navigate back to the CLI terminal and you will see the logs from your backend logic in the following form:
Note: To view logs for events in production, use the logs
command.
Once your app is ready for production, you can build it and create a version in app dashboard.
Run the following command to build your app:
Run the following command and follow the prompts to create an app version:
An app version allows you to publish an app to the Wix App Market or install it on a site with a direct install URL.
For more information about building and deploying your app, see Build and Deploy an App with the CLI.
Note: Events are different from other CLI extensions in that once you create a version, the changes you made to the events are automatically reflected in production without publishing the app again.
To delete an event extension from your app, simply delete the subfolder under src/backend/events
that contains your event extension's files.