Skip to main content
Hooks let you “hook into” the lifecycle of API calls and webhook processing. Use them to add custom logic — logging, validation, transformations — without modifying your core application code.
corsair.ts

Before Hooks

Before hooks run before an operation executes. Use them to:
  • Log or audit actions
  • Validate or modify input
  • Add default values
  • Short-circuit operations

Modify Arguments

Transform the arguments before the API call is made.
corsair.ts

Validate Input

Check conditions before proceeding.
corsair.ts

Log Actions

Track every API call for debugging or auditing.
corsair.ts

After Hooks

After hooks run after an operation completes. Use them to:
  • Log results
  • Trigger side effects
  • Transform responses
  • Send notifications

Send Notifications

Notify your team when something happens.
corsair.ts

Log Results

Track successful operations.
corsair.ts

API Hooks

API hooks are defined under hooks in your plugin configuration. They follow the structure: hooks.[resource].[action].before/after.
corsair.ts

Webhook Hooks

Webhook hooks are defined under webhookHooks. They guarantee your logic runs every time a webhook is processed — even if Corsair handles the database update automatically.
corsair.ts
See Webhooks for more on webhook processing.

Context Object

Both before and after hooks receive a ctx object with useful properties:
  • ctx.options — Plugin configuration options
  • ctx.db — Database service clients for this plugin
  • ctx.endpoints — Bound API endpoints (call other APIs within hooks)
example.ts

Hook Order

When both plugin-level and operation-level hooks exist:
  1. Before hooks run in order: plugin → operation
  2. The operation executes
  3. After hooks run in order: operation → plugin
This lets you add global logging at the plugin level while keeping operation-specific logic separate.