instruction: How do I implement JWT authentication in n8n workflows?

output:
# JWT Authentication in n8n

## Generate JWT Token
```javascript
// Code node - Generate JWT
const crypto = require('crypto');

const header = { alg: 'HS256', typ: 'JWT' };
const payload = {
  sub: $json.userId,
  email: $json.email,
  iat: Math.floor(Date.now() / 1000),
  exp: Math.floor(Date.now() / 1000) + 3600 // 1 hour
};

const base64Header = Buffer.from(JSON.stringify(header)).toString('base64url');
const base64Payload = Buffer.from(JSON.stringify(payload)).toString('base64url');

const signature = crypto
  .createHmac('sha256', $env.JWT_SECRET)
  .update(`${base64Header}.${base64Payload}`)
  .digest('base64url');

return { token: `${base64Header}.${base64Payload}.${signature}` };
```

## Verify JWT Token
```javascript
// Code node - Verify JWT
const [header, payload, signature] = $json.token.split('.');

const expectedSig = crypto
  .createHmac('sha256', $env.JWT_SECRET)
  .update(`${header}.${payload}`)
  .digest('base64url');

if (signature !== expectedSig) throw new Error('Invalid token');

const decoded = JSON.parse(Buffer.from(payload, 'base64url').toString());
if (decoded.exp < Date.now() / 1000) throw new Error('Token expired');

return { valid: true, user: decoded };
```

## Workflow Pattern
```
Login Request → Validate Credentials → Generate JWT → Return Token
Protected Endpoint → Extract Token → Verify JWT → Process Request
```

type: implementation


instruction: How do I implement OAuth 2.0 flows in n8n?

output:
# OAuth 2.0 Implementation in n8n

## Authorization Code Flow

### Step 1: Redirect to Auth Provider
```javascript
const authUrl = new URL('https://provider.com/oauth/authorize');
authUrl.searchParams.set('client_id', $env.CLIENT_ID);
authUrl.searchParams.set('redirect_uri', 'https://yourapp.com/callback');
authUrl.searchParams.set('response_type', 'code');
authUrl.searchParams.set('scope', 'read write');
authUrl.searchParams.set('state', crypto.randomBytes(16).toString('hex'));

return { redirectUrl: authUrl.toString() };
```

### Step 2: Exchange Code for Token
```json
{
  "name": "Exchange Code",
  "type": "n8n-nodes-base.httpRequest",
  "parameters": {
    "url": "https://provider.com/oauth/token",
    "method": "POST",
    "body": {
      "grant_type": "authorization_code",
      "code": "={{ $json.code }}",
      "client_id": "={{ $env.CLIENT_ID }}",
      "client_secret": "={{ $env.CLIENT_SECRET }}",
      "redirect_uri": "https://yourapp.com/callback"
    }
  }
}
```

### Step 3: Refresh Token
```json
{
  "name": "Refresh Token",
  "type": "n8n-nodes-base.httpRequest",
  "parameters": {
    "url": "https://provider.com/oauth/token",
    "method": "POST",
    "body": {
      "grant_type": "refresh_token",
      "refresh_token": "={{ $json.refresh_token }}",
      "client_id": "={{ $env.CLIENT_ID }}"
    }
  }
}
```

## Token Storage
Store tokens securely in database with encryption:
```javascript
const encrypted = crypto.createCipheriv('aes-256-gcm', key, iv)
  .update(token, 'utf8', 'hex');
await db.upsert('oauth_tokens', { user_id, encrypted_token });
```

topic: oauth


instruction: How do I implement real-time data sync with Supabase in n8n?

output:
# Real-Time Supabase Sync in n8n

## Supabase Realtime Triggers
```javascript
// Subscribe to table changes
const { createClient } = require('@supabase/supabase-js');
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);

supabase
  .channel('table-changes')
  .on('postgres_changes',
    { event: '*', schema: 'public', table: 'orders' },
    (payload) => {
      // Trigger n8n webhook with payload
      fetch('https://n8n.example.com/webhook/orders', {
        method: 'POST',
        body: JSON.stringify(payload)
      });
    }
  )
  .subscribe();
```

