Documentation Index
Fetch the complete documentation index at: https://mintlify.com/directus/directus/llms.txt
Use this file to discover all available pages before exploring further.
Hooks allow you to execute custom code in response to Directus events.
Creating a Hook
npm init directus-extension
# Select "hook" as the extension type
Hook Structure
import { defineHook } from '@directus/extensions-sdk';
export default defineHook(({ filter, action }, { services, getSchema }) => {
// Filter hook - modify data before it's processed
filter('items.create', async (input, meta, context) => {
if (meta.collection === 'articles') {
input.slug = input.title.toLowerCase().replace(/\s+/g, '-');
}
return input;
});
// Action hook - execute after an action completes
action('items.create', async (meta, context) => {
if (meta.collection === 'articles') {
console.log('Article created:', meta.key);
}
});
});
Event Types
Item Events
items.create
items.update
items.delete
items.promote
items.sort
Authentication Events
auth.login
auth.create
auth.update
Server Events
Request Events
Filter Hooks
Modify data before it’s processed:
filter('items.create', (input, meta) => {
// Modify input
input.created_at = new Date();
return input;
});
Action Hooks
Execute code after an action:
action('items.create', async (meta, context) => {
const { ItemsService } = context.services;
const schema = await context.getSchema();
const itemsService = new ItemsService('logs', { schema });
await itemsService.createOne({
action: 'create',
collection: meta.collection,
item: meta.key,
});
});
Context
Hooks receive context with:
interface Context {
services: Services;
getSchema: () => Promise<Schema>;
database: Knex;
accountability: Accountability | null;
}
Next Steps
Operations
Create flow operations
Endpoints
Create API endpoints