# Creating handlers to receive events with IFX

Source: https://dev.arcxp.com/integrations/manage-integrations/creating-handlers-to-receive-events-with-ifx/

This guide helps developers learn how to build event handlers within an integration to receive both asynchronous (async) and synchronous (sync) events.

This guide helps developers learn how to build event handlers within an integration to receive both asynchronous (async) and synchronous (sync) events.

## Prerequisites

1. Have [created an integration](https://dev.arcxp.com/integrations/manage-integrations/create-and-manage-integrations/)
2. Have your [local environment set up](https://dev.arcxp.com/integrations/manage-integrations/integration-setup/)
3. Download the [starter bundle](https://dev.arcxp.com/integrations/bundles/bundle-management/#download-a-bundle/) or have a bundle locally ready to go

## Handlers, routers, and key files

The `/src` directory is where the action happens — and there are a few files in there worth knowing.

- Directorysrc  
  - DirectoryeventsHandlers  
    - defaultHandler.js
  - eventsRouter.json

`eventsRouter.json`: maps each event to the handler that should process it. The keys are the names of the handler files (without extensions), and the values list the events they handle.

You can call your handlers whatever you please, but a name that hints at what they handle might save future-you some head-scratching.

> **Tip:**
> 
> If multiple handlers are defined for the same event, the first one listed takes priority.

/src/eventsRouter.json

```json
{
  "storyHandler": ["story:create", "story:update"],
  "storyDeleteHandler": ["story:delete"]
}
```

When you run `npm run localTestingServer`, `/src/eventsHandlers.js` is **auto-generated** to include the handler files defined in `eventsRouter.json`. There is no need to modify this file, it’s just to show how it works.

/src/eventsHandlers.js

```js
const storyHandler = require('./eventsHandlers/storyHandler');
const storyDeleteHandler = require('./eventsHandlers/storyDeleteHandler');

module.exports = {
  storyHandler,
  storyDeleteHandler,
}
```

If you prefer to manage this file yourself, you can remove these lines from `package.json`. Just remember that you’ll have to manually maintain it.

```js
"prelocalTestingServer": "node node_modules/@arcxp/arcxp-ifx-node-sdk/eventsHandlersModuleGenerator",
"postinstall": "npm run prelocalTestingServer",
```

## Creating an event handler

Next, in the `/eventsHandler` directory, add the following files:

/src/eventsHandler/storyHandler.js

```js
const storyHandler = (event) => {
  console.log(`my logic in storyHandler`);
  console.log(JSON.stringify(event, null, 2));
}

module.exports = storyHandler;
```

/src/eventsHandler/storyDeleteHandler.js

```js
const storyDeleteHandler = (event) => {
  console.log(`my logic in storyDeleteHandler`);
  console.log(JSON.stringify(event, null, 2));
}

module.exports = storyDeleteHandler;
```

Now if I run `npm run localTestingServer` I see the following with no errors:

Terminal window

```bash
> @myorg/myorg-myintegration@1.0.0 prestart
> node node_modules/@arcxp/arcxp-ifx-node-sdk/eventsHandlersModuleGenerator

The 'eventsHandlers' file content:
const storyDeleteHandler = require('./eventsHandlers/storyDeleteHandler');
const storyHandler = require('./eventsHandlers/storyHandler');

module.exports = {
  storyDeleteHandler,
  storyHandler,
}

> @myorg/myorg-myintegration@1.0.0 start
> node node_modules/@arcxp/arcxp-ifx-node-sdk/localTestingServer

Loading environment configuration for env:  development
Local Express Server Started at http://127.0.0.1:8080/ifx/local/invoke
```

You’ve set up the handlers, routing, and files. Next stop: Locally simulate HTTP requests to watch your integration come alive.

---
  
Have a feature request or suggestion? Let us know on [Our Ideas Portal](https://ideas.arcxp.com/?project=IFX).