## n8n Webhook Handler
```json
{
  "nodes": [
    {
      "name": "Supabase Webhook",
      "type": "n8n-nodes-base.webhook",
      "parameters": {"path": "orders"}
    },
    {
      "name": "Route by Event",
      "type": "n8n-nodes-base.switch",
      "parameters": {
        "rules": [
          {"value": "INSERT"},
          {"value": "UPDATE"},
          {"value": "DELETE"}
        ]
      }
    },
    {
      "name": "Handle New Order",
      "type": "n8n-nodes-base.code",
      "parameters": {
        "jsCode": "// Process new order\nconst order = $json.new;\nreturn { order, action: 'notify_warehouse' };"
      }
    }
  ]
}
```

## Bidirectional Sync Pattern
```
Supabase Change → Webhook → n8n Process → Update External System
External Change → n8n Poll/Webhook → Supabase Upsert
```

type: workflow


instruction: How do I use WebSockets with n8n for real-time updates?

output:
# WebSocket Integration with n8n

## WebSocket Server (External)
```javascript
// Node.js WebSocket server
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', (ws) => {
  ws.on('message', async (message) => {
    // Forward to n8n webhook
    await fetch('https://n8n.example.com/webhook/ws-message', {
      method: 'POST',
      body: message,
      headers: { 'Content-Type': 'application/json' }
    });
  });
});

// Broadcast from n8n
app.post('/broadcast', (req, res) => {
  wss.clients.forEach(client => {
    if (client.readyState === WebSocket.OPEN) {
      client.send(JSON.stringify(req.body));
    }
  });
  res.json({ sent: true });
});
```

## n8n Workflow
```json
{
  "nodes": [
    {"name": "WS Message Received", "type": "n8n-nodes-base.webhook"},
    {"name": "Process Message", "type": "n8n-nodes-base.code"},
    {"name": "Broadcast Response", "type": "n8n-nodes-base.httpRequest",
     "parameters": {
       "url": "http://ws-server:8080/broadcast",
       "method": "POST"
     }}
  ]
}
```

## Use Cases
- Live dashboards
- Chat applications
- Real-time notifications
- Collaborative editing
- Live order tracking

topic: websockets

type: integration


instruction: How do I trigger AWS Lambda functions from n8n?

output:
# AWS Lambda Integration with n8n

## Direct Invocation
```json
{
  "name": "Invoke Lambda",
  "type": "n8n-nodes-base.awsLambda",
  "parameters": {
    "function": "my-function-name",
    "payload": "={{ JSON.stringify($json) }}",
    "invocationType": "RequestResponse"
  },
  "credentials": {"aws": {"accessKeyId": "", "secretAccessKey": "", "region": "us-east-1"}}
}
```

## Via API Gateway
```json
{
  "name": "Lambda via API Gateway",
  "type": "n8n-nodes-base.httpRequest",
  "parameters": {
    "url": "https://abc123.execute-api.us-east-1.amazonaws.com/prod/function",
    "method": "POST",
    "headers": {"x-api-key": "={{ $env.API_GATEWAY_KEY }}"},
    "body": "={{ JSON.stringify($json) }}"
  }
}
```

## Async Invocation
```json
{
  "parameters": {
    "invocationType": "Event",
    "payload": "={{ JSON.stringify({taskId: $json.id, callback: 'https://n8n.example.com/webhook/lambda-complete'}) }}"
  }
}
```

## Lambda Function Pattern
```javascript
// Lambda handler
exports.handler = async (event) => {
  const result = await processData(event);

  // Callback to n8n
  await fetch(event.callback, {
    method: 'POST',
    body: JSON.stringify({ taskId: event.taskId, result })
  });

  return { statusCode: 200 };
};
```


instruction: How do I use Google Cloud Functions with n8n?

output:
# Google Cloud Functions with n8n

## HTTP Trigger
```json
{
  "name": "Call Cloud Function",
  "type": "n8n-nodes-base.httpRequest",
  "parameters": {
    "url": "https://REGION-PROJECT.cloudfunctions.net/function-name",
    "method": "POST",
    "headers": {
      "Authorization": "Bearer {{ $json.idToken }}"
    },
    "body": "={{ JSON.stringify($json.payload) }}"
  }
}
```

## Get ID Token for Auth
```json
{
  "name": "Get ID Token",
  "type": "n8n-nodes-base.httpRequest",
  "parameters": {
    "url": "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=https://REGION-PROJECT.cloudfunctions.net/function-name",
    "headers": {"Metadata-Flavor": "Google"}
  }
}
```

