Example: Closed-Loop Router
This is the reference answer to “show me HiveCFM driving something outside HiveCFM”. A response comes in, its KPI score is below a threshold, and a case appears in an external CRM with a priority, an owner, and the customer’s own words attached. No code in hivecfm-core changes to add a second CRM.
The workflow ships in the repo:
hivecfm-core/scripts/n8n-workflow.json
name: "Al Rajhi Bank - CX Closed-Loop Feedback Router"
active: falseFourteen nodes: one webhook, a Switch on survey type, three IF thresholds, three Set nodes that build the case, three HTTP POSTs that create it, three HTTP POSTs that raise the alert.
What it does
A promoter or a satisfied customer falls out at the IF node and nothing is created. That is the intended behavior, and it is what test 6 in the harness checks.
Thresholds and priority
| SurveySurveyThe core HiveCFM object. A definition of questions, logic, and targeting — shown to respondents to collect feedback. type | Routed when | Verbatim field read | P1 when | Otherwise |
|---|---|---|---|---|
| NPS | nps-score ≤ 6 | nps-detractor-reason | score ≤ 3 | P2 |
| CSAT | csat-overall ≤ 2 | csat-feedback | score ≤ 1 | P2 |
| CES | ces-score ≤ 3 | ces-difficulty | never | P2 at ≤ 2, else P3 |
Each Set node emits the same case shape, so the receiving system sees one contract regardless of survey type:
{
"customerId": "CUST-TEST-001",
"surveyType": "NPS",
"score": 3,
"verbatim": "Long wait times at branch and mobile app is slow",
"priority": "P1",
"assignedTeam": "Customer Recovery Team",
"status": "Open"
}Every value is read from the response’s own answer data, with ?? 'unknown' style fallbacks, so a missing hidden field degrades the case rather than failing the workflow.
The one mapping that is wrong
The Switch node branches on {{ $json.data.surveyName || $json.surveyName || '' }}. The HiveCFM webhook payload contains no surveyName at any depth. The survey name is at data.survey.title. The Switch also has no fallback output, so as shipped every item compares an empty string, matches no branch, and nothing is created.
Repoint the expression at data.survey.title, then confirm the prefix against the webhook node’s own output before trusting it: n8n’s Webhook node nests the HTTP request under body, which makes the expression {{ $json.body.data.survey.title || '' }} and means the score paths need the same prefix ($json.body.data.data['nps-score']). Open one execution, read the Webhook node’s output, and use whichever root it actually shows. The field name is the verified part; the prefix depends on your n8n version’s node output.
This is also why the file ships "active": false. Treat it as a verified-by-reading template, not a tested one. The payload contract it must match is documented in n8n → The payload your workflow receives.
Running it end to end
Prerequisites: the composeDocker ComposeThe YAML file and CLI that spin up Postgres, Redis, MinIO, MailHog locally with one command. stack up, an environment id, and three surveys whose names contain NPS, CSAT, and CES with the question ids from the table above. scripts/create-surveys.sh builds them and writes scripts/.survey-ids.env.
- Import. In n8n, import
scripts/n8n-workflow.json. - Fix the Switch expression. See above. Nothing works before this.
- Repoint the HTTP nodes. The shipped JSON posts to a mock CRM host. Change the three
/casesand three/notificationsURLs to your target, and attach a credential if it needs auth. - Activate the workflow and copy its production webhook URL.
- Register the HiveCFM webhook for that environment: URL from step 4, trigger
responseFinished, and a secret if the receiver verifies signatures. - Fire the harness.
cd hivecfm-core
./scripts/test-closed-loop.shThe harness submits four responses through the public client API, three below threshold and one promoter, then asserts that exactly three cases and three notifications appeared:
curl -sS -X POST "$HIVECFM_URL/api/v1/client/$HIVECFM_ENV_ID/responses" \
-H "Content-Type: application/json" \
-d '{
"surveyId": "'"$NPS_SURVEY_ID"'",
"finished": true,
"data": {
"nps-score": 3,
"nps-detractor-reason": "Long wait times at branch and mobile app is slow",
"customerId": "CUST-TEST-001",
"branchCode": "BR-RYD-001",
"channel": "branch"
},
"language": "en"
}'No API key is involved: api/v1/client/:environmentId/responses is a public respondent endpoint, which is exactly what makes this runnable from a laptop. Both v1 and v2 client response routes exist in the tree; the harness targets v1, and the widget uses v2.
Adapting it to another system
The three things that change per client, and nothing else:
| Change | Where |
|---|---|
| Target system | The six HTTP Request node URLs, plus an n8n credential for auth |
| Thresholds | rightValue on the three IF nodes |
| Case shape | The three Set nodes’ jsonOutput |
Two structural improvements worth making before production use:
- Collapse the three branches. NPS, CSAT, and CES run identical Set → POST → Notify chains. One parameterized branch is less to keep in sync, and adding a fourth KPI stops being a copy-paste.
- Acknowledge fast. Hive gives a webhook consumer 5 seconds and never retries. If the CRM is slow, have the webhook node respond immediately and do the POST in a queued follow-up, or drops will be invisible.
Where to look in the code
hivecfm-core/scripts/n8n-workflow.json— the workflow.hivecfm-core/scripts/test-closed-loop.sh— the eight-test harness, including the promoter negative case.hivecfm-core/scripts/create-surveys.sh— creates the three surveys and.survey-ids.env.hivecfm-core/apps/web/app/api/(internal)/pipeline/route.ts— wherekpiScoresis attached and the POST is made.