{
  "name": "Burrow Event",
  "nodes": [
    {
      "id": "when-executed-by-another-workflow",
      "name": "When Executed by Another Workflow",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        0
      ],
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "client"
            },
            {
              "name": "events",
              "type": "array"
            }
          ]
        }
      }
    },
    {
      "id": "lookup-client",
      "name": "Lookup Client",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1,
      "position": [
        220,
        0
      ],
      "parameters": {
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultName": "burrow_destinations"
        },
        "filters": {
          "conditions": [
            {
              "keyName": "key",
              "keyValue": "={{ $json.client }}"
            }
          ]
        },
        "returnAll": true
      },
      "alwaysOutputData": true
    },
    {
      "id": "known-client",
      "name": "Known Client?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        440,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "c",
              "leftValue": "={{ $json.client_id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "unknown-client",
      "name": "Unknown Client",
      "type": "n8n-nodes-base.stopAndError",
      "typeVersion": 1,
      "position": [
        660,
        180
      ],
      "parameters": {
        "errorMessage": "=Burrow Event: no row in burrow_destinations for client key \"{{ $('When Executed by Another Workflow').first().json.client }}\". Nothing was sent."
      }
    },
    {
      "id": "build-envelopes",
      "name": "Build Envelopes",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        -60
      ],
      "parameters": {
        "jsCode": "// Why Code: builds one Burrow envelope per event in one place.\n// Paste your Burrow organization ID here. It is an ID, not a secret; the ingestion key lives in the credential.\nconst ORGANIZATION_ID = 'PASTE_YOUR_BURROW_ORGANIZATION_ID';\nif (ORGANIZATION_ID.startsWith('PASTE')) throw new Error('Burrow Event: set ORGANIZATION_ID in Build Envelopes.');\nconst rows = $input.all().map(i => i.json).filter(r => r.client_id);\nconst input = $('When Executed by Another Workflow').first().json;\nlet events = input.events;\nif (typeof events === 'string') { try { events = JSON.parse(events); } catch (e) { events = []; } }\nif (!Array.isArray(events)) events = [];\nevents = events.filter(e => e && e.event);\nconst out = [];\nfor (const row of rows) {\n  for (const e of events) out.push({ json: { envelope: {\n    organizationId: ORGANIZATION_ID,\n    clientId: row.client_id,\n    projectId: row.project_id || null,\n    channel: 'automations',\n    event: String(e.event),\n    timestamp: e.timestamp || new Date().toISOString(),\n    source: 'n8n',\n    ...(e.description ? { description: String(e.description).slice(0, 200) } : {}),\n    ...(e.externalEventId ? { externalEventId: String(e.externalEventId) } : {}),\n    properties: e.properties || {},\n    tags: { platform: 'n8n', ...(e.tags || {}) },\n  } } });\n}\nreturn out;"
      }
    },
    {
      "id": "post-to-burrow",
      "name": "POST to Burrow",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        880,
        -60
      ],
      "parameters": {
        "method": "POST",
        "url": "https://app.useburrow.com/api/v1/events",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.envelope) }}",
        "options": {
          "batching": {
            "batch": {
              "batchSize": 10,
              "batchInterval": 200
            }
          }
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000
    },
    {
      "id": "return",
      "name": "Return",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1100,
        -60
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "ok",
              "name": "ok",
              "type": "boolean",
              "value": true
            },
            {
              "id": "ev",
              "name": "event",
              "type": "string",
              "value": "={{ $('Build Envelopes').item.json.envelope.event }}"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "sticky-note",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -520
      ],
      "parameters": {
        "width": 660,
        "height": 400,
        "content": "## Burrow Event\nSends workflow and agent events to Burrow's **Automations** channel.\n\n**Input:** `{ \"client\": \"acme\", \"events\": [ \u2026 ] }`\n\n**Event shapes:** each entry in `events` is a Burrow event (`event`, `properties`, `tags`, optional `timestamp` and `externalEventId`). See every event in the [Burrow Playground](https://app.useburrow.com/playground?channel=automations).\n\n**Set up once:**\n1. *Lookup Client*: choose your **burrow_destinations** data table.\n2. *Build Envelopes*: paste your Burrow organization ID.\n3. *POST to Burrow*: choose a Bearer Auth credential holding your Burrow ingestion key.\n\n- `client` is a `key` in the data table, the one place Burrow client and project IDs live.\n- This workflow adds `organizationId`, `clientId`, `projectId`, `channel`, `source: n8n` and tag `platform: n8n`.\n- Call it with *Wait for completion* off, after any reply to a visitor.\n- An unknown `client` sends nothing and fails, so a typo is noticed."
      }
    }
  ],
  "settings": {
    "executionOrder": "v1",
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "none",
    "callerPolicy": "workflowsFromSameOwner"
  },
  "connections": {
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Lookup Client",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Lookup Client": {
      "main": [
        [
          {
            "node": "Known Client?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Known Client?": {
      "main": [
        [
          {
            "node": "Build Envelopes",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Unknown Client",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Envelopes": {
      "main": [
        [
          {
            "node": "POST to Burrow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "POST to Burrow": {
      "main": [
        [
          {
            "node": "Return",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
