> 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`). > - 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-sdk/code-your-site/build-a-custom-backend/backend-events/define-backend-event-handlers.md ## Article Content: # Define Backend Event Handlers You can define [backend event](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/build-a-custom-backend/backend-events/about-backend-events.md) handlers in the `events.js` backend file to automatically respond to actions that occur on your site. > **Note**: Backend events currently require Velo APIs and file naming conventions. While you can use the JavaScript SDK alongside Velo, backend event handlers must be defined using Velo syntax in the `events.js` backend file. Don't use the event handlers in the JavaScript SDK for backend events when developing Wix sites. ## Step 1 | Add an `events.js` backend file The way that you add an `events.js` file to the backend depends on which development environment 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/174671ebdfa1dffd306ef30c4c4d4af5.png) and then click **Handle backend events**. ![Add events.js](https://wixmp-833713b177cebf373f611808.wixmp.com/images/a1fac75ac6a2e949a10e8098f84d04ea.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. The function name should consist of the Wix module and the name of the event, separated by an underscore. Check the **Events** subcategory 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's 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. } ``` ### Using the SDK in backend event handlers While the event handler function itself must use Velo syntax, you can call SDK functions from in your event handlers: ```javascript import { items } from "@wix/data"; export function wixStores_onOrderPaid(event) { // Use SDK functionality within the Velo event handler return items.insert("orderHistory", { orderId: event.data.id, customerId: event.data.buyerInfo.id, paymentDate: new Date(), amount: event.data.totals.total, }); } ``` This hybrid approach allows you to leverage both the event-driven architecture and modern SDK APIs for data manipulation and business logic. ## Step 3 | Test and debug your function There are 2 ways to [test your backend event handlers](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/build-a-custom-backend/backend-events/about-backend-events.md#testing-and-debugging): - In preview mode, use [functional testing](https://dev.wix.com/docs/develop-websites-sdk/test-your-site/test-backend-functions/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. ## See also - [About backend events](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/build-a-custom-backend/backend-events/about-backend-events.md) - [About JavaScript support](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/code-with-java-script/about-java-script-support.md) - [Module export syntax](https://dev.wix.com/docs/develop-websites-sdk/code-your-site/code-with-java-script/module-export-syntax.md)