> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

## Resource: Define Backend Event Handlers

## Article: Define Backend Event Handlers

## Article Link: https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/backend-code/events/define-backend-event-handlers.md

## Article Content:

# Define Backend Event Handlers

You can define [backend event](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/backend-code/events/about-backend-events.md) handlers in the `events.js` backend file.

> **Note**: When extending websites, you can only handle events using Velo. Handling site events using the JavaScript SDK [isn't currently supported](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/develop-with-the-sdk/velo-to-sdk-api-mapping.md#velo-apis-you-should-continue-to-use).

## Step 1 | Add an `events.js` backend file 

The way that you add an `events.js` file to the backend depends on which IDE you're using.

### Editor

1. Hover over **Backend** in the **Public & Backend** section of the code sidebar.
2. Click the **More Actions** icon  ![plus icon](https://wixmp-833713b177cebf373f611808.wixmp.com/images/09238277769fcf676394bfa42b62b30e.png) and then click **Handle backend events**.

![Add events.js](https://wixmp-833713b177cebf373f611808.wixmp.com/images/00e770f633344a1acd766330aa389d44.png)

### Wix IDE or your local IDE

Add an `events.js` file to the `src/backend` folder.

## Step 2 | Define a backend event handler

To define an event handler in the `events.js` file, export a function using a [supported export syntax](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/java-script-velo/module-export-syntax.md). 
The function name should consist of the Wix module and the name of the event, separated by an underscore. Check the **Events** subcategory in most APIs in the [API documentation](https://dev.wix.com/docs/velo.md) to confirm the exact function naming and to see what information is passed to the event handler when it is called. 

Here is an example of an event handler that [triggers when an invoice is paid](https://dev.wix.com/docs/velo/apis/wix-billing-backend/events/on-invoice-paid.md):

```javascript
export function wixBilling_onInvoicePaid(event) {
  const invoiceId = event.id.id;
  const email = event.customer.email;
  // Add your own logic here.
}
```

## Step 3 | Test and debug your function

There are two ways to [test your backend event handlers](https://dev.wix.com/docs/develop-websites/articles/coding-with-velo/backend-code/events/about-backend-events.md#testing-and-debugging): 

+ In preview mode, use [functional testing](https://dev.wix.com/docs/develop-websites/articles/workspace-tools/testing-monitoring/functional-testing/test-backend-functions-with-functional-testing.md) to call your event handler function manually.
+ Publish your site, then trigger the event that activates your event handler.

Watch a demo of how to set up backend event handlers:

`youtube:https://youtu.be/-LAtdo0mbiY?t=162`