Skip to content
Skillpedia
APIs & servers (backend)8.1k Β· #12586

webiny-event-handler-pattern

What it does

This skill guides your AI when writing code that runs when something changes in Webiny, such as an entry being created. It is for developers who want to add checks or side effects at those moments.

When to use it

  • βœ“When you want code to run before an entry or other record is saved, to check or change it.
  • βœ“When you want a side effect to happen after a record is created, updated or deleted.
  • βœ“When you need to define and publish your own domain event.

Skill link

https://github.com/webiny/webiny-js/blob/next/skills/user-skills/api/event-handler-pattern/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/webiny/webiny-js/blob/next/skills/user-skills/api/event-handler-pattern/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute β†’

Example of what to ask your AI

β€œAfter a CMS entry is created, send its title to our search index using an event handler.”

5 new skills a week. 1 minute to read.

How it works

The skill explains an event handler with a single handle method, kept thin so it calls a reusable service. It covers the naming pattern for before and after handlers and how events are published.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

EventHandler Pattern

What It Is

An EventHandler reacts to domain events in the Webiny lifecycle (e.g., EntryBeforeCreateEventHandler, TenantAfterDeleteEventHandler). Each handler is a DI abstraction with a single handle method.

Naming Convention

  • {Entity}Before{Operation}EventHandler β€” fires before persistence, can validate/transform/reject
  • {Entity}After{Operation}EventHandler β€” fires after persistence, for side effects

Interface Shape

interface SomeEventHandler.Interface {
    handle(event: SomeEventHandler.Event): Promise<void>;
}

The Event is a DomainEvent<Payload> where the payload contains the entity and input data.

Architecture Rule: Always Wrap Logic in a Reusable Abstraction (MANDATORY)

Never put business logic directly inside an EventHandler. EventHandlers are thin orchestrators β€” they receive an event and delegate to an injected service or use case. The real logic lives in a dedicated abstraction.

Why: Inline handler logic cannot be reused by other handlers, GraphQL resolvers, or CLI commands.

Always follow this structure:

features/
β”œβ”€β”€ myService/             ← the reusable abstraction
β”‚   β”œβ”€β”€ abstractions.ts
β”‚   β”œβ”€β”€ feature.ts
β”‚   └── MyService.ts
└── syncOnCreate/          ← thin handler that injects the service
    β”œβ”€β”€ feature.ts
    └── EntryAfterCreateHandler.ts

The EventHandler feature and the service feature are registered separately in Extension.ts.

How to Implement

import { SomeEventHandler } from "webiny/api/<category>";
import { MyService } from "../myService/abstractions.js";

// βœ… Handler is a thin orchestrator β€” no business logic here
class MyHandler implements SomeEventHandler.Interface {
  constructor(private myService: MyService.Interface) {}

  async handle(event: SomeEventHandler.Event) {
    const { entity, model } = event.payload;

    // For CMS handlers: ALWAYS filter by model
    if (model.modelId !== "myModel") return;

    await this.myService.doWork(entity);
  }
}

export default SomeEventHandler.createImplementation({
  implementation: MyHandler,
  dependencies: [MyService]
});

See webiny-api-architect for how to define MyService as a proper abstraction.

Injecting Dependencies

EventHandlers can depend on UseCases, platform services, or custom abstractions:

import { SomeEventHandler } from "webiny/api/<category>";
import { SomeUseCase } from "webiny/api/<category>";

class MyHandler implements SomeEventHandler.Interface {
  constructor(private someUseCase: SomeUseCase.Interface) {}

  async handle(event: SomeEventHandler.Event) {
    const result = await this.someUseCase.execute({/* ... */});
  }
}

export default SomeEventHandler.createImplementation({
  implementation: MyHandler,
  dependencies: [SomeUseCase]
});

Defining Your Own Domain Events

When your feature needs to notify other parts of the system about important domain actions, define your own events.

Event Payload Types (in abstractions.ts)

Event payloads and handler abstractions live in abstractions.ts. The events.ts file only contains the event classes.

// features/disableEntity/abstractions.ts
import { createAbstraction, type DomainEvent } from "webiny/api";
import type { Entity } from "~/shared/Entity.js";
// Forward declaration β€” actual classes are in events.ts
import type { EntityBeforeDisableEvent, EntityAfterDisableEvent } from "./events.js";

// Event Payload Types
export interface EntityBeforeDisablePayload {
  entity: Entity;
}

export interface EntityAfterDisablePayload {
  entity: Entity;
}

// Handler Abstractions β€” one per event
interface IEventHandler<TEvent extends DomainEvent<any>> {
  handle(event: TEvent): Promise<void>;
}

export const EntityBeforeDisableEventHandler = createAbstraction<
  IEventHandler<EntityBeforeDisableEvent>
>("MyPackage/EntityBeforeDisableEventHandler");

export namespace EntityBeforeDisableEventHandler {
  export type Interface = IEventHandler<EntityBeforeDisableEvent>;
  export type Event = EntityBeforeDisableEvent;
}

export const EntityAfterDisableEventHandler = createAbstraction<
  IEventHandler<EntityAfterDisableEvent>
>("MyPackage/EntityAfterDisableEventHandler");

export namespace EntityAfterDisableEventHandler {
  export type Interface = IEventHandler<EntityAfterDisableEvent>;
  export type Event = EntityAfterDisableEvent;
}

Event Class Definition (events.ts)

Event classes import payload types and handler abst

FAQ

How should a handler be named?

Use the entity name, then Before or After, then the operation, then EventHandler.

Where should the logic go?

In a reusable service or use case, with the handler only calling it.

Can a handler stop a change?

A before handler can validate, transform or reject, according to the skill.

Details

Original description by the author

EventHandler implementation pattern β€” handle method, event payloads, filtering, DI, domain event definition, publishing events from UseCases, and reacting to external events. Use this skill to implement any Webiny EventHandler (before/after hooks) or to define and publish your own domain events.

repo: webiny/webiny-js

Popularity

GitHub stars
8.1k
Ranking position
#12586
License
β€”
Updated
11/2/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills