Create
main.pb.jsOpen your PocketBase instance, go to Hooks, and create a .pb.js file.
Write and manage JavaScript hooks from the dashboard. React to data changes, create API routes, schedule jobs, and call external services without deploying a separate backend.
Write server-side behavior in the dashboard and let your PocketBase instance load it for you.
main.pb.jsOpen your PocketBase instance, go to Hooks, and create a .pb.js file.
onRecord* · routerAddUse the PocketBase JavaScript VM API to add the server behavior you need.
Hook active ✓Save in the dashboard and your instance automatically reloads the hook.
Store API keys, webhook secrets, and per-environment configuration encrypted at rest. PocketBase Cloud injects them into your instance so hooks can read them with $os.getenv().
Updating an environment variable restarts the instance. Batch related edits to keep the brief interruption to one restart.
Save the value without placing it in your hook source.
Read the injected value from server-side JavaScript.
Authenticate a request to an external service safely.
Use PocketBase’s embedded JavaScript VM for lightweight logic close to your data — no separate service to deploy.
Validate, transform, or react to records before and after database changes.
1onRecordCreateRequest((e) => {
2 e.record.set('slug', slugify(...))
3 e.next()
4}, 'posts')Expose purpose-built HTTP endpoints directly from your PocketBase instance.
1routerAdd('GET', '/api/hello/{name}', (e) => {
2 return e.json(200, { message: 'Hello!' })
3})Run recurring cleanup, reporting, or synchronization tasks on a cron schedule.
1cronAdd('cleanup', '0 3 * * *', () => {
2 $app.logger().info('cleanup complete')
3})Use encrypted environment variables when calling third-party APIs, triggering webhooks, or sending email.
1const key = $os.getenv('API_KEY')
2const res = $http.send({
3 url: 'https://api.example.com', headers: { key }
4})Best for lightweight logic tied closely to PocketBase data.
Use a dedicated backend when your workload needs a normal server project.
The dashboard editor is one way in. Keep pb_hooks/ in version control instead, review hooks like the rest of your code, and push the directory when it is ready.
pb cloud pb hooks lshooks pushed through the CLIpb cloud pb hooks rm old.pb.jsremove one you no longer needpb cloud pb deploypush hooks and migrations togetherpb cloud logs -fwatch a hook run in productionHooks written in the dashboard are live but unmanaged. Push them once and the CLI can list, replace, and remove them.
Read the hooks docsCreate your first hook from the dashboard and add server-side behavior without another deployment.