Skip to main content

Trigger Functions

Trigger functions execute automatically when data changes in a collection. Use them for validation, transformation, notifications, and integrations.

How It Works

  1. Configure which collection to monitor
  2. Select which operations trigger execution (POST, PUT, PATCH, DELETE)
  3. Write JavaScript code or configure an external webhook
  4. Function runs automatically on matching operations

Configuration

Basic Settings

SettingDescription
NameUnique identifier for the function
DescriptionOptional documentation
EnabledToggle to enable/disable without deleting
CollectionThe collection to monitor
MethodsWhich operations trigger execution

Triggering Methods

MethodWhen It Fires
POSTNew item created
PUTItem replaced
PATCHItem updated
DELETEItem deleted

Select one or more methods. The function fires for each matching operation.

Execution Context

What Your Code Receives

ObjectDescription
itemThe record being created, updated, or deleted
meThe user who triggered the change
reqRequest details (method, headers, query)
apiAPI information
secretsConfigured secrets

Token Options

Control whose permissions the function uses:

OptionDescription
NoneNo authentication (anonymous)
Current UserExecute as the user who triggered the change
Service AccountExecute with service account permissions

Execution Control

OptionDescription
Skip Security PolicyBypass row-level access control
Skip RolesIgnore role-based permissions
Halt on ErrorStop the operation if the function fails

Use Cases

Data Validation

Validate data before it's saved:

// Reject orders with invalid totals
if (item.total < 0) {
throw new Error('Order total cannot be negative');
}

if (item.items.length === 0) {
throw new Error('Order must have at least one item');
}

Data Transformation

Transform values before saving:

// Auto-generate slug from title
item.slug = item.title
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '');

// Set timestamps
item.updatedAt = new Date().toISOString();
item.updatedBy = me.id;

Notifications

Send notifications on changes (requires paid tier):

// Notify when order status changes
if (item.status === 'shipped') {
await fetch('https://api.email-service.com/send', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
to: item.customerEmail,
subject: 'Your order has shipped!',
body: `Order ${item.orderNumber} is on its way.`
})
});
}

External Sync

Sync data with external systems (requires paid tier):

// Sync customer to CRM
await fetch('https://crm.example.com/api/contacts', {
method: 'POST',
headers: {
'Authorization': `Bearer ${secrets.CRM_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: item.email,
name: item.name,
source: 'website'
})
});

Cascade Operations

Perform related operations using built-in methods:

// When a project is deleted, archive its tasks
if (req.method === 'DELETE') {
const tasks = await get(`tasks?filter=projectId eq "${item.id}"`);
for (const task of tasks) {
await patch(`tasks/${task.id}`, { archived: true });
}
}

External Endpoint Action

Instead of JavaScript, call an external webhook:

SettingDescription
URLThe webhook endpoint to call
Custom HeadersAdditional headers to include
Send TokenInclude authentication token
Halt on ErrorStop if webhook fails

The webhook receives the same context as JavaScript functions.

Best Practices

  • Keep functions focused — One function, one responsibility
  • Handle errors gracefully — Use try-catch and meaningful error messages
  • Use Halt on Error carefully — Only when the operation should fail if the function fails
  • Test thoroughly — Use the built-in test runner before enabling
  • Log important events — Use console.log for debugging and auditing