## Cloud Function Example
```javascript
const functions = require('@google-cloud/functions-framework');

functions.http('processData', async (req, res) => {
  const data = req.body;
  const result = await heavyProcessing(data);

  // Optional: callback to n8n
  if (data.callbackUrl) {
    await fetch(data.callbackUrl, {
      method: 'POST',
      body: JSON.stringify(result)
    });
  }

  res.json(result);
});
```

## Pub/Sub Trigger Pattern
```
n8n → Publish to Pub/Sub → Cloud Function → Process → Webhook back to n8n
```


instruction: How do I build ETL pipelines in n8n?

output:
# ETL Pipeline Implementation in n8n

## Extract Phase
```json
{
  "nodes": [
    {
      "name": "Extract from Source DB",
      "type": "n8n-nodes-base.postgres",
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT * FROM orders WHERE updated_at > '{{ $json.lastSync }}'"
      }
    },
    {
      "name": "Extract from API",
      "type": "n8n-nodes-base.httpRequest",
      "parameters": {
        "url": "https://api.source.com/data",
        "qs": {"since": "={{ $json.lastSync }}"}
      }
    }
  ]
}
```

## Transform Phase
```javascript
// Code node - Transform data
const transformed = items.map(item => {
  return {
    json: {
      id: item.json.id,
      customer_name: `${item.json.first_name} ${item.json.last_name}`.trim(),
      total_amount: parseFloat(item.json.amount) || 0,
      order_date: new Date(item.json.created_at).toISOString().split('T')[0],
      status: item.json.status.toUpperCase(),
      source_system: 'legacy_db'
    }
  };
});
return transformed;
```

## Load Phase
```json
{
  "name": "Load to Data Warehouse",
  "type": "n8n-nodes-base.postgres",
  "parameters": {
    "operation": "insert",
    "table": "fact_orders",
    "columns": "id, customer_name, total_amount, order_date, status, source_system",
    "options": {"onConflict": "id", "updateOnConflict": true}
  }
}
```

## Full ETL Workflow
```
Schedule Trigger (daily) → Get Last Sync Time → Extract Sources (parallel) →
Merge Data → Transform/Clean → Validate → Load to Warehouse → Update Sync Time
```

## Error Handling
- Log failed records to error table
- Continue on fail for non-critical fields
- Retry transient failures
- Alert on threshold exceeded

topic: etl


instruction: How do I implement CDC (Change Data Capture) with n8n?

output:
# Change Data Capture with n8n

## Database Triggers Approach
```sql
-- PostgreSQL trigger
CREATE OR REPLACE FUNCTION notify_n8n()
RETURNS trigger AS $$
BEGIN
  PERFORM pg_notify('table_changes',
    json_build_object(
      'table', TG_TABLE_NAME,
      'operation', TG_OP,
      'old', OLD,
      'new', NEW
    )::text
  );
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER orders_cdc
AFTER INSERT OR UPDATE OR DELETE ON orders
FOR EACH ROW EXECUTE FUNCTION notify_n8n();
```

## n8n Listener Service
```javascript
// External service listening to Postgres NOTIFY
const { Client } = require('pg');
const client = new Client(connectionString);

await client.connect();
await client.query('LISTEN table_changes');

client.on('notification', async (msg) => {
  const payload = JSON.parse(msg.payload);
  await fetch('https://n8n.example.com/webhook/cdc', {
    method: 'POST',
    body: JSON.stringify(payload)
  });
});
```

## Debezium Integration
```json
{
  "name": "Kafka CDC Consumer",
  "nodes": [
    {"name": "Poll Kafka", "type": "n8n-nodes-base.kafka",
     "parameters": {"topic": "dbserver1.public.orders"}},
    {"name": "Parse CDC Event", "type": "n8n-nodes-base.code",
     "parameters": {"jsCode": "const event = $json;\nreturn {op: event.op, before: event.before, after: event.after};"}}
  ]
}
```

## CDC Event Routing
```javascript
switch ($json.operation) {
  case 'INSERT': await handleInsert($json.new); break;
  case 'UPDATE': await handleUpdate($json.old, $json.new); break;
  case 'DELETE': await handleDelete($json.old); break;
}
```

topic: cdc


instruction: How do I integrate MongoDB with n8n?

