Skip to main content

GET /api/v1/behavioral-metrics

List all metric definitions with value counts. Supports cursor-based pagination.

Response

{
  "data": [
    {
      "id": "metric_001",
      "name": "purchase_frequency_30d",
      "description": "Number of purchases in last 30 days",
      "aggregateFunction": "count",
      "sourceField": "purchase",
      "windowDays": 30,
      "computeMode": "batch",
      "batchIntervalMin": 60,
      "status": "active",
      "_count": { "values": 4521 },
      "createdAt": "2026-02-01T10:00:00.000Z"
    }
  ],
  "total": 8,
  "hasMore": false
}

POST /api/v1/behavioral-metrics

Create a new metric definition. Maximum 20 metrics per tenant.

Request Body

FieldTypeRequiredDescription
namestringYesUnique metric name
descriptionstringNoDescription
aggregateFunctionstringYes"count", "sum", "avg", "min", "max"
sourceFieldstringYesSource interaction field to aggregate
windowDaysintegerNoRolling window in days (null = all-time)
groupByDimensionsarrayNoDimension fields to group by
filterConditionsobjectNoFilter conditions for the aggregation
sqlFilterstringNoRaw SQL WHERE clause filter
computeModestringNo"batch" or "realtime". Default: "batch"
batchIntervalMinintegerNoBatch compute interval in minutes. Default: 60
Realtime metrics only support count or sum aggregate functions.

Example

curl -X POST https://playground.kaireonai.com/api/v1/behavioral-metrics \
  -H "Content-Type: application/json" \
  -H "X-Tenant-Id: my-tenant" \
  -d '{
    "name": "purchase_frequency_30d",
    "aggregateFunction": "count",
    "sourceField": "purchase",
    "windowDays": 30,
    "computeMode": "batch",
    "batchIntervalMin": 60
  }'
Response: 201 Created

PUT /api/v1/behavioral-metrics

Update a metric definition.

Request Body

FieldTypeRequiredDescription
idstringYesMetric ID
All POST fieldsNoOnly provided fields are updated
statusstringNo"active" or "paused"
Response: 200 OK

DELETE /api/v1/behavioral-metrics

Delete a metric definition. Fails if the metric is referenced by active contact policies or qualification rules.
ParameterTypeRequiredDescription
idstringYesMetric ID (query parameter)
Response: 204 No Content

GET /api/v1/behavioral-metrics/

Get a metric definition with value count and last computed timestamp.

Response

{
  "id": "metric_001",
  "name": "purchase_frequency_30d",
  "aggregateFunction": "count",
  "sourceField": "purchase",
  "windowDays": 30,
  "computeMode": "batch",
  "_count": { "values": 4521 },
  "lastComputedAt": "2026-03-16T02:00:00.000Z"
}

POST /api/v1/behavioral-metrics//compute

Trigger computation of metric values for all customers.

Response

{
  "computed": true,
  "valueCount": 4521
}

GET /api/v1/behavioral-metrics//values

List computed metric values. Optionally filter by customer.

Query Parameters

ParameterTypeDescription
customerIdstringFilter values for a specific customer

Response

Returns top 20 values sorted by value descending.
[
  {
    "id": "mv_001",
    "metricId": "metric_001",
    "customerId": "CUST001",
    "value": 12,
    "computedAt": "2026-03-16T02:00:00.000Z"
  }
]

Roles

EndpointAllowed Roles
GET /behavioral-metricsadmin, editor, viewer
POST /behavioral-metricsadmin, editor
PUT /behavioral-metricsadmin, editor
DELETE /behavioral-metricsadmin, editor
GET /{id}admin, editor, viewer
POST /{id}/computeadmin, editor
GET /{id}/valuesadmin, editor, viewer
See also: Behavioral Metrics