> ## Documentation Index
> Fetch the complete documentation index at: https://docs.twenty.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Events (beta)

> Declare your app's own events and emit them from logic functions.

## Overview

An **event** is something your app declares once and emits from a logic function each time it happens.

> [!NOTE]
>
> **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.

```ts src/events/message-added.event.ts theme={null}
import { defineEvent } from 'twenty-sdk/define';

import { CONVERSATION_OBJECT_UNIVERSAL_IDENTIFIER } from 'src/objects/conversation.object';

export const MESSAGE_ADDED_EVENT_UNIVERSAL_IDENTIFIER =
  '3c1d2a4b-5e6f-4a7b-8c9d-0e1f2a3b4c5d';

export default defineEvent({
  universalIdentifier: MESSAGE_ADDED_EVENT_UNIVERSAL_IDENTIFIER,
  name: 'messageAdded',
  label: 'Message added',
  schema: {
    type: 'object',
    properties: {
      conversationId: {
        type: 'record',
        objectUniversalIdentifier: CONVERSATION_OBJECT_UNIVERSAL_IDENTIFIER,
      },
      messageId: { type: 'string', format: 'uuid' },
      text: { type: 'string' },
    },
    required: ['conversationId', 'messageId'],
  },
});
```

`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.

| Property schema | Accepted value |
| - | - |
| `{ type: 'string' }` | string |
| `{ type: 'string', format: 'date-time' }` | ISO 8601 string |
| `{ type: 'string', format: 'uuid' }` | UUID string |
| `{ type: 'number' }` | finite number |
| `{ type: 'integer' }` | integer |
| `{ type: 'boolean' }` | boolean |
| `{ type: 'record', objectUniversalIdentifier }` | UUID of a record of that object (any object, including standard ones) |
| `{}` (no `type`) | any JSON value |

`enum`, `minimum` and `maximum` are checked too. `label` and `description` document a property.

## Emitting

```ts src/logic-functions/handlers/post-message-handler.ts theme={null}
import { emitEvent } from 'twenty-sdk/logic-function';

const { id, occurredAt } = await emitEvent({
  event: 'messageAdded',
  id: `message-added-${messageId}`,
  properties: { conversationId, messageId, text },
});
```

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:

```ts src/logic-functions/on-message-added.ts theme={null}
import { defineLogicFunction, type AppEventPayload } from 'twenty-sdk/define';

import { MESSAGE_ADDED_EVENT_UNIVERSAL_IDENTIFIER } from 'src/events/message-added.event';

const handler = async (occurrence: AppEventPayload) => {
  // occurrence: { id, type, occurredAt, properties }
};

export default defineLogicFunction({
  universalIdentifier: '7a2b3c4d-5e6f-4a7b-8c9d-0e1f2a3b4c5e',
  name: 'on-message-added',
  handler,
  eventTriggerSettings: {
    on: 'appEvent',
    eventUniversalIdentifier: MESSAGE_ADDED_EVENT_UNIVERSAL_IDENTIFIER,
  },
});
```

* 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:

```ts src/agents/reply-assistant.ts theme={null}
import { defineAgent } from 'twenty-sdk/define';

import { MESSAGE_ADDED_EVENT_UNIVERSAL_IDENTIFIER } from 'src/events/message-added.event';

export default defineAgent({
  ...,
  triggers: [
    {
      universalIdentifier: '8b3c4d5e-6f7a-4b8c-9d0e-1f2a3b4c5d6e',
      type: 'EVENT',
      settings: {
        on: 'appEvent',
        eventUniversalIdentifier: MESSAGE_ADDED_EVENT_UNIVERSAL_IDENTIFIER,
      },
      instructions: 'Reply to this message.',
    },
  ],
});
```

* 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

| Limit | Value |
| - | - |
| Properties per event | 50 |
| Occurrence payload | 32 KB |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.