output:
# MongoDB Integration with n8n

## Basic Operations

### Find Documents
```json
{
  "name": "Find Orders",
  "type": "n8n-nodes-base.mongoDb",
  "parameters": {
    "operation": "find",
    "collection": "orders",
    "query": {"status": "pending", "created_at": {"$gte": "={{ $json.startDate }}"}},
    "options": {"sort": {"created_at": -1}, "limit": 100}
  }
}
```

### Insert Document
```json
{
  "name": "Insert Order",
  "type": "n8n-nodes-base.mongoDb",
  "parameters": {
    "operation": "insert",
    "collection": "orders",
    "fields": {
      "customer_id": "={{ $json.customerId }}",
      "items": "={{ $json.items }}",
      "total": "={{ $json.total }}",
      "created_at": "={{ new Date().toISOString() }}"
    }
  }
}
```

### Update Document
```json
{
  "name": "Update Order Status",
  "type": "n8n-nodes-base.mongoDb",
  "parameters": {
    "operation": "update",
    "collection": "orders",
    "query": {"_id": {"$oid": "={{ $json.orderId }}"}},
    "update": {"$set": {"status": "shipped", "shipped_at": "={{ new Date().toISOString() }}"}}
  }
}
```

### Aggregation Pipeline
```json
{
  "parameters": {
    "operation": "aggregate",
    "collection": "orders",
    "pipeline": [
      {"$match": {"status": "completed"}},
      {"$group": {"_id": "$customer_id", "total_spent": {"$sum": "$total"}}},
      {"$sort": {"total_spent": -1}},
      {"$limit": 10}
    ]
  }
}
```

## Connection String
```
mongodb+srv://user:pass@cluster.mongodb.net/database?retryWrites=true
```

topic: mongodb


instruction: How do I use Firebase/Firestore with n8n?

output:
# Firebase/Firestore Integration with n8n

## Firestore Operations

### Get Document
```json
{
  "name": "Get User",
  "type": "n8n-nodes-base.httpRequest",
  "parameters": {
    "url": "https://firestore.googleapis.com/v1/projects/PROJECT/databases/(default)/documents/users/{{ $json.userId }}",
    "headers": {"Authorization": "Bearer {{ $json.accessToken }}"}
  }
}
```

### Create Document
```json
{
  "name": "Create Order",
  "type": "n8n-nodes-base.httpRequest",
  "parameters": {
    "url": "https://firestore.googleapis.com/v1/projects/PROJECT/databases/(default)/documents/orders",
    "method": "POST",
    "body": {
      "fields": {
        "customer": {"stringValue": "={{ $json.customer }}"},
        "total": {"doubleValue": "={{ $json.total }}"},
        "items": {"arrayValue": {"values": "={{ $json.items.map(i => ({stringValue: i})) }}"}}
      }
    }
  }
}
```

### Query Collection
```json
{
  "parameters": {
    "url": "https://firestore.googleapis.com/v1/projects/PROJECT/databases/(default)/documents:runQuery",
    "method": "POST",
    "body": {
      "structuredQuery": {
        "from": [{"collectionId": "orders"}],
        "where": {
          "fieldFilter": {
            "field": {"fieldPath": "status"},
            "op": "EQUAL",
            "value": {"stringValue": "pending"}
          }
        },
        "orderBy": [{"field": {"fieldPath": "created_at"}, "direction": "DESCENDING"}],
        "limit": 50
      }
    }
  }
}
```

## Firebase Admin SDK (Code Node)
```javascript
const admin = require('firebase-admin');
admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });
const db = admin.firestore();

const snapshot = await db.collection('users').where('active', '==', true).get();
return snapshot.docs.map(doc => ({ id: doc.id, ...doc.data() }));
```

topic: firebase


instruction: How do I trigger n8n workflows from Go microservices?

output:
# Go Microservices + n8n Integration

