Skip to content
JavaScript Hooks

Add server logic directly inside PocketBase

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.

Included on every plan
JavaScript Hooks
Dashboard demo
How it works

From an idea to a running hook

Write server-side behavior in the dashboard and let your PocketBase instance load it for you.

01

Create

main.pb.js

Open your PocketBase instance, go to Hooks, and create a .pb.js file.

02

Write

onRecord* · routerAdd

Use the PocketBase JavaScript VM API to add the server behavior you need.

03

Save & reload

Hook active ✓

Save in the dashboard and your instance automatically reloads the hook.

Environment Variables

Give your hooks secrets, not hard-coded credentials

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.

Explore environment variables
Environment Variables
encrypted at rest
01 · Dashboard

Encrypted secret

Save the value without placing it in your hook source.

02 · Hook

$os.getenv()

Read the injected value from server-side JavaScript.

03 · Integration

$http.send()

Authenticate a request to an external service safely.

Capabilities

Server-side behavior, built into your instance

Use PocketBase’s embedded JavaScript VM for lightweight logic close to your data — no separate service to deploy.

Record events

Create · Update · Delete

Validate, transform, or react to records before and after database changes.

records.pb.js
active
1onRecordCreateRequest((e) => {
2  e.record.set('slug', slugify(...))
3  e.next()
4}, 'posts')

Custom API routes

routerAdd

Expose purpose-built HTTP endpoints directly from your PocketBase instance.

routes.pb.js
active
1routerAdd('GET', '/api/hello/{name}', (e) => {
2  return e.json(200, { message: 'Hello!' })
3})

Scheduled jobs

cronAdd

Run recurring cleanup, reporting, or synchronization tasks on a cron schedule.

jobs.pb.js
active
1cronAdd('cleanup', '0 3 * * *', () => {
2  $app.logger().info('cleanup complete')
3})

Outbound integrations

$http.send()

Use encrypted environment variables when calling third-party APIs, triggering webhooks, or sending email.

integrations.pb.js
active
1const key = $os.getenv('API_KEY')
2const res = $http.send({
3  url: 'https://api.example.com', headers: { key }
4})
Choose the right runtime

Hooks or a backend?

Every plan

JavaScript Hooks

Best for lightweight logic tied closely to PocketBase data.

  • Record validation and automation
  • Custom routes, webhooks, and cron
  • No separate deployment
Pro

Backend Hosting

Use a dedicated backend when your workload needs a normal server project.

  • Long-running work or large dependencies
  • Websockets and full frameworks
  • Node.js, Next.js, Deno, and Bun
Explore Backend Hosting
CLI

Keep your hooks in the repository

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.

my-app — zsh
# once, on your machine
npm i -g @pocketbasecloud/cli
pb cloud login
# push the whole directory
pb cloud pb hooks push ./pb_hooks --name my-app-db
  uploading main.pb.js · billing.pb.js
  reloading my-app-db
✓ 2 hooks active

The same tool, every day after

  • pb cloud pb hooks lshooks pushed through the CLI
  • pb cloud pb hooks rm old.pb.jsremove one you no longer need
  • pb cloud pb deploypush hooks and migrations together
  • pb cloud logs -fwatch a hook run in production

Hooks written in the dashboard are live but unmanaged. Push them once and the CLI can list, replace, and remove them.

Read the hooks docs
JavaScript Hooks

Make PocketBase behave like your application needs

Create your first hook from the dashboard and add server-side behavior without another deployment.