Skip to main content

Overview

An event is something your app declares once and emits from a logic function each time it happens.
Beta. Events can be declared, emitted, checked against their definition and used to trigger your app’s logic functions and agents, and any workflow of the workspace. Realtime delivery and storage are planned.

Declaring an event

An event describes its properties with the same schema format as a tool trigger’s inputSchema: an object schema, with type: 'record' and objectUniversalIdentifier for a reference to a record.
src/events/message-added.event.ts
name and property names are camelCase. An event name is unique within your app. Dotted names are reserved for events Twenty itself emits. Properties are flat: nested objects and arrays are refused. enum, minimum and maximum are checked too. label and description document a property.

Emitting

src/logic-functions/handlers/post-message-handler.ts
Name the event with event or with eventUniversalIdentifier: MESSAGE_ADDED_EVENT_UNIVERSAL_IDENTIFIER, not both. Either way, only events your app declares can be emitted. id is optional. Set it whenever the emission may be retried, such as by a retried logic function or after emitEvent throws, since a refused emission may already have reached some receivers. Both emissions then carry the same id and each receiver runs once. It follows the same rules as a background job’s jobId: letters, digits, _, . and -, up to 128 characters. Without it, Twenty generates one. The server checks the properties against the schema and refuses anything undeclared or mistyped. It returns the occurrence id and the time it was recorded.

Triggering a logic function

A logic function of your app runs each time one of your app’s events is emitted when its eventTriggerSettings names the event:
src/logic-functions/on-message-added.ts
  • The function receives the occurrence: { id, type, occurredAt, properties }.
  • Only events your app declares can trigger its functions; syncing a trigger on any other event fails.
  • The run is queued like a background job and follows the function’s retryLimit. It has no member: it acts with the app’s own token, like a cron run.
  • When Twenty cannot queue the runs, for instance once your app hits its job rate limit, emitEvent fails with the reason.

Triggering an agent

An agent of your app runs each time one of your app’s events is emitted when one of its triggers names the event:
src/agents/reply-assistant.ts
  • The run receives the event type, the time it occurred and its properties.
  • Only events your app declares can trigger its agents; syncing a trigger on any other event fails.
  • Each agent’s triggers share its run limit. Once it is reached, the agent skips the occurrence and emitEvent still succeeds.

Triggering a workflow

Every event your app declares shows up in the workflow builder, under Apps in the trigger list, labelled with its label. A workspace member picks it as the trigger of a workflow, and each emitted occurrence starts a run.
  • The steps read the occurrence as {{trigger.id}}, {{trigger.occurredAt}} and {{trigger.properties.<name>}}. The variable picker lists the properties from the event’s schema, with their label.
  • Your app does not know which workflows listen. Put in properties only what any member who builds a workflow may see; prefer record ids to content, so workflows read records through their own permissions.
  • A run that cannot be queued is recorded as a failed run of that workflow, and emitEvent still succeeds.

Limits