# CountWatch recipe: n8n

Counts what an n8n workflow does (leads created, forms submitted, rows synced) per 5 minutes and alerts when it spikes, drops or stops. One HTTP Request node at the end of the workflow calls a webhook URL. Needs a CountWatch account (the person signs up at https://staging.countwatch.com/login) and a webhook URL the person makes in the dashboard (Sources, https://staging.countwatch.com/dashboard/sources): it is a secret, so the agent never handles it.

## 1. The webhook URL and the watch

The person makes the webhook URL for `workflow_items` in the dashboard (Sources, https://staging.countwatch.com/dashboard/sources) and keeps it: it is a secret for this one metric, so the agent never sees it. The agent creates the watch as a push watch first (a 5-minute bucket without hits is zero; a watch's source is fixed when it is created): over MCP, `send_counts {"source": "push", "points": [{"metric": "workflow_items", "add": 0}]}`; with an API key:

```
curl -sX POST https://staging.countwatch.com/v1/ingest -H "Authorization: Bearer $COUNTWATCH_API_KEY" -H "Content-Type: application/json" \
  -d '{"source":"push","points":[{"metric":"workflow_items","add":0}]}'
```

Query parameters on the link become dimensions: the watch here must have the same ones as the link's hits (each dimension combination is a watch: mind the plan's watch limit).

## 2. Add the node

Copy this node, paste it into the workflow canvas (Ctrl+V or Cmd+V), put the webhook URL in its URL, and connect it after the step whose outcome you count (so a failed run counts nothing):

```json
{
  "nodes": [
    {
      "parameters": {
        "method": "POST",
        "url": "<webhook url>",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "n",
              "value": "={{ $input.all().length }}"
            }
          ]
        },
        "options": {}
      },
      "name": "CountWatch ping",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        0,
        0
      ],
      "executeOnce": true,
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 2000,
      "onError": "continueRegularOutput"
    }
  ],
  "connections": {}
}
```

It runs once per execution (Settings → Execute Once) and sends `n` = the number of items that reached it, so one run with 40 new leads adds 40 in one request. It retries a failed call twice, then lets the workflow go on (Settings → On Error: continue): a refused call never fails your workflow, and the missing count shows as a drop. A run with no items does not run the node: the bucket reads as zero, which is what a push watch expects. To count runs instead of items, remove the `n` query parameter. Every call is one request against the plan, so keep Execute Once on.

Do not add n8n's execution id as `?id=`: it restarts from 1 when n8n's database is reset, and the webhook URL would drop new runs as retries for 4 days.

## 3. History

n8n keeps its execution history for a short time only. Skip the import: the watch learns for about 3 weeks, with starter rules meanwhile.

## Check it works

1. **Data arrives.** `check_setup` (or `curl -s https://staging.countwatch.com/v1/status -H "Authorization: Bearer $COUNTWATCH_API_KEY"`): the `workflow_items` watch shows `last_request_at`, and `missing_buckets_24h` stays `0` (explicit watches). Do what `next_actions` says.
2. **History is in.** `GET /v1/imports/{import_id}` shows `status: done` and the rules chosen for `workflow_items`, with the alerts they would have raised. (Skipped the import? The watch learns for about 3 weeks; starter rules apply meanwhile.)
3. **An alert reaches you.** The person turns on e-mail alerts in the dashboard (https://staging.countwatch.com/dashboard), or you add a webhook (`manage add_destination`, then `verify_destination`; the endpoint answers the challenge, see https://staging.countwatch.com/agent). Then `manage {"action": "send_test_alert"}`: every active destination receives a test alert (`test: true`) within a few minutes.
