Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Collection Hooks

Collection Hooks are Hooks that run on Documents within a specific Collection. They allow you to execute your own logic during specific events of the Document lifecycle.

To add Hooks to a Collection, use the hooks property in your Collection Config:

1
import type { CollectionConfig } from 'payload';
2
3
export const CollectionWithHooks: CollectionConfig = {
4
// ...
5
hooks: {
6
// ...
7
},
8
}

Config Options

All Collection Hooks accept an array of synchronous or asynchronous functions. Each Collection Hook receives specific arguments based on its own type, and has the ability to modify specific outputs.

1
import type { CollectionConfig } from 'payload';
2
3
export const CollectionWithHooks: CollectionConfig = {
4
// ...
5
hooks: {
6
beforeOperation: [(args) => {...}],
7
beforeValidate: [(args) => {...}],
8
beforeDelete: [(args) => {...}],
9
beforeChange: [(args) => {...}],
10
beforeRead: [(args) => {...}],
11
afterChange: [(args) => {...}],
12
afterRead: [(args) => {...}],
13
afterDelete: [(args) => {...}],
14
afterOperation: [(args) => {...}],
15
afterError: [(args) => {....}],
16
17
// Auth-enabled Hooks
18
beforeLogin: [(args) => {...}],
19
afterLogin: [(args) => {...}],
20
afterLogout: [(args) => {...}],
21
afterRefresh: [(args) => {...}],
22
afterMe: [(args) => {...}],
23
afterForgotPassword: [(args) => {...}],
24
refresh: [(args) => {...}],
25
me: [(args) => {...}],
26
},
27
}

beforeOperation

The beforeOperation hook can be used to modify the arguments that operations accept or execute side-effects that run before an operation begins.

Available Collection operations include create, read, update, delete, login, refresh, and forgotPassword.

1
import type { CollectionBeforeOperationHook } from 'payload'
2
3
const beforeOperationHook: CollectionBeforeOperationHook = async ({
4
args,
5
operation,
6
req,
7
}) => {
8
return args // return modified operation arguments as necessary
9
}

The following arguments are provided to the beforeOperation hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between Hooks. More details.
operationThe name of the operation that this hook is running within.
reqThe Web Request object. This is mocked for Local API operations.

beforeValidate

Runs before the create and update operations. This hook allows you to add or format data before the incoming data is validated server-side.

Please do note that this does not run before the client-side validation. If you added a validate function, this would be the lifecycle:

  1. validate runs on the client
  2. if successful, beforeValidate runs on the server
  3. validate runs on the server
1
import type { CollectionBeforeValidateHook } from 'payload'
2
3
const beforeValidateHook: CollectionBeforeValidateHook = async ({
4
data,
5
}) => {
6
return data
7
}

The following arguments are provided to the beforeValidate hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between Hooks. More details.
dataThe incoming data passed through the operation.
operationThe name of the operation that this hook is running within.
originalDocThe Document before changes are applied.
reqThe Web Request object. This is mocked for Local API operations.

beforeChange

Immediately following validation, beforeChange hooks will run within create and update operations. At this stage, you can be confident that the data that will be saved to the document is valid in accordance to your field validations. You can optionally modify the shape of data to be saved.

1
import type { CollectionBeforeChangeHook } from 'payload'
2
3
const beforeChangeHook: CollectionBeforeChangeHook = async ({
4
data,
5
}) => {
6
return data
7
}

The following arguments are provided to the beforeChange hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.
dataThe incoming data passed through the operation.
operationThe name of the operation that this hook is running within.
originalDocThe Document before changes are applied.
reqThe Web Request object. This is mocked for Local API operations.

afterChange

After a document is created or updated, the afterChange hook runs. This hook is helpful to recalculate statistics such as total sales within a global, syncing user profile changes to a CRM, and more.

1
import type { CollectionAfterChangeHook } from 'payload'
2
3
const afterChangeHook: CollectionAfterChangeHook = async ({
4
doc,
5
}) => {
6
return doc
7
}

