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.mdCopy 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
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
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
Comments
- β¦
More skills from this repository
prd-to-plan
8.1kHelps your agent turn a product requirements document into a phased plan of small end-to-end slices, saved as a Markdown file.
webiny-admin-architect
8.1kTeaches your agent how to build admin screens for the Webiny platform, using its business logic layers and presenter pattern.
webiny-admin-permissions
8.1kTeaches your agent to add permission controls to Webiny admin screens, including auto-generated permission forms and hide-or-show checks.
webiny-form-model
8.1kTeaches your agent to build forms in Webiny using a declarative field builder, layouts, validation and conditional rules.
Similar skills
api-connector-builder
280kHas your agent build new API connections following the existing pattern. Use when you need to add one more integration without creating a new way of doing things.
api-design
280kHelps your agent design REST APIs following best practices. It's useful for defining how APIs communicate, how errors are shown, and how to organize data.
backend-patterns
280kHelps your agent with server-side architecture patterns and database optimization. It's useful for building or reviewing code that runs on the server.