> 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: Build a Backend Events Extension in Studio Two

## Article: Build a Backend Events Extension in Studio Two

## Article Link: https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-backend/backend-events/build-a-backend-events-extension-in-studio-two.md

## Article Content:

# Build a Backend Events Extension in Studio Two

A Backend Events extension is a configuration file plus an event handler. You generate the files first, then spend most of your time in the handler.

This article walks through the handler for the Contact Created event. To add the extension, see [Add a Backend Events Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-backend/backend-events/add-a-backend-events-extension-in-studio-two.md).

Follow the instructions below to:

1. Scaffold the extension.
2. Keep the configuration.
3. Write the handler.

## Step 1 | Scaffold the extension

Start from a generated extension rather than an empty folder, so the file structure and registration are already in place.

For the full procedure, see [Add a Backend Events Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-backend/backend-events/add-a-backend-events-extension-in-studio-two.md).

You can find the new files under `src/extensions/backend/events/<event-folder>/`. You spend most of this article in `<event-folder>.ts`. For what every file in the folder is for, see [Event Extension Files and Code](https://dev.wix.com/docs/backend-extensions/backend-events/event-extension-files-and-code.md).

## Step 2 | Keep the configuration

`<event-folder>.extension.ts` is the configuration file. For an extension generated in the folder `docs-event-check`, it looks like this:

```ts
import { extensions } from '@wix/custom-extensions';

export default extensions.event({
  id: '57adc8fe-1b0b-4a62-80fe-5d8a3b73342a',
  source: './extensions/backend/events/docs-event-check/docs-event-check.ts',
});
```

Leave the `id` that the Builder generated. `source` is the path to the handler, relative to `src/`.

## Step 3 | Write the handler

`<event-folder>.ts` is the code Wix calls when the event occurs. The Contact Created handler that writes a log entry looks like this:

```ts
import { contacts } from '@wix/crm';
import { monitoring } from '@wix/essentials';

export default contacts.onContactCreated((event) => {
  monitoring.getMonitoringClient().captureMessage('Contact created');
});
```

`event` is the event object that Wix passes to the handler. Its fields depend on the event. For the payload of each event, see its page in the [API Reference](https://dev.wix.com/docs/api-reference?apiView=SDK.md).

The handler can run more than once for the same event, so keep it safe to repeat. For example, avoid creating a second record each time it runs without checking whether one exists. For how events are delivered, see [Events](https://dev.wix.com/docs/overview/backend-services/events.md).

To listen for a different event, change the import and the handler method. For example, swap `contacts.onContactCreated` for another event method from the same module or another module. The available methods are in the [API Reference](https://dev.wix.com/docs/api-reference?apiView=SDK.md).

Use `monitoring` from `@wix/essentials` to write log entries. `console` output doesn't appear in your site's logs. See [Write Log Messages in Studio Two](https://dev.wix.com/docs/studio-two/test-your-site/work-with-logs/write-log-messages.md).

When the handler does what you want, click **Publish** in the Studio Two editor to ship the extension with your site.

## See also

- [About Backend Events](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-backend/backend-events/about-backend-events.md)
- [Add a Backend Events Extension in Studio Two](https://dev.wix.com/docs/studio-two/code-your-site/build-a-custom-backend/backend-events/add-a-backend-events-extension-in-studio-two.md)
- [Event Extension Files and Code](https://dev.wix.com/docs/backend-extensions/backend-events/event-extension-files-and-code.md)
- [Events](https://dev.wix.com/docs/overview/backend-services/events.md)