The following arguments are provided to the afterChange hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.
docThe resulting Document after changes are applied.
operationThe name of the operation that this hook is running within.
previousDocThe Document before changes were applied.
reqThe Web Request object. This is mocked for Local API operations.

beforeRead

Runs before find and findByID operations are transformed for output by afterRead. This hook fires before hidden fields are removed and before localized fields are flattened into the requested locale. Using this Hook will provide you with all locales and all hidden fields via the doc argument.

1
import type { CollectionBeforeReadHook } from 'payload'
2
3
const beforeReadHook: CollectionBeforeReadHook = async ({
4
doc,
5
}) => {
6
return doc
7
}

The following arguments are provided to the beforeRead hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.
docThe resulting Document after changes are applied.
queryThe Query of the request.
reqThe Web Request object. This is mocked for Local API operations.

afterRead

Runs as the last step before documents are returned. Flattens locales, hides protected fields, and removes fields that users do not have access to.

1
import type { CollectionAfterReadHook } from 'payload'
2
3
const afterReadHook: CollectionAfterReadHook = async ({
4
doc,
5
}) => {
6
return doc
7
}

The following arguments are provided to the afterRead hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.
docThe resulting Document after changes are applied.
queryThe Query of the request.
reqThe Web Request object. This is mocked for Local API operations.

beforeDelete

Runs before the delete operation. Returned values are discarded.

1
import type { CollectionBeforeDeleteHook } from 'payload';
2
3
const beforeDeleteHook: CollectionBeforeDeleteHook = async ({
4
req,
5
id,
6
}) => {...}

The following arguments are provided to the beforeDelete hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.
idThe ID of the Document being deleted.
reqThe Web Request object. This is mocked for Local API operations.

afterDelete

Runs immediately after the delete operation removes records from the database. Returned values are discarded.

1
import type { CollectionAfterDeleteHook } from 'payload';
2
3
const afterDeleteHook: CollectionAfterDeleteHook = async ({
4
req,
5
id,
6
doc,
7
}) => {...}

The following arguments are provided to the afterDelete hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.
docThe resulting Document after changes are applied.
idThe ID of the Document that was deleted.
reqThe Web Request object. This is mocked for Local API operations.

afterOperation

The afterOperation hook can be used to modify the result of operations or execute side-effects that run after an operation has completed.

Available Collection operations include create, find, findByID, update, updateByID, delete, deleteByID, login, refresh, and forgotPassword.

1
import type { CollectionAfterOperationHook } from 'payload'
2
3
const afterOperationHook: CollectionAfterOperationHook = async ({
4
result,
5
}) => {
6
return result
7
}

The following arguments are provided to the afterOperation hook:

OptionDescription
argsThe arguments passed into the operation.
collectionThe Collection in which this Hook is running against.
reqThe Web Request object. This is mocked for Local API operations.
operationThe name of the operation that this hook is running within.
resultThe result of the operation, before modifications.

afterError

The afterError Hook is triggered when an error occurs in the Payload application. This can be useful for logging errors to a third-party service, sending an email to the development team, logging the error to Sentry or DataDog, etc. The output can be used to transform the result object / status code.

1
import type { CollectionAfterErrorHook } from 'payload';
2
3
const afterDeleteHook: CollectionAfterErrorHook = async ({
4
req,
5
id,
6
doc,
7
}) => {...}

The following arguments are provided to the afterError Hook:

ArgumentDescription
errorThe error that occurred.
contextCustom context passed between Hooks. More details.
graphqlResultThe GraphQL result object, available if the hook is executed within a GraphQL context.
reqThe PayloadRequest object that extends Web Request. Contains currently authenticated user and the Local API instance payload.
collectionThe Collection in which this Hook is running against.
resultThe formatted error result object, available if the hook is executed from a REST context.

beforeLogin

For Auth-enabled Collections, this hook runs during login operations where a user with the provided credentials exist, but before a token is generated and added to the response. You can optionally modify the user that is returned, or throw an error in order to deny the login operation.

1
import type { CollectionBeforeLoginHook } from 'payload'
2
3
const beforeLoginHook: CollectionBeforeLoginHook = async ({
4
user,
5
}) => {
6
return user
7
}

The following arguments are provided to the beforeLogin hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.
reqThe Web Request object. This is mocked for Local API operations.
userThe user being logged in.

afterLogin

For Auth-enabled Collections, this hook runs after successful login operations. You can optionally modify the user that is returned.

1
import type { CollectionAfterLoginHook } from 'payload';
2
3
const afterLoginHook: CollectionAfterLoginHook = async ({
4
user,
5
token,
6
}) => {...}

The following arguments are provided to the afterLogin hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.
reqThe Web Request object. This is mocked for Local API operations.
tokenThe token generated for the user.
userThe user being logged in.

afterLogout

For Auth-enabled Collections, this hook runs after logout operations.

1
import type { CollectionAfterLogoutHook } from 'payload';
2
3
const afterLogoutHook: CollectionAfterLogoutHook = async ({
4
req,
5
}) => {...}

The following arguments are provided to the afterLogout hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.
reqThe Web Request object. This is mocked for Local API operations.

afterMe

For Auth-enabled Collections, this hook runs after me operations.

1
import type { CollectionAfterMeHook } from 'payload';
2
3
const afterMeHook: CollectionAfterMeHook = async ({
4
req,
5
response,
6
}) => {...}

The following arguments are provided to the afterMe hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.
reqThe Web Request object. This is mocked for Local API operations.
responseThe response to return.

afterRefresh

For Auth-enabled Collections, this hook runs after refresh operations.

1
import type { CollectionAfterRefreshHook } from 'payload';
2
3
const afterRefreshHook: CollectionAfterRefreshHook = async ({
4
token,
5
}) => {...}

The following arguments are provided to the afterRefresh hook:

OptionDescription
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.
expThe expiration time of the token.
reqThe Web Request object. This is mocked for Local API operations.
tokenThe newly refreshed user token.

afterForgotPassword

For Auth-enabled Collections, this hook runs after successful forgotPassword operations. Returned values are discarded.

1
import type { CollectionAfterForgotPasswordHook } from 'payload'
2
3
const afterForgotPasswordHook: CollectionAfterForgotPasswordHook = async ({
4
args,
5
context,
6
collection,
7
}) => {...}

The following arguments are provided to the afterForgotPassword hook:

OptionDescription
argsThe arguments passed into the operation.
collectionThe Collection in which this Hook is running against.
contextCustom context passed between hooks. More details.

refresh

For Auth-enabled Collections, this hook allows you to optionally replace the default behavior of the refresh operation with your own. If you optionally return a value from your hook, the operation will not perform its own logic and continue.

1
import type { CollectionRefreshHook } from 'payload'
2
3
const myRefreshHook: CollectionRefreshHook = async ({
4
args,
5
user,
6
}) => {...}

The following arguments are provided to the afterRefresh hook:

OptionDescription
argsThe arguments passed into the operation.
userThe user being logged in.

me

For Auth-enabled Collections, this hook allows you to optionally replace the default behavior of the me operation with your own. If you optionally return a value from your hook, the operation will not perform its own logic and continue.

1
import type { CollectionMeHook } from 'payload'
2
3
const meHook: CollectionMeHook = async ({
4
args,
5
user,
6
}) => {...}

The following arguments are provided to the me hook:

OptionDescription
argsThe arguments passed into the operation.
userThe user being logged in.

TypeScript

Payload exports a type for each Collection hook which can be accessed as follows:

1
import type {
2
CollectionBeforeOperationHook,
3
CollectionBeforeValidateHook,
4
CollectionBeforeChangeHook,
5
CollectionAfterChangeHook,
6
CollectionAfterReadHook,
7
CollectionBeforeReadHook,
8
CollectionBeforeDeleteHook,
9
CollectionAfterDeleteHook,
10
CollectionBeforeLoginHook,
11
CollectionAfterLoginHook,
12
CollectionAfterLogoutHook,
13
CollectionAfterRefreshHook,
14
CollectionAfterMeHook,
15
CollectionAfterForgotPasswordHook,
16
CollectionRefreshHook,
17
CollectionMeHook,
18
} from 'payload'
Next

Global Hooks