Webhooks
Relevant source files
The following files were used as context for generating this wiki page:
- packages/twenty-server/src/engine/core-modules/webhook/webhook.service.ts
- packages/twenty-front/src/pages/settings/developers/webhooks/components/SettingsWebhooks.tsx
- packages/twenty-server/src/engine/core-modules/webhook/jobs/call-webhook.job.ts
- packages/twenty-server/src/engine/core-modules/webhook/webhook.service.spec.ts
- packages/twenty-server/test/integration/metadata/suites/developers/webhooks.integration-spec.ts
- packages/twenty-front/src/modules/settings/developers/hooks/useWebhookForm.test.tsx
- packages/twenty-front/src/modules/settings/developers/utils/tests/createWebhookInput.test.ts
- packages/e2e-testing/lib/pom/settings/webhooksSection.ts
Webhooks in Twenty provide a mechanism to send real-time data from the CRM to external services. When specific events occur within a workspace (e.g., a new person is created), Twenty can send an HTTP POST request to a configured URL. This allows for building integrations, automating workflows, and synchronizing data with other applications.
The system is designed to be robust, featuring asynchronous execution via a message queue, secure payload delivery with HMAC signatures, and comprehensive management through the developer settings UI. The entire lifecycle, from creation to execution, is handled by distinct backend and frontend components.
Architecture Overview
The webhook system is composed of several key parts: a frontend UI for management, a backend GraphQL API for CRUD operations, a database entity for persistence, a service for business logic, and a message queue job for asynchronous execution.
This diagram shows the high-level flow from a user creating a webhook to an event triggering its execution.
Backend System
The backend is responsible for managing webhook configurations and executing them when triggered.
Data Model (WebhookEntity)
Webhooks are stored in the database using the WebhookEntity. This entity contains all the necessary information to trigger and secure a webhook call.
The following diagram illustrates the WebhookEntity schema.
| Field | Type | Description |
|---|---|---|
id | string | Unique identifier for the webhook. |
targetUrl | string | The URL of the external service to which the POST request will be sent. |
secret | string | A secret key used to generate an HMAC signature for verifying the payload's authenticity. |
operations | string[] | An array of event names (e.g., person.created) that will trigger this webhook. |
workspaceId | string | The ID of the workspace this webhook belongs to. |
createdAt | Date | Timestamp of when the webhook was created. |
updatedAt | Date | Timestamp of the last update. |
deletedAt | Date | Timestamp for soft-deletion. If set, the webhook is considered deleted. |
Source: packages/twenty-server/src/engine/core-modules/webhook/webhook.service.spec.ts:13-24
WebhookService
WebhookService encapsulates the core business logic for webhook management. It handles creation, retrieval, updates, and deletion (CRUD) of webhooks.
Key methods include:
create(data): Creates a new webhook. It validates that thetargetUrlis a valid HTTP/HTTPS URL before saving.update(id, workspaceId, data): Updates an existing webhook. It performs URL validation iftargetUrlis part of the update.delete(id, workspaceId): Soft-deletes a webhook by setting thedeletedAttimestamp.findById(id, workspaceId): Retrieves a single webhook by its ID within a specific workspace.findByWorkspaceId(workspaceId): Retrieves all non-deleted webhooks for a given workspace.findByOperations(workspaceId, operations): Finds all webhooks that are subscribed to one or more of the specified operations. This is the key method for triggering webhooks.
The service throws a WebhookException with code INVALID_TARGET_URL if the URL protocol is not http: or https:.
Sources: packages/twenty-server/src/engine/core-modules/webhook/webhook.service.ts, packages/twenty-server/src/engine/core-modules/webhook/webhook.service.spec.ts:80-92, 238-251
GraphQL API
Webhooks are managed via a GraphQL API. The API exposes mutations for CRUD operations and queries for retrieving webhook data.
The following sequence diagram shows how a user creates a webhook via the frontend UI and GraphQL.
Mutations
| Mutation | Input Type | Description |
|---|---|---|
createWebhook | CreateWebhookInput | Creates a new webhook. |
updateWebhook | UpdateWebhookInput | Updates an existing webhook. |
deleteWebhook | DeleteWebhookInput | Deletes a webhook. |
Source: packages/twenty-server/test/integration/metadata/suites/developers/webhooks.integration-spec.ts
CreateWebhookInput
| Field | Type | Required | Description |
|---|---|---|---|
targetUrl | String | Yes | The destination URL. |
operations | [String!] | Yes | List of events to subscribe to. |
description | String | No | A description for the webhook. |
secret | String | No | The secret for signing payloads. |
Webhook Execution
When a subscribed event occurs in Twenty, the system identifies the relevant webhooks and enqueues a job to notify the target URLs.
CallWebhookJob
Execution is handled asynchronously by CallWebhookJob, a processor for the webhookQueue (BullMQ). This ensures that CRM operations are not blocked by slow external services.
The job performs the following steps:
- Receives an array of
CallWebhookJobDataevents. - For each event, it constructs an HTTP POST request.
- If a
secretis present, it generates a signature and adds security headers. - It sends the request to the
targetUrlwith a 5-second timeout. - It records the outcome (success or failure, and status code) using the
AuditService. - It increments a counter (
JobWebhookCallCompleted) via theMetricsService.
Source: packages/twenty-server/src/engine/core-modules/webhook/jobs/call-webhook.job.ts:39-45, 80-117
Payload and Signature
The payload sent to the target URL is a JSON object containing event details.
| Field | Type | Description |
|---|---|---|
targetUrl | string | The URL the webhook was sent to. |
eventName | string | The name of the event that triggered the webhook (e.g., person.created). |
objectMetadata | object | Contains the id and nameSingular of the affected object. |
workspaceId | string | The ID of the workspace where the event occurred. |
webhookId | string | The ID of the webhook that was triggered. |
eventDate | Date | The timestamp of the event. |
userId | string | (Optional) The ID of the user who initiated the event. |
workspaceMemberId | string | (Optional) The ID of the workspace member who initiated the event. |
record | any | The full data record of the object associated with the event. |
updatedFields | string[] | (Optional) For update events, a list of fields that were changed. |
Source: packages/twenty-server/src/engine/core-modules/webhook/jobs/call-webhook.job.ts:16-29
To ensure security, if a secret is configured for the webhook, the following headers are added to the request:
| Header | Description |
|---|---|
X-Twenty-Webhook-Timestamp | The UNIX timestamp (in milliseconds) of when the request was sent. |
X-Twenty-Webhook-Signature | An HMAC-SHA256 signature. It is generated by hashing the timestamp and the JSON payload with the webhook's secret. |
X-Twenty-Webhook-Nonce | A random 16-byte string to prevent replay attacks. |
The signature is generated using the following logic:
crypto
.createHmac('sha256', secret)
.update(`${timestamp}:${JSON.stringify(payload)}`)
.digest('hex');
Source: packages/twenty-server/src/engine/core-modules/webhook/jobs/call-webhook.job.ts:47-53, 89-97
Frontend Management
The frontend provides a user-friendly interface for managing webhooks within the "Developers" section of the workspace settings.
UI Components
The main view is rendered by SettingsWebhooks.tsx, which displays a list of existing webhooks and a button to "Create Webhook". The UI allows users to:
- View all webhooks.
- Create a new webhook.
- Click on an existing webhook to edit its details.
- Delete a webhook.
The form for creating and editing webhooks includes fields for the target URL, description, secret, and a dynamic list of operations to subscribe to.
Sources: packages/twenty-front/src/pages/settings/developers/webhooks/components/SettingsWebhooks.tsx, packages/e2e-testing/lib/pom/settings/webhooksSection.ts:12-20
Form Logic (useWebhookForm)
The useWebhookForm hook manages the state and logic for the webhook creation/editing form.
- It initializes the form in either
CreateorEditmode. - In
Editmode, it fetches the existing webhook's data using theGET_WEBHOOKGraphQL query. - The
handleSavefunction is responsible for calling either thecreateWebhookorupdateWebhookmutation. - The
handleDeletefunction calls thedeleteWebhookmutation. - It displays success or error notifications using
useSnackBar.
Source: packages/twenty-front/src/modules/settings/developers/hooks/useWebhookForm.test.tsx
Data Transformation
The frontend form represents operations as an array of objects ({ object: string, action: string }) for a better user experience. Before sending this data to the GraphQL API, it is transformed into an array of strings in the format object.action. This transformation also removes duplicates and invalid entries.
// Example form values
const mockFormValues: WebhookFormValues = {
// ...
operations: [
{ object: 'person', action: 'created' },
{ object: 'person', action: 'created' }, // duplicate
{ object: 'company', action: 'updated' },
{ object: null, action: 'test' }, // filtered out
],
// ...
};
// Transformed to: ['person.created', 'company.updated']