## Trigger n8n Webhook from Go
```go
package main

import (
    "bytes"
    "encoding/json"
    "net/http"
)

type WorkflowPayload struct {
    OrderID    string  `json:"order_id"`
    CustomerID string  `json:"customer_id"`
    Amount     float64 `json:"amount"`
    Action     string  `json:"action"`
}

func triggerN8NWorkflow(payload WorkflowPayload) error {
    jsonData, _ := json.Marshal(payload)

    resp, err := http.Post(
        "https://n8n.example.com/webhook/order-created",
        "application/json",
        bytes.NewBuffer(jsonData),
    )
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    return nil
}

// Usage in your service
func handleNewOrder(order Order) {
    // Process order...

    // Trigger n8n workflow
    triggerN8NWorkflow(WorkflowPayload{
        OrderID:    order.ID,
        CustomerID: order.CustomerID,
        Amount:     order.Total,
        Action:     "new_order",
    })
}
```

## n8n Calling Go Service
```json
{
  "name": "Call Go Service",
  "type": "n8n-nodes-base.httpRequest",
  "parameters": {
    "url": "http://go-service:8080/api/process",
    "method": "POST",
    "headers": {"Authorization": "Bearer {{ $env.SERVICE_TOKEN }}"},
    "body": "={{ JSON.stringify($json) }}"
  }
}
```

## Go Service Endpoint
```go
func processHandler(w http.ResponseWriter, r *http.Request) {
    var data map[string]interface{}
    json.NewDecoder(r.Body).Decode(&data)

    result := processData(data)

    json.NewEncoder(w).Encode(result)
}
```

## Async Pattern with Queue
```go
// Publish to queue, n8n consumes
func publishToQueue(msg Message) {
    ch.Publish("n8n-tasks", "", false, false, amqp.Publishing{
        ContentType: "application/json",
        Body:        msgBytes,
    })
}
```


instruction: How do I implement parallel execution in n8n for better performance?

output:
# Parallel Execution in n8n

## Split in Batches with Parallel Processing
```json
{
  "nodes": [
    {"name": "Get All Items", "type": "n8n-nodes-base.httpRequest"},
    {"name": "Split Batches", "type": "n8n-nodes-base.splitInBatches",
     "parameters": {"batchSize": 10}},
    {"name": "Process Item", "type": "n8n-nodes-base.httpRequest",
     "executeOnce": false}
  ]
}
```

## Parallel HTTP Requests
```javascript
// Code node - parallel fetch
const urls = $json.urls;
const results = await Promise.all(
  urls.map(url =>
    fetch(url).then(r => r.json()).catch(e => ({ error: e.message }))
  )
);
return results.map(r => ({ json: r }));
```

## Merge Parallel Branches
```json
{
  "nodes": [
    {"name": "Start", "type": "n8n-nodes-base.manualTrigger"},
    {"name": "Branch 1", "type": "n8n-nodes-base.httpRequest", "parameters": {"url": "https://api1.com"}},
    {"name": "Branch 2", "type": "n8n-nodes-base.httpRequest", "parameters": {"url": "https://api2.com"}},
    {"name": "Branch 3", "type": "n8n-nodes-base.httpRequest", "parameters": {"url": "https://api3.com"}},
    {"name": "Merge All", "type": "n8n-nodes-base.merge", "parameters": {"mode": "append"}}
  ],
  "connections": {
    "Start": {"main": [[{"node": "Branch 1"}, {"node": "Branch 2"}, {"node": "Branch 3"}]]},
    "Branch 1": {"main": [[{"node": "Merge All"}]]},
    "Branch 2": {"main": [[{"node": "Merge All"}]]},
    "Branch 3": {"main": [[{"node": "Merge All"}]]}
  }
}
```

## Worker Queue Pattern
```
Main Workflow → Push to Queue → Multiple Workers Process → Results to Queue → Aggregate Results
```

## Performance Tips
1. Use `executeOnce: false` for parallel item processing
2. Batch external API calls (respect rate limits)
3. Use merge node to combine parallel branches
4. Consider queue-based processing for heavy loads

type: optimization


instruction: How do I monitor n8n workflows with Grafana and Prometheus?

output:
# n8n Monitoring with Grafana/Prometheus

## n8n Metrics Endpoint
Enable in n8n environment:
```bash
N8N_METRICS=true
N8N_METRICS_PREFIX=n8n_
```

## Prometheus Scrape Config
```yaml
scrape_configs:
  - job_name: 'n8n'
    static_configs:
      - targets: ['n8n:5678']
    metrics_path: /metrics
```

## Key Metrics to Monitor

