We have implemented custom hooks for Payload CMS on 20+ projects: from e-commerce stores to corporate portals. Standard validators fail when checking stock via external API, generating unique order numbers, or sending notifications to Telegram.
In one project, we needed to calculate a discount based on purchase history — this required a complex beforeChange hook that queried a separate table. Without a custom hook, we would have had to modify the CMS core, which is unacceptable. Result: the discount is calculated in 200 ms instead of 5 seconds manually.
Custom hooks integrate into the document lifecycle without modifying Payload's source code. Over 5+ years, we've written dozens of such solutions, each requiring deep understanding of collection lifecycle. On average, one hook saves 3–4 hours of manual work per week. Every hook is strictly typed in TypeScript and covered by tests.
How custom hooks solve business logic tasks?
Payload hooks operate at different stages: beforeChange, afterChange, beforeRead, afterRead, beforeDelete, afterDelete. Each hook receives data, req, and context. We use strict TypeScript typing to avoid errors at compile time.
| Hook Type | Task | Example Use Case |
|---|---|---|
beforeChange |
Data transformation | Generating orderNumber, setting createdBy |
afterChange |
Side effects | Sending email, syncing with CRM, cache invalidation |
beforeRead |
Security | Filtering data by user role |
afterRead |
Enrichment | Computing subtotal from items, populating related data |
beforeDelete |
Protection | Preventing deletion of a client with active orders |
What typical errors occur when developing hooks?
An unhandled error in beforeChange blocks saving, while in afterChange it can lead to data loss. We always follow the pattern: in validation hooks — throw new Error, in side-effect hooks — logging + retry. For long-running operations, we queue tasks via Bull or Redis. The beforeRead hook can hide sensitive fields from unauthorized users. For example, a manager sees only their orders, while an admin sees all. This is implemented by filtering on req.user. After afterDelete, we can write a log to a separate collection for auditing.
Example of correct stock check:
const validateStock: CollectionBeforeChangeHook = async ({ data, req }) => { for (const item of data.items) { const { stock } = await externalApi.checkStock(item.product) if (stock < item.quantity) { throw new Error(`Not enough stock for product "${item.name}"`) } } return data } Case study: electronics e-commerce store
We needed to generate order numbers in the format ELEC-XXXXX (without year to avoid staleness), check stock via an external API, and send data to 1C. We implemented three hooks:
- beforeChange — generate number and call warehouse API. If stock insufficient — return error.
- afterChange — send email to customer and create deal in CRM.
- afterChange — enqueue job for 1C sync (via Bull).
All hooks are typed, use CollectionConfig. Errors are logged in Sentry. Result: orders are processed without delays, manual work eliminated, and 1C sync occurs every minute. Custom hooks solve validation tasks 3x faster than Payload's built-in methods.
How to implement custom hooks: step-by-step
- Requirements analysis: describe business logic, identify needed stages (beforeChange, afterChange, etc.).
- Design: design data schema and interaction with external services.
- Development: write hook code with typing and error handling.
- Testing: cover critical scenarios with unit tests.
- Deployment: deploy via CI/CD with automatic checks.
Process overview
| Stage | Duration | Result |
|---|---|---|
| Analysis | 1 day | Hook specification considering business logic |
| Development | 2–3 days | Hook code with unit tests |
| Testing | 1 day | Verification in staging environment |
| Deployment & docs | 0.5 day | Merge to main, description of each hook |
What's included in deliverables
- Source code of hooks with comments (TypeScript).
- Tests for critical scenarios (80%+ coverage).
- Documentation: description of each hook, its purpose, input/output.
- Access to repository with commit history.
- Support for 2 weeks after deployment.
Timelines and how to order
Development time for hooks for one collection is from 1 to 3 days. Cost is calculated individually after requirements analysis. If you need to implement custom hooks in Payload CMS — contact us for project evaluation. Describe your task, and we'll offer an optimal solution. Learn more about hooks in the official Payload documentation. Order custom hooks development for your task — we guarantee transparency and quality.







