Hook Types
Feathers supports four types of hooks:- around - Wraps around a method call, with full control via
next() - before - Runs before a service method
- after - Runs after a successful service method call
- error - Runs when an error occurs in a service method
Hook Context
All hooks receive acontext object with information about the service call:
Context Properties
Application
The Feathers application object
Service
The service this hook currently runs on
string
The service path without leading or trailing slashes
string
The name of the service method (e.g.,
'find', 'create')'before' | 'after' | 'error' | 'around'
The hook type
any[]
The list of method arguments. Should not be modified directly.
Params
Service method parameters (including
params.query). Can be modified.Id
The id for
get, update, patch, and remove calls. Can be null for multi-operations.any
The data for
create, update, and patch calls. Can be modified.any
The result of the service method call. Available in
after hooks. Setting result in a before hook skips the actual method call.any
A ‘safe’ version of data to send to clients. If not set,
result is sent instead.Error
The error object thrown. Only available in
error hooks.string | null
The event to emit. Set to
null to skip event emitting.number
HTTP status code override (deprecated, use
http.status instead)Http
HTTP-specific options including status code, headers, and location
Registering Hooks
Service Hooks
Register hooks on a specific service using the.hooks() method:
Application Hooks
Register hooks that run for all services:Setup/Teardown Hooks
Register hooks for application lifecycle:Hook Functions
Before/After/Error Hooks
Regular hooks receive the context and can modify it:Around Hooks
Around hooks wrap the method call and must callnext() to continue:
Common Hook Patterns
Validation
Authentication & Authorization
Data Transformation
Query Modification
Logging
Error Handling
Caching
Pagination
Skip Method Call
Settingcontext.result in a before hook skips the actual service method:
Hook Registration Formats
Hooks can be registered in multiple formats:Object Format
Array Format (Around Hooks)
Method-Specific Format
Helper Functions
createContext()
Create a hook context programmatically:Service
required
The service instance
string
required
The method name
object
Initial context data
HookContext
The created hook context