Skip to main content
PocketBase provides a comprehensive event system that allows you to execute custom logic at specific points in the application lifecycle.

Event types

PocketBase events are categorized into several groups:
  • App events - Application lifecycle events
  • Model events - Generic database model events
  • Record events - Specific to record operations
  • Collection events - Collection schema changes
  • Request events - HTTP API request events

App events

OnBootstrap

Triggered when initializing app resources:

OnServe

Triggered when the web server starts:

OnTerminate

Triggered when the app is being terminated:

OnBackupCreate

Triggered when creating a backup:

OnBackupRestore

Triggered before restoring a backup:

Record events

OnRecordCreate

Triggered when creating a new record:

OnRecordUpdate

Triggered when updating a record:

OnRecordDelete

Triggered when deleting a record:

OnRecordValidate

Triggered during record validation:

Event execution phases

Record events have multiple execution phases:

Before execution

OnRecordCreate, OnRecordUpdate, OnRecordDelete run BEFORE validation and database operations:

Execute phase

OnRecordCreateExecute, OnRecordUpdateExecute, OnRecordDeleteExecute run right before the database operation:

After success

OnRecordAfterCreateSuccess, OnRecordAfterUpdateSuccess, OnRecordAfterDeleteSuccess run after successful database persistence:
In transactions, “After Success” hooks are delayed until the transaction commits.

After error

OnRecordAfterCreateError, OnRecordAfterUpdateError, OnRecordAfterDeleteError run after failed operations:

Collection events

OnCollectionCreate

Triggered when creating a collection:

OnCollectionUpdate

Triggered when updating a collection:

OnCollectionDelete

Triggered when deleting a collection:

Request events

Custom routes

Add custom HTTP endpoints:

OnRecordsListRequest

Triggered on listing records via API:

OnRecordViewRequest

Triggered when viewing a single record:

Hook binding

BindFunc()

Bind a function to an event:

Bind()

Bind a handler with custom ID and priority:

Unbind()

Remove a specific handler:

Event priorities

Control execution order with priorities:
Default priority is 0. Lower numbers run earlier, higher numbers run later.

Event propagation

e.Next()

Continue to the next handler or core logic:

Stopping propagation

Return an error to stop execution:

Auth events

OnRecordAuthRequest

Triggered after successful authentication:

OnRecordAuthWithPasswordRequest

Triggered during password authentication:

OnRecordAuthWithOAuth2Request

Triggered during OAuth2 authentication:

Mail events

OnMailerSend

Intercept all outgoing emails:

OnMailerRecordAuthAlertSend

Intercept auth alert emails:

Best practices

Keep hooks focused

Use appropriate event phases

Handle errors gracefully