Skip to content

The open alternative to Salesforce, designed for AI.

Let application workflows start from a webhook (#27933)

Thomas Trompette9e1cf9c05c7b
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-workflow-manifest-to-universal-flat-workflow-or-throw.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-workflow-manifest-to-universal-flat-workflow-or-throw.util.spec.ts
index 80e2dec4185..8e85cfba49c 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-workflow-manifest-to-universal-flat-workflow-or-throw.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-workflow-manifest-to-universal-flat-workflow-or-throw.util.spec.ts
@@ -257,6 +257,63 @@ describe('application workflow definitions', () => {
).toThrow("Workflow trigger: Cron pattern 'every monday' is invalid");
});
+ it('exposes the expected body of a POST webhook as trigger variables', () => {
+ const { version } = convert({
+ ...options,
+ manifest: {
+ ...manifest,
+ version: {
+ ...manifest.version,
+ trigger: {
+ universalIdentifier: TRIGGER_ID,
+ type: 'WEBHOOK',
+ nextStepIds: [STEP_ID],
+ settings: {
+ httpMethod: 'POST',
+ expectedBody: { email: '[email protected]' },
+ },
+ },
+ },
+ },
+ });
+
+ expect(version.triggers?.[0]).toMatchObject({
+ type: 'WEBHOOK',
+ name: 'Webhook',
+ settings: {
+ httpMethod: 'POST',
+ authentication: null,
+ expectedBody: { email: '[email protected]' },
+ expectedOutputSchema: { email: '[email protected]' },
+ outputSchema: { email: { isLeaf: true, type: 'string' } },
+ },
+ });
+ });
+
+ it('converts a GET webhook without authentication or body', () => {
+ const { version } = convert({
+ ...options,
+ manifest: {
+ ...manifest,
+ version: {
+ ...manifest.version,
+ trigger: {
+ universalIdentifier: TRIGGER_ID,
+ type: 'WEBHOOK',
+ nextStepIds: [STEP_ID],
+ settings: { httpMethod: 'GET' },
+ },
+ },
+ },
+ });
+
+ expect(version.triggers?.[0]?.settings).toEqual({
+ httpMethod: 'GET',
+ authentication: null,
+ outputSchema: {},
+ });
+ });
+
it('listens to the database event of the referenced object and fields', () => {
const OBJECT_ID = '99999999-9999-4999-8999-999999999999';
const FIELD_ID = '88888888-8888-4888-8888-888888888888';
@@ -482,7 +539,7 @@ describe('application workflow definitions', () => {
it('rejects an unsupported trigger on the server too', () => {
const invalid = structuredClone(manifest);
- Object.assign(invalid.version.trigger, { type: 'WEBHOOK' });
+ Object.assign(invalid.version.trigger, { type: 'UNKNOWN' });
expect(() =>
convert({
...options,
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-workflow-trigger-manifest-to-trigger-or-throw.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-workflow-trigger-manifest-to-trigger-or-throw.util.ts
index 9b900b28ea8..01e07ed2772 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-workflow-trigger-manifest-to-trigger-or-throw.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-workflow-trigger-manifest-to-trigger-or-throw.util.ts
@@ -1,5 +1,6 @@
import { msg } from '@lingui/core/macro';
import { type WorkflowTriggerManifest } from 'twenty-shared/application';
+import { getOutputSchemaFromValue } from 'twenty-shared/logic-function';
import { FieldMetadataType, RelationType } from 'twenty-shared/types';
import { isDefined, isNonEmptyArray } from 'twenty-shared/utils';
@@ -19,6 +20,7 @@ import {
type WorkflowManualTrigger,
type WorkflowTrigger,
WorkflowTriggerType,
+ type WorkflowWebhookTrigger,
} from 'src/modules/workflow/workflow-trigger/types/workflow-trigger.type';
import { computeCronPatternFromSchedule } from 'src/modules/workflow/workflow-trigger/utils/compute-cron-pattern-from-schedule';
import { computeMorphOrRelationFieldJoinColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-morph-or-relation-field-join-column-name.util';
@@ -41,6 +43,11 @@ type DatabaseEventTriggerManifest = Extract<
{ type: 'DATABASE_EVENT' }
>;
+type WebhookTriggerManifest = Extract<
+ WorkflowTriggerManifest,
+ { type: 'WEBHOOK' }
+>;
+
const DATABASE_EVENT_TRIGGER_NAME_BY_ACTION: Record<
DatabaseEventTriggerManifest['settings']['action'],
string
@@ -196,6 +203,36 @@ const fromDatabaseEventTriggerManifestOrThrow = ({
} satisfies WorkflowDatabaseEventTrigger;
};
+const fromWebhookTriggerManifestSettings = (
+ settings: WebhookTriggerManifest['settings'],
+): WorkflowWebhookTrigger['settings'] => {
+ if (settings.httpMethod === 'GET') {
+ return { httpMethod: 'GET', authentication: null, outputSchema: {} };
+ }
+
+ return {
+ httpMethod: 'POST',
+ authentication: null,
+ expectedBody: settings.expectedBody,
+ expectedOutputSchema: settings.expectedBody,
+ outputSchema: getOutputSchemaFromValue(settings.expectedBody),
+ };
+};
+
+const fromWebhookTriggerManifest = (
+ trigger: WebhookTriggerManifest,
+): WorkflowWebhookTrigger => {
+ const { settings, ...identity } = trigger;
+
+ return {
+ ...identity,
+ name: 'Webhook',
+ type: WorkflowTriggerType.WEBHOOK,
+ position: { x: 0, y: 0 },
+ settings: fromWebhookTriggerManifestSettings(settings),
+ } satisfies WorkflowWebhookTrigger;
+};
+
export const fromWorkflowTriggerManifestToTriggerOrThrow = ({
trigger,
references,
@@ -215,6 +252,8 @@ export const fromWorkflowTriggerManifestToTriggerOrThrow = ({
return fromCronTriggerManifestOrThrow(trigger);
case 'DATABASE_EVENT':
return fromDatabaseEventTriggerManifestOrThrow({ trigger, resolvers });
+ case 'WEBHOOK':
+ return fromWebhookTriggerManifest(trigger);
default:
return assertNever(trigger);
}
diff --git a/packages/twenty-server/src/engine/metadata-modules/metadata-side-effect/handlers/workflow/utils/__tests__/build-workflow-version-side-effects.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/metadata-side-effect/handlers/workflow/utils/__tests__/build-workflow-version-side-effects.util.spec.ts
index aa0a8a9554a..3c3eca42cc6 100644
--- a/packages/twenty-server/src/engine/metadata-modules/metadata-side-effect/handlers/workflow/utils/__tests__/build-workflow-version-side-effects.util.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/metadata-side-effect/handlers/workflow/utils/__tests__/build-workflow-version-side-effects.util.spec.ts
@@ -308,17 +308,55 @@ describe('managed workflow version validation', () => {
expect(validate(version).errors.length).toBeGreaterThan(1);
});
- it('refuses a trigger type applications cannot use', () => {
+ it('refuses an unknown trigger type', () => {
const { version } = convert();
const [trigger] = version.triggers ?? [];
expect(
validate({
...version,
- triggers: [{ ...trigger, type: WorkflowTriggerType.WEBHOOK }],
+ triggers: [
+ { ...trigger, type: 'UNKNOWN' as unknown as WorkflowTriggerType },
+ ],
} as typeof version).errors[0]?.message,
).toContain('Application workflows require one trigger of type MANUAL');
});
+ it('only accepts webhook body variables declared in the expected body', () => {
+ const readingTriggerField = (field: string) =>
+ convert({
+ ...manifest,
+ version: {
+ trigger: {
+ universalIdentifier: TRIGGER_ID,
+ type: 'WEBHOOK',
+ nextStepIds: [STEP_ID],
+ settings: {
+ httpMethod: 'POST',
+ expectedBody: { email: '[email protected]' },
+ },
+ },
+ steps: [
+ {
+ universalIdentifier: STEP_ID,
+ name: 'Forward',
+ type: 'HTTP_REQUEST',
+ input: {
+ url: 'https://example.com/hook',
+ method: 'POST',
+ body: { contact: `{{trigger.${field}}}` },
+ },
+ nextStepIds: [],
+ },
+ ],
+ },
+ }).version;
+
+ expect(validate(readingTriggerField('email')).errors).toEqual([]);
+ expect(validate(readingTriggerField('phone')).errors[0]?.message).toContain(
+ '{{trigger.phone}}',
+ );
+ });
+
it('validates a managed definition but leaves API definitions unchanged', () => {
const definition = convert();
const invalid = { ...definition.version, triggers: null };
diff --git a/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.ts b/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.ts
index 3aa4c4d7ffb..a2bf75eb6a6 100644
--- a/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.ts
+++ b/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.ts
@@ -195,11 +195,11 @@ describe('workflow manifest manual trigger', () => {
it('names the supported trigger types when the type is unknown', () => {
const result = workflowManifestSchema.safeParse(
- withTrigger({ type: 'WEBHOOK' }),
+ withTrigger({ type: 'UNKNOWN' }),
);
expect(result.success).toBe(false);
expect(result.error?.issues[0]?.message).toBe(
- 'Unsupported trigger type. Application workflows support: MANUAL, CRON, DATABASE_EVENT',
+ 'Unsupported trigger type. Application workflows support: MANUAL, CRON, DATABASE_EVENT, WEBHOOK',
);
});
});
@@ -259,6 +259,55 @@ describe('workflow manifest cron trigger', () => {
});
});
+describe('workflow manifest webhook trigger', () => {
+ const withWebhook = (settings: Record<string, unknown>) => ({
+ ...workflow,
+ version: {
+ ...workflow.version,
+ trigger: {
+ universalIdentifier: workflow.version.trigger.universalIdentifier,
+ type: 'WEBHOOK',
+ nextStepIds: workflow.version.trigger.nextStepIds,
+ settings,
+ },
+ },
+ });
+
+ it('accepts a GET webhook and a POST webhook with its expected body', () => {
+ for (const settings of [
+ { httpMethod: 'GET' },
+ {
+ httpMethod: 'POST',
+ expectedBody: { email: '[email protected]', score: 3 },
+ },
+ ]) {
+ expect(
+ workflowManifestSchema.safeParse(withWebhook(settings)).success,
+ ).toBe(true);
+ }
+ });
+
+ it('requires the expected body of a POST webhook', () => {
+ const result = workflowManifestSchema.safeParse(
+ withWebhook({ httpMethod: 'POST' }),
+ );
+ expect(result.success).toBe(false);
+ expect(result.error?.issues[0]?.path).toEqual([
+ 'version',
+ 'trigger',
+ 'settings',
+ 'expectedBody',
+ ]);
+ });
+
+ it('refuses an authentication setting, which webhook triggers do not enforce', () => {
+ const result = workflowManifestSchema.safeParse(
+ withWebhook({ httpMethod: 'GET', authentication: 'API_KEY' }),
+ );
+ expect(result.success).toBe(false);
+ });
+});
+
describe('workflow manifest database event trigger', () => {
const OBJECT_ID = '99999999-9999-4999-8999-999999999999';
const FIELD_ID = '88888888-8888-4888-8888-888888888888';
diff --git a/packages/twenty-shared/src/application/workflowTriggerManifestType.ts b/packages/twenty-shared/src/application/workflowTriggerManifestType.ts
index 8c6d92f9564..6e673297958 100644
--- a/packages/twenty-shared/src/application/workflowTriggerManifestType.ts
+++ b/packages/twenty-shared/src/application/workflowTriggerManifestType.ts
@@ -6,6 +6,7 @@ import { isNonEmptyArray } from '@/utils/array/isNonEmptyArray';
import { isDefined } from '@/utils/validation/isDefined';
import { workflowCronTriggerSchema } from '@/workflow/schemas/cron-trigger-schema';
import { stepFilterGroupSchema } from '@/workflow/schemas/step-filter-group-schema';
+import { workflowWebhookTriggerSchema } from '@/workflow/schemas/webhook-trigger-schema';
const DATABASE_EVENT_ACTIONS_WITH_WATCHED_FIELDS = ['updated', 'upserted'];
@@ -44,6 +45,25 @@ const cronTriggerSettingsManifestSchema = z.discriminatedUnion('type', [
customCronSettingsSchema.omit({ outputSchema: true }).strict(),
]);
+const [getRequestWebhookSettingsSchema, postRequestWebhookSettingsSchema] =
+ workflowWebhookTriggerSchema.shape.settings.options;
+
+const webhookTriggerSettingsManifestSchema = z.discriminatedUnion(
+ 'httpMethod',
+ [
+ getRequestWebhookSettingsSchema
+ .omit({ outputSchema: true, authentication: true })
+ .strict(),
+ postRequestWebhookSettingsSchema
+ .omit({
+ outputSchema: true,
+ expectedOutputSchema: true,
+ authentication: true,
+ })
+ .strict(),
+ ],
+);
+
const workflowTriggerManifestOptions = [
z.strictObject({
universalIdentifier: z.uuid(),
@@ -187,6 +207,12 @@ const workflowTriggerManifestOptions = [
});
}),
}),
+ z.strictObject({
+ universalIdentifier: z.uuid(),
+ type: z.literal('WEBHOOK'),
+ nextStepIds: z.array(z.uuid()).min(1),
+ settings: webhookTriggerSettingsManifestSchema,
+ }),
] as const;
export const APPLICATION_WORKFLOW_TRIGGER_TYPES: readonly string[] =