Chapter 5

Triggers In Depth

Ch05 Triggers In Depth

Triggers are the entry point of every workflow โ€” they decide when automation runs. n8n provides three trigger categories: scheduled (Schedule), event-based (Webhook/Event), and polling. Choosing the right trigger type directly affects both real-time responsiveness and resource consumption. This chapter covers each type in depth, with special attention to Cron expressions โ€” the foundation of most production scheduled tasks.

Schedule Trigger

Schedule Trigger supports two configuration modes:

Simple mode: Select an interval from a dropdown (every minute, hourly, daily, etc.) โ€” suitable for straightforward recurring tasks.

Cron expression mode: Use Unix Cron syntax for precise scheduling (e.g., "weekdays at 9 AM", "first business day of every month").

Cron Expression Syntax

n8n Cron expressions have 5 fields (no seconds field):

Minute  Hour  Day  Month  Weekday
  |       |    |     |       |
  *       *    *     *       *
Field Range Special chars
Minute 0-59 * , - /
Hour 0-23 * , - /
Day 1-31 * , - / ?
Month 1-12 * , - /
Weekday 0-7 (both 0 and 7 = Sunday) * , - / ?

Common Cron expression examples:

Expression Meaning
0 8 * * * Every day at 8:00 AM
0 8 * * 1-5 Weekdays at 8:00 AM
*/15 * * * * Every 15 minutes
0 9,18 * * * Daily at 9:00 AM and 6:00 PM
0 0 1 * * First day of every month at midnight
30 8 * * 1 Every Monday at 8:30 AM

Timezone: Schedule Trigger uses the n8n instance's system timezone (set via the GENERIC_TIMEZONE environment variable). Always configure the appropriate timezone for your location to prevent triggers firing at unexpected UTC-offset times.

Webhook Trigger: HTTP Event Receiver

Webhook Trigger creates a unique HTTP endpoint URL for your workflow. External systems trigger the workflow by sending HTTP requests to this URL โ€” the core mechanism for real-time, event-driven automation.

Configuration Parameters

Generated webhook URL format:

https://your-n8n.com/webhook/[unique-id-after-activation]

Test URL (while workflow is inactive):

https://your-n8n.com/webhook-test/[unique-id]

Common Webhook Use Cases

Webhook Security: Signature Verification

For webhooks from GitHub, GitLab, Stripe, etc., verify the request signature to prevent forgery:

// Code node after the Webhook node for signature verification
const crypto = require('crypto');

const secret = $vars.GITHUB_WEBHOOK_SECRET;
const payload = $input.first().json.body;
const signature = $input.first().headers['x-hub-signature-256'];

const expectedSignature = 'sha256=' + crypto
  .createHmac('sha256', secret)
  .update(JSON.stringify(payload))
  .digest('hex');

if (signature !== expectedSignature) {
  throw new Error('Invalid webhook signature - request rejected');
}

return $input.all();

Polling Trigger

For services that don't support webhook push, n8n uses periodic polling to check for new data. Many built-in nodes include polling trigger capability:

Polling has lower real-time responsiveness (typically minute-level intervals) and generates frequent API calls. Prefer webhooks over polling whenever the service supports them.

Trigger Selection Guide

Scenario Recommended Trigger Notes
Scheduled reports, data sync Schedule Trigger Most reliable, zero dependencies
Receive third-party platform events Webhook Trigger Best real-time response
Monitor inbox for new emails Email Trigger Built-in IMAP polling
Monitor RSS content updates RSS Feed Trigger Auto-deduplicates read entries
Monitor GitHub activity GitHub Trigger Supports multiple event types
Called by another workflow Execute Workflow Trigger Sub-workflow entry point

Database Trigger (PostgreSQL)

For scenarios requiring immediate triggering when new records are inserted, n8n's PostgreSQL Trigger uses the LISTEN/NOTIFY mechanism for true real-time triggering without polling:

-- Create a trigger function in PostgreSQL
CREATE OR REPLACE FUNCTION notify_new_order()
RETURNS TRIGGER AS $$
BEGIN
  PERFORM pg_notify('new_order', row_to_json(NEW)::text);
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Bind the trigger to the orders table
CREATE TRIGGER orders_after_insert
  AFTER INSERT ON orders
  FOR EACH ROW EXECUTE FUNCTION notify_new_order();

The n8n PostgreSQL Trigger node listens on the new_order channel and fires within milliseconds of each new order insertion โ€” typical latency under 100ms.

Rate this chapter
4.9  / 5  (59 ratings)

๐Ÿ’ฌ Comments