<!-- skill: n8n Flows | version: 1.0.0 | description: Self-host n8n triggers, webhooks, vaults and error flows | category: AI & Automation -->
## Instructions
- Self-host with Docker Compose: pinned n8n image, Postgres volume, timezone set, behind reverse proxy with TLS.
- Model triggers explicitly: webhook for events, schedule for polls, manual for backfills — document each choice.
- Store secrets in credentials vault: one credential per service, never hardcode tokens in expressions or Code nodes.
- Add error branches on every external call: Error Trigger + Slack/email alert + retry counter, never silent fail.
- Use queue mode note: Redis + 2 workers over 50 execs/day, else main process stalls on long AI waits.
- Version workflows JSON in git: export weekly, name lead-sync-v1.4.json, note n8n version in commit.
- Paginate and batch: SplitInBatches 50 items, rate-limit 5/sec, respect API quotas with Wait nodes.
- Idempotency first: dedupe by external id in Postgres, check-before-create so replays never double-post.
- Test with pinned data: pin webhook payload, run step-by-step, unpin before activating production flow.
- Separate dev/prod: N8N_ENCRYPTION_KEY backed up, staging instance on different port, promote via import.
- Log to external store: workflow id + execution id + ms to Postgres for SLA dashboards and postmortems.
- Cap AI spend: max-tokens 800, temperature 0.3, daily execution cap alert at 80% via IF + notify branch.
## Examples
- Example 1: Lead sync: Typeform webhook > dedupe Postgres > HubSpot create > Slack alert branch on 409.
- Example 2: Nightly digest: schedule 06:00 > RSS fetch > GPT summarize 800tok > email, queue mode Redis 2 workers.
- Example 3: Stripe guard: webhook signature verify in Code > error branch posts to ops channel with execution URL.
- Example 4: Backfill: manual trigger > SplitInBatches 50 > Wait 200ms > Sheets append, replay-safe by row id.
## Workflows
- Workflow A — Setup: 1. Compose up 2. Encryption key backup 3. Users/roles 4. Credentials vault 5. TLS proxy.
- Workflow B — Build: 1. Trigger pick 2. Happy path 3. Error branch 4. Pin test 5. Export JSON 6. Activate.
- Workflow C — Scale: 1. Enable Redis queue 2. Add workers 3. Postgres prune 4. Monitor exec time 5. Cap AI.
- Workflow D — Incident: 1. Failed exec link 2. Replay pinned 3. Fix node 4. Re-export 5. Postmortem note.
## Templates
- Docker Compose self-host:
```yaml
services:
  postgres: {image: postgres:16, volumes: [pgdata:/var/lib/postgresql/data]}
  redis: {image: redis:7-alpine}
  n8n:
    image: n8nio/n8n:1.61.0
    environment: [DB_TYPE=postgresdb, QUEUE_BULL_REDIS_HOST=redis, N8N_ENCRYPTION_KEY=${N8N_KEY}, GENERIC_TIMEZONE=Europe/Berlin]
    ports: ["5678:5678"]
    volumes: [n8ndata:/home/node/.n8n]
```
- Webhook verify Code node:
```js
const sig = $headers["stripe-signature"];
if (!verify(sig, $json.raw, $credentials.stripeSecret)) throw new Error("bad-signature");
return [{json: $input.first().json}];
```
- Error branch: HTTP node onError=continueErrorOutput > IF status!=200 > Slack post `{{$execution.id}} failed {{$node.name}}`.
- Queue env note: EXECUTIONS_MODE=queue, QUEUE_HEALTH_CHECK_ACTIVE=true, 2 workers via `n8n worker --concurrency 10`.
## Tools
- n8n core + community nodes — 400+ integrations plus npm-installable custom nodes for niche APIs.
- Postgres + Redis — durable executions plus Bull queue backend required for worker scaling.
- Caddy / Traefik — automatic TLS in front of port 5678 with basic-auth or OAuth gate.
- Uptime Kuma — ping /healthz every 60s, alert when queue latency exceeds 30s.
- Git + n8n export — version control via n8n export:workflow --all for weekly snapshots.
- Slack / SMTP notify — error-branch destinations with execution URL for one-click replay.
## Advanced
- Shard heavy flows: parent calls sub-workflow via Execute Workflow, isolates retries and keeps canvas readable.
- Backpressure with Wait: 429 triggers 60s exponential backoff x4 then dead-letter Postgres table.
- Encrypt extra fields: use $credentials only, enable N8N_BLOCK_ENV_ACCESS_IN_NODE=true to stop env leaks.
- Prune executions: EXECUTIONS_DATA_PRUNE=true, MAX_AGE=168h, keeps Postgres under 10GB at 5k execs/day.
- Feature-flag nodes: IF $env.FEATURE_X equals true routes to v2 chain without deactivating flow.
- Mirror to queue gradually: duplicate flow to worker-enabled instance, compare p95 times before cutover.
- Audit credential access: quarterly rotate tokens, list unused credentials via API, delete orphans.
- Load-test webhooks: k6 50 rps 5min against staging, watch worker lag metric before raising prod limits.
## Documentation
- n8n hosting docs — Compose, Postgres/Redis, queue mode, encryption key backup procedure.
- n8n workflow docs — triggers, error outputs, expressions, Code node API and paging patterns.
- n8n credentials docs — vault types, sharing, external secret stores and rotation steps.
- Reverse-proxy TLS guide — Caddy/Traefik config for websockets and webhook delivery.
- API rate-limit catalog — per-service quotas to encode in Wait/SplitInBatches nodes.
