# How to programmatically trigger flows

**URL:** <https://community.directus.com/t/how-to-programmatically-trigger-flows/1082>\
**Category:** Help\
**Tags:** custom-extensions, extensions\
**Created:** [August 30, 2025, 7:20pm UTC](https://community.directus.com/t/how-to-programmatically-trigger-flows/1082 "2025-08-30T19:20:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![remihuigen](https://sea2.discourse-cdn.com/flex002/user_avatar/community.directus.com/remihuigen/32/726_2.png) [@remihuigen](https://community.directus.com/u/remihuigen)\
**Post date:** [August 30, 2025, 7:20pm UTC](https://community.directus.com/t/how-to-programmatically-trigger-flows/1082/1 "2025-08-30T19:20:26Z")

</div>

Hi there!

I need to trigger a flow from a hook extension. I know there is a FlowsService, but this does not contain a trigger method.

I now there is a `POST /flows/trigger/<id>`endpoint, but I’d much rather use an internal service to prevent http request.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![Nik](https://avatars.discourse-cdn.com/v4/letter/n/6de8d8/32.png) [@Nik](https://community.directus.com/u/Nik)\
**Post date:** [August 31, 2025, 4:05pm UTC](https://community.directus.com/t/how-to-programmatically-trigger-flows/1082/3 "2025-08-31T16:05:30Z")

</div>

I could successfully trigger a webhook flow (without web request) using dynamically imported [FlowManager](https://github.com/directus/directus/blob/f55913653b02caa7cf8fbd90a0c2b48108419719/api/src/flows.ts#L63). I guess you should be able to run other flow trigger types as well but I didn’t test that.

First of all make sure to change the module config inside your tsconfig.json of your extension to make dynamic imports possible.

```json
	"compilerOptions": {
		"module": "node16",
		"moduleResolution": "node16",

```

In case you are running Directus Docker Compose right inside your extension dev folder, make sure that you don’t expose the dev directory for convenience reasons like I am used to do it, because it leaks the dev dependencies to the directus container and messes up with module resolutions:

```auto
volumes:
     - .:/directus/extensions/my-custom-extension // <= remove that in case you set it up this way

```

Then you should be able to import all that good stuff that’s not exposed by the Extension SDK at runtime.

```ts
import { defineHook } from '@directus/extensions-sdk';

export default defineHook(({ filter, action }) => {
	action('server.start', async (_meta, ctx) => {
		console.log('[flow-manager] server.start');
		console.log('[flow-manager] incoming accountability:', ctx?.accountability ?? null);
		try {
			// Build import path at runtime so bundler won't include @directus/api
			const base = '@directus' + '/api';

            // Get the FlowManager
			const { getFlowManager } = await import(`${base}/flows`);
			const { default: getDatabase } = await import(`${base}/database/index`);
			const { getSchema } = await import(`${base}/utils/get-schema`);
			const flowManager = getFlowManager();
			await flowManager?.initialize?.();
			console.log('[flow-manager] FlowManager initialized');

			// Build context: use provided accountability or fallback to internal admin
            // But since server.start hook does not have accountability it will be admin
			const accountability = (ctx as any)?.accountability ?? { admin: true, app: 'server' };
			const database = (ctx as any)?.database ?? getDatabase();
			const schema = (ctx as any)?.schema ?? (await getSchema({ database }));
			const flowCtx = {
				accountability,
				database,
				schema,
			} as any;

			// Change this to the friendly name of you flow
			const flowName = 'my-webhook-flow';

            // Resolve flow by friendly name and run it
			const { FlowsService } = await import(`${base}/services/flows`);
			const flowsService = new FlowsService({ knex: database, schema });
			const flows = await flowsService.readByQuery({
				filter: { name: { _eq: flowName }, status: { _eq: 'active' } },
				fields: ['id', 'trigger', 'options'],
				limit: 1,
			});
			const flow = flows?.[0];
			if (!flow) {
				console.warn(`[flow-manager] Flow not found or inactive: ${flowName}`);
				return;
			}
			if (flow.trigger === 'operation') {
				const res = await flowManager.runOperationFlow(flow.id, {}, flowCtx);
				console.log(`[flow-manager] Ran operation flow ${flowName} (${flow.id})`, res);
			} else if (flow.trigger === 'webhook') {
				const method = (flow.options?.method || 'GET').toUpperCase();
				const key = `${method}-${flow.id}`;
				const res = await flowManager.runWebhookFlow(key, {}, flowCtx);
				console.log(`[flow-manager] Ran webhook flow ${flowName} (${key})`, res);
			} else if (flow.trigger === 'manual') {
				const key = `POST-${flow.id}`;
				const res = await flowManager.runWebhookFlow(key, {}, flowCtx);
				console.log(`[flow-manager] Ran manual flow ${flowName} (${key})`, res);
			} else {
				console.warn(`[flow-manager] Flow trigger not supported in startup: ${flow.trigger}`);
			}
	
	
		} catch (err) {
			console.error('Something went wrong executing a flow from the server.start hook:', err);
		}
	});
});

```

---

<div class="post-metadata">

**Author:** ![ahmad\_quvor](https://sea2.discourse-cdn.com/flex002/user_avatar/community.directus.com/ahmad_quvor/32/310_2.png) [@ahmad\_quvor](https://community.directus.com/u/ahmad_quvor)\
**Post date:** [August 30, 2025, 7:42pm UTC](https://community.directus.com/t/how-to-programmatically-trigger-flows/1082/2 "2025-08-30T19:42:17Z")

</div>

Hello @remihuigen

Did you try this?

 ![IMG_20250831_011050](https://us1.discourse-cdn.com/flex002/uploads/directus/original/1X/6e0bed02bec2a9322aeb6957d054b20b4961e118.jpeg)