### Workflow Execution
```promql
# Execution count by workflow
sum(rate(n8n_workflow_executions_total[5m])) by (workflow_name)

# Error rate
sum(rate(n8n_workflow_executions_total{status="error"}[5m])) /
sum(rate(n8n_workflow_executions_total[5m]))

# Execution duration
histogram_quantile(0.95, rate(n8n_workflow_execution_duration_seconds_bucket[5m]))
```

### System Health
```promql
# Active executions
n8n_active_executions

# Memory usage
process_resident_memory_bytes{job="n8n"}

# CPU usage
rate(process_cpu_seconds_total{job="n8n"}[5m])
```

## Grafana Dashboard JSON
```json
{
  "panels": [
    {
      "title": "Workflow Executions/min",
      "type": "graph",
      "targets": [{"expr": "sum(rate(n8n_workflow_executions_total[1m])) by (workflow_name)"}]
    },
    {
      "title": "Error Rate %",
      "type": "gauge",
      "targets": [{"expr": "sum(rate(n8n_workflow_executions_total{status='error'}[5m])) / sum(rate(n8n_workflow_executions_total[5m])) * 100"}]
    }
  ]
}
```

## Alerting Rules
```yaml
groups:
  - name: n8n
    rules:
      - alert: HighErrorRate
        expr: sum(rate(n8n_workflow_executions_total{status="error"}[5m])) / sum(rate(n8n_workflow_executions_total[5m])) > 0.1
        for: 5m
        labels: {severity: critical}
        annotations: {summary: "n8n error rate above 10%"}
```

topic: monitoring


instruction: How do I encrypt sensitive data in n8n workflows?

output:
# Data Encryption in n8n Workflows

## AES-256-GCM Encryption
```javascript
// Code node - Encrypt
const crypto = require('crypto');

const key = Buffer.from($env.ENCRYPTION_KEY, 'hex'); // 32 bytes
const iv = crypto.randomBytes(16);

const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
let encrypted = cipher.update($json.sensitiveData, 'utf8', 'hex');
encrypted += cipher.final('hex');
const authTag = cipher.getAuthTag().toString('hex');

return {
  encrypted: encrypted,
  iv: iv.toString('hex'),
  authTag: authTag
};
```

## Decryption
```javascript
// Code node - Decrypt
const crypto = require('crypto');

const key = Buffer.from($env.ENCRYPTION_KEY, 'hex');
const iv = Buffer.from($json.iv, 'hex');
const authTag = Buffer.from($json.authTag, 'hex');

const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
decipher.setAuthTag(authTag);

let decrypted = decipher.update($json.encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');

return { decrypted };
```

## Field-Level Encryption
```javascript
// Encrypt specific fields before storage
const fieldsToEncrypt = ['ssn', 'credit_card', 'password'];
const record = $json;

for (const field of fieldsToEncrypt) {
  if (record[field]) {
    record[`${field}_encrypted`] = encrypt(record[field]);
    delete record[field];
  }
}

return record;
```

## Key Rotation Pattern
```javascript
// Re-encrypt with new key
const oldKey = Buffer.from($env.OLD_KEY, 'hex');
const newKey = Buffer.from($env.NEW_KEY, 'hex');

const decrypted = decrypt($json.encrypted, oldKey);
const reEncrypted = encrypt(decrypted, newKey);

return { encrypted: reEncrypted, keyVersion: 2 };
```

## Best Practices
1. Never log encrypted data or keys
2. Use environment variables for keys
3. Rotate keys periodically
4. Use authenticated encryption (GCM)
5. Store IV with encrypted data

topic: encryption

type: security


instruction: How do I implement automated prompt engineering and validation in n8n?

output:
# Automated Prompt Engineering in n8n

## Prompt Template System
```javascript
// Code node - Build prompt from template
const templates = {
  summarize: "Summarize the following text in {{length}} sentences:\n\n{{text}}",
  translate: "Translate the following from {{source_lang}} to {{target_lang}}:\n\n{{text}}",
  classify: "Classify the following text into one of these categories: {{categories}}\n\nText: {{text}}",
  extract: "Extract the following information from the text: {{fields}}\n\nText: {{text}}"
};

const template = templates[$json.templateType];
let prompt = template;
for (const [key, value] of Object.entries($json.variables)) {
  prompt = prompt.replace(`{{${key}}}`, value);
}
return { prompt };
```

## Prompt Validation
```javascript
// Validate prompt before sending to LLM
const prompt = $json.prompt;
const issues = [];

// Check length
if (prompt.length > 4000) issues.push('Prompt too long');
if (prompt.length < 10) issues.push('Prompt too short');

// Check for required elements
if (!prompt.includes('{{')) issues.push('No variable placeholders found');

// Check for injection attempts
const injection_patterns = ['ignore previous', 'disregard instructions'];
if (injection_patterns.some(p => prompt.toLowerCase().includes(p))) {
  issues.push('Potential prompt injection detected');
}

return { valid: issues.length === 0, issues };
```

## Output Validation
```javascript
// Validate LLM response
const response = $json.aiResponse;
const expected = $json.expectedFormat;

const validation = {
  isJSON: false,
  hasRequiredFields: false,
  withinLength: false
};

if (expected.format === 'json') {
  try {
    const parsed = JSON.parse(response);
    validation.isJSON = true;
    validation.hasRequiredFields = expected.requiredFields.every(f => f in parsed);
  } catch (e) {
    validation.isJSON = false;
  }
}

validation.withinLength = response.length <= expected.maxLength;

return {
  valid: Object.values(validation).every(v => v),
  validation,
  response: validation.isJSON ? JSON.parse(response) : response
};
```

## A/B Testing Prompts
```javascript
// Select prompt variant
const variants = [
  { id: 'A', prompt: 'Concise version...', weight: 0.5 },
  { id: 'B', prompt: 'Detailed version...', weight: 0.5 }
];

const rand = Math.random();
let cumulative = 0;
let selected;
for (const v of variants) {
  cumulative += v.weight;
  if (rand <= cumulative) { selected = v; break; }
}

return { promptVariant: selected.id, prompt: selected.prompt };
```

type: ai


instruction: How do I implement multi-agent orchestration with n8n?

output:
# Multi-Agent Orchestration in n8n

## Agent Architecture
```
Orchestrator Agent → Specialist Agents → Result Aggregation
     ↓
[Research Agent] [Writing Agent] [Review Agent] [Code Agent]
```

## Orchestrator Workflow
```json
{
  "nodes": [
    {"name": "Task Input", "type": "n8n-nodes-base.webhook"},
    {"name": "Analyze Task", "type": "n8n-nodes-base.openAi",
     "parameters": {"prompt": "Analyze this task and determine which specialists are needed: {{$json.task}}\n\nReturn JSON: {agents: ['research', 'write', 'review']}"}},
    {"name": "Parse Agent List", "type": "n8n-nodes-base.code"},
    {"name": "Route to Agents", "type": "n8n-nodes-base.switch"},
    {"name": "Research Agent", "type": "n8n-nodes-base.executeWorkflow", "parameters": {"workflowId": "research_agent"}},
    {"name": "Writing Agent", "type": "n8n-nodes-base.executeWorkflow", "parameters": {"workflowId": "writing_agent"}},
    {"name": "Aggregate Results", "type": "n8n-nodes-base.merge"}
  ]
}
```

## Specialist Agent (Research)
```javascript
// Research agent prompt
const systemPrompt = `You are a research specialist. Your role:
1. Search for relevant information
2. Verify facts from multiple sources
3. Summarize findings concisely
4. Cite sources

Return JSON: {findings: [...], sources: [...], confidence: 0-1}`;
```

## Agent Communication Protocol
```javascript
// Message format between agents
const agentMessage = {
  from: 'research_agent',
  to: 'writing_agent',
  type: 'research_complete',
  payload: {
    findings: [...],
    context: {...},
    metadata: { timestamp, confidence }
  }
};
```

## Feedback Loop
```json
{
  "nodes": [
    {"name": "Generate Content", "type": "n8n-nodes-base.openAi"},
    {"name": "Review Agent", "type": "n8n-nodes-base.openAi",
     "parameters": {"prompt": "Review this content for accuracy and quality..."}},
    {"name": "Check Approval", "type": "n8n-nodes-base.if",
     "parameters": {"conditions": {"string": [{"value1": "={{ $json.approved }}", "value2": "true"}]}}},
    {"name": "Return to Generator", "type": "n8n-nodes-base.set"}
  ]
}
```

## Best Practices
1. Define clear agent responsibilities
2. Use structured message formats
3. Implement timeout handling
4. Log all agent interactions
5. Set max iteration limits for loops