> For the complete documentation index, see [llms.txt](https://docs.akenza.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.akenza.io/reference/api-documentation/querying-device-data/querying-aggregations/migrating-aggregations-v3-to-v4.md).

# Migrating from v3 to v4

Move from the deprecated /v3/ aggregation endpoints to the /v4/query API, with a v3 and a v4 example for each endpoint.

The `/v3/devices/...` and `/v3/tags/...` aggregation endpoints are deprecated. Use the `/v4/query` API instead. This page shows, for each deprecated endpoint, how to send the same query to v4. Each section has the v3 request and its v4 replacement as `curl` commands.

## Endpoint overview

| Deprecated v3 endpoint                                                                                 | v4 endpoint                                                |
| ------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------- |
| [`/v3/devices/{deviceId}/query/aggregated/hourly/kpi`](#hourly-single-metric-for-one-device)           | `/v4/query/single-metrics`                                 |
| [`/v3/devices/{deviceId}/query/aggregated/hourly/time-series`](#hourly-time-series-for-one-device)     | `/v4/query/time-series`                                    |
| [`/v3/devices/query/batch/aggregated/hourly/time-series`](#hourly-time-series-for-several-devices)     | `/v4/query/time-series/by-device`                          |
| [`/v3/devices/query/batch/aggregated/hourly/kpi`](#hourly-single-metric-for-several-devices)           | `/v4/query/single-metrics/by-device`                       |
| [`/v3/devices/{deviceId}/query/aggregated/hourly/trend-status`](#hourly-trend-status)                  | `/v4/query/trend-status`                                   |
| [`/v3/devices/{deviceId}/query/raw/kpi`](#raw-single-metric-for-one-device)                            | `/v4/query/single-metrics` with `RAW`                      |
| [`/v3/devices/{deviceId}/query/raw/time-series`](#raw-time-series-for-one-device)                      | `/v4/query/time-series` with `RAW`                         |
| [`/v3/devices/query/batch/raw/time-series`](#raw-time-series-for-several-devices)                      | `/v4/query/time-series/by-device` with `RAW`               |
| [`/v3/devices/query/batch/raw/kpi`](#raw-single-metric-for-several-devices)                            | `/v4/query/single-metrics/by-device` with `RAW`            |
| [`/v3/devices/{deviceId}/query/raw/latest-sample`](#latest-sample-for-one-device)                      | `/v4/query/single-metrics` with `LAST` and `RAW`           |
| [`/v3/devices/query/batch/raw/latest-sample`](#latest-sample-for-several-devices)                      | `/v4/query/single-metrics/by-device` with `LAST` and `RAW` |
| [`/v3/tags/{tagId}/query/aggregated/hourly/kpi`](#hourly-single-metric-per-device-of-a-tag)            | `/v4/query/single-metrics/by-device`                       |
| [`/v3/tags/{tagId}/query/aggregated/hourly/accumulated-kpi`](#hourly-single-metric-across-a-tag)       | `/v4/query/single-metrics`                                 |
| [`/v3/tags/{tagId}/query/aggregated/hourly/accumulated-time-series`](#hourly-time-series-across-a-tag) | `/v4/query/time-series`                                    |
| [`/v3/tags/{tagId}/query/raw/kpi`](#raw-single-metric-per-device-of-a-tag)                             | `/v4/query/single-metrics/by-device` with `RAW`            |
| [`/v3/tags/{tagId}/query/raw/latest-samples`](#latest-sample-per-device-of-a-tag)                      | `/v4/query/single-metrics/by-device` with `LAST` and `RAW` |
| [`/v3/tags/{tagId}/query/raw/accumulated-kpi`](#raw-single-metric-across-a-tag)                        | `/v4/query/single-metrics` with `RAW`                      |
| [`/v3/tags/{tagId}/query/raw/accumulated-time-series`](#raw-time-series-across-a-tag)                  | `/v4/query/time-series` with `RAW`                         |

All endpoints use `POST`. Four v3 endpoints have no full v4 replacement yet and stay supported. See [Endpoints that stay supported](#endpoints-that-stay-supported).

## Before you start

The examples send an [API key](https://docs.akenza.io/reference/api-documentation/authentication.md) in the `x-api-key` header. They use these shell variables:

```bash
export AKENZA_API="https://api.akenza.io"
export API_KEY="<your API key>"
export DEVICE_ID="<16 hex chars device id>"
export DEVICE_ID_2="<another device id>"
export TAG_ID="<16 hex chars tag id>"
# start of the latest sample time windows: 7 days ago (macOS/BSD date, falling back to GNU date)
export SINCE=$(date -u -v-7d +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -d "7 days ago" +%Y-%m-%dT%H:%M:%SZ)
```

## What changes

### Request body

v3 had one flat body. v4 groups the same information into two sections. `match` says which data to read. `aggregation` says how to aggregate it.

| v3 field                          | v4 field                                                                    |
| --------------------------------- | --------------------------------------------------------------------------- |
| `{deviceId}` in the path          | `match.devices.deviceIds: ["..."]`                                          |
| `deviceIds` (batch endpoints)     | `match.devices.deviceIds`                                                   |
| `{tagId}` in the path             | `match.devices.tagId: "..."`                                                |
| `interval.from` and `interval.to` | `match.timestamps.from` and `match.timestamps.to`. **Required** in v4.      |
| `topic` and `dataKey`             | `match.dataPointSelector.topic` and `match.dataPointSelector.dataKey`       |
| `accumulator`                     | `aggregation.accumulator` (simple mode). See [Accumulators](#accumulators). |
| `bucketInterval`                  | `aggregation.interval` (simple mode) or `aggregation.timeStage.interval`    |
| `filterWorkingHours`              | `flags.filterWorkingHours`                                                  |
| `/raw/` in the path               | `aggregation.sourceStage.sourceDataProduct: "RAW"`                          |
| `/aggregated/hourly/` in the path | Nothing to set. Hourly data is the default.                                 |

{% hint style="info" %}
The v3 hourly endpoints merged the values of all measurement types of a `topic` and `dataKey`. To get the same result in v4, set `match.dataPointSelector.mergeMeasurementTypes` to `true`. The hourly examples below do this.
{% endhint %}

### Response body

| v3 response                                                       | v4 response                                                                          |
| ----------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
| Single metric: `{deviceId, topic, dataKey, kpi, timestamp}`       | `{metric: {topic, dataKey, measurementType, akenzaDeviceIds}, dataPoint, timestamp}` |
| Time series: `{deviceId, topic, dataKey, dataPoints, timestamps}` | `{metric: {...}, dataPoints, timestamps}`                                            |
| Trend status: `{deviceId, topic, dataKey, trendStatus}`           | `{metric: {...}, trendStatus}`                                                       |
| Single-device endpoints return one object.                        | `{devicesQueryMode, results: [...]}`. Read `results[0]`.                             |
| Batch endpoints return a JSON array.                              | A page `{content: [...], totalElements, ...}`. Read `content`.                       |

* `kpi` is now `dataPoint`.
* The device ID moved from `deviceId` to `metric.akenzaDeviceIds`. This is a set with one entry per result.

### Accumulators

v4 has two modes:

* **Simple mode** uses one accumulator for the whole query: `aggregation.accumulator`. Use it for a v3 request that has an `accumulator` and no `summationAccumulator`.
* **Advanced mode** sets one accumulator per stage. Use it for a v3 request that has a `summationAccumulator` or reads raw data. `sourceDataProduct` only exists in advanced mode, so every raw example below sets all three stages.

In advanced mode, map the v3 accumulators to the stages as follows:

| v3                                                              | v4 `sourceStage` | v4 `timeStage` | v4 `devicesStage` |
| --------------------------------------------------------------- | ---------------- | -------------- | ----------------- |
| `accumulator: X`                                                | `X`              | `X`            | `X`               |
| `accumulator: X` (`FIRST` or `LAST`), `summationAccumulator: Y` | `X`              | `X`            | `Y`               |
| `accumulator: X`, `summationAccumulator: COUNT`                 | `X`              | `COUNT`        | `SUM`             |
| `accumulator: X`, `summationAccumulator: Y` (any other)         | `X`              | `Y`            | `Y`               |
| `accumulator: MATCHED_COUNT` or `UNMATCHED_COUNT`               | same as v3       | `SUM`          | `SUM`             |

### Behaviour differences

* **Hour alignment (hourly data):** v3 aligned `interval` to full hours without telling you, and left out the hour that contains `interval.to`. To get the same results as v3, send `from` and `to` on full hours.
* **Working hours:** if `filterWorkingHours` is not set, akenza applies it only for the `OCCUPANCY` accumulator. This is the same as in v3.
* **Raw data** (`RAW`) can cover at most 30 days. The v3 tag latest samples endpoint accepted up to one year.
* **Auto-routing:** if you do not set `sourceDataProduct`, akenza picks the source data product for you. The time range of your query affects the choice: a range shorter than one day can be routed to raw data, a longer range to hourly data.
* **Raw `single-metrics` aggregates across devices.** It therefore accepts only **one** device ID. To get one value per device, use `single-metrics/by-device`.
* **`/by-device` with `deviceIds` takes no pagination parameters** (`page`, `size`, `sort`). It returns all requested devices at once. v3 `batch/raw/kpi` accepted `?sort=`. In v4, sort the results in your application.
* **`NONE` accumulator:** the v3 hourly endpoints replaced `NONE` with `LAST` without telling you. In v4, use `LAST`. v4 rejects `NONE` together with `RAW`.

## Hourly data

### Hourly single metric for one device

Replaces `POST /v3/devices/{deviceId}/query/aggregated/hourly/kpi` with `POST /v4/query/single-metrics`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/devices/$DEVICE_ID/query/aggregated/hourly/kpi" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "interval": {
    "from": "2026-10-14T00:00:00Z",
    "to": "2026-10-16T00:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/single-metrics" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "deviceIds": ["$DEVICE_ID"] },
    "timestamps": {
      "from": "2026-10-14T00:00:00Z",
      "to": "2026-10-16T00:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature", "mergeMeasurementTypes": true }
  },
  "aggregation": { "accumulator": "AVG" }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 `kpi` is `results[0].dataPoint`.

### Hourly time series for one device

Replaces `POST /v3/devices/{deviceId}/query/aggregated/hourly/time-series` with `POST /v4/query/time-series`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/devices/$DEVICE_ID/query/aggregated/hourly/time-series" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "bucketInterval": "PT1H",
  "interval": {
    "from": "2026-10-14T00:00:00Z",
    "to": "2026-10-16T00:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/time-series" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "deviceIds": ["$DEVICE_ID"] },
    "timestamps": {
      "from": "2026-10-14T00:00:00Z",
      "to": "2026-10-16T00:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature", "mergeMeasurementTypes": true }
  },
  "aggregation": { "accumulator": "AVG", "interval": "PT1H" }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 `dataPoints` and `timestamps` are `results[0].dataPoints` and `results[0].timestamps`.

### Hourly time series for several devices

Replaces `POST /v3/devices/query/batch/aggregated/hourly/time-series` with `POST /v4/query/time-series/by-device`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/devices/query/batch/aggregated/hourly/time-series" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "deviceIds": ["$DEVICE_ID", "$DEVICE_ID_2"],
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "bucketInterval": "PT1H",
  "interval": {
    "from": "2026-10-14T00:00:00Z",
    "to": "2026-10-16T00:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/time-series/by-device" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "deviceIds": ["$DEVICE_ID", "$DEVICE_ID_2"] },
    "timestamps": {
      "from": "2026-10-14T00:00:00Z",
      "to": "2026-10-16T00:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature", "mergeMeasurementTypes": true }
  },
  "aggregation": { "accumulator": "AVG", "interval": "PT1H" }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 array is `content`, with one entry per device (`metric.akenzaDeviceIds`).

### Hourly single metric for several devices

Replaces `POST /v3/devices/query/batch/aggregated/hourly/kpi` with `POST /v4/query/single-metrics/by-device`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/devices/query/batch/aggregated/hourly/kpi" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "deviceIds": ["$DEVICE_ID", "$DEVICE_ID_2"],
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "interval": {
    "from": "2026-10-14T00:00:00Z",
    "to": "2026-10-16T00:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/single-metrics/by-device" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "deviceIds": ["$DEVICE_ID", "$DEVICE_ID_2"] },
    "timestamps": {
      "from": "2026-10-14T00:00:00Z",
      "to": "2026-10-16T00:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature", "mergeMeasurementTypes": true }
  },
  "aggregation": { "accumulator": "AVG" }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 array is `content`, with one entry per device. `kpi` is `dataPoint`.

### Hourly trend status

Replaces `POST /v3/devices/{deviceId}/query/aggregated/hourly/trend-status` with `POST /v4/query/trend-status`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/devices/$DEVICE_ID/query/aggregated/hourly/trend-status" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "bucketInterval": "PT1H",
  "interval": {
    "from": "2026-10-14T00:00:00Z",
    "to": "2026-10-16T00:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/trend-status" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "deviceIds": ["$DEVICE_ID"] },
    "timestamps": {
      "from": "2026-10-14T00:00:00Z",
      "to": "2026-10-16T00:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature", "mergeMeasurementTypes": true }
  },
  "aggregation": { "accumulator": "AVG", "interval": "PT1H" }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 `trendStatus` is `results[0].trendStatus`. Trend status accepts exactly one device ID.

## Raw data

### Raw single metric for one device

Replaces `POST /v3/devices/{deviceId}/query/raw/kpi` with `POST /v4/query/single-metrics` and `RAW`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/devices/$DEVICE_ID/query/raw/kpi" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "interval": {
    "from": "2026-10-15T08:00:00Z",
    "to": "2026-10-15T20:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/single-metrics" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "deviceIds": ["$DEVICE_ID"] },
    "timestamps": {
      "from": "2026-10-15T08:00:00Z",
      "to": "2026-10-15T20:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature" }
  },
  "aggregation": {
    "sourceStage": { "accumulator": "AVG", "sourceDataProduct": "RAW" },
    "timeStage": { "accumulator": "AVG" },
    "devicesStage": { "accumulator": "AVG" }
  }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 `kpi` is `results[0].dataPoint`.

### Raw time series for one device

Replaces `POST /v3/devices/{deviceId}/query/raw/time-series` with `POST /v4/query/time-series` and `RAW`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/devices/$DEVICE_ID/query/raw/time-series" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "bucketInterval": "PT15M",
  "interval": {
    "from": "2026-10-15T08:00:00Z",
    "to": "2026-10-15T20:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/time-series" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "deviceIds": ["$DEVICE_ID"] },
    "timestamps": {
      "from": "2026-10-15T08:00:00Z",
      "to": "2026-10-15T20:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature" }
  },
  "aggregation": {
    "sourceStage": { "accumulator": "AVG", "sourceDataProduct": "RAW" },
    "timeStage": { "accumulator": "AVG", "interval": "PT15M" },
    "devicesStage": { "accumulator": "AVG" }
  }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** `results[0].dataPoints` and `results[0].timestamps`. To get the raw samples instead of buckets, set `"timeStage": { "interval": null }`.

### Raw time series for several devices

Replaces `POST /v3/devices/query/batch/raw/time-series` with `POST /v4/query/time-series/by-device` and `RAW`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/devices/query/batch/raw/time-series" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "deviceIds": ["$DEVICE_ID", "$DEVICE_ID_2"],
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "bucketInterval": "PT15M",
  "interval": {
    "from": "2026-10-15T08:00:00Z",
    "to": "2026-10-15T20:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/time-series/by-device" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "deviceIds": ["$DEVICE_ID", "$DEVICE_ID_2"] },
    "timestamps": {
      "from": "2026-10-15T08:00:00Z",
      "to": "2026-10-15T20:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature" }
  },
  "aggregation": {
    "sourceStage": { "accumulator": "AVG", "sourceDataProduct": "RAW" },
    "timeStage": { "accumulator": "AVG", "interval": "PT15M" },
    "devicesStage": { "accumulator": "AVG" }
  }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 array is `content`, with one entry per device.

### Raw single metric for several devices

Replaces `POST /v3/devices/query/batch/raw/kpi` with `POST /v4/query/single-metrics/by-device` and `RAW`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/devices/query/batch/raw/kpi" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "deviceIds": ["$DEVICE_ID", "$DEVICE_ID_2"],
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "interval": {
    "from": "2026-10-15T08:00:00Z",
    "to": "2026-10-15T20:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/single-metrics/by-device" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "deviceIds": ["$DEVICE_ID", "$DEVICE_ID_2"] },
    "timestamps": {
      "from": "2026-10-15T08:00:00Z",
      "to": "2026-10-15T20:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature" }
  },
  "aggregation": {
    "sourceStage": { "accumulator": "AVG", "sourceDataProduct": "RAW" },
    "timeStage": { "accumulator": "AVG" },
    "devicesStage": { "accumulator": "AVG" }
  }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 array is `content`, with one entry per device. v3 accepted `?sort=`. v4 takes no pagination parameters here, so sort the results in your application.

### Latest sample for one device

Replaces `POST /v3/devices/{deviceId}/query/raw/latest-sample` with `POST /v4/query/single-metrics`, `LAST` and `RAW`.

v4 requires `timestamps`. Pick a time window of up to 30 days that contains the latest sample. In the v4 example, `from` is `$SINCE` (7 days ago) and the shell fills in the current time for `to`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/devices/$DEVICE_ID/query/raw/latest-sample" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "LAST"
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/single-metrics" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "deviceIds": ["$DEVICE_ID"] },
    "timestamps": {
      "from": "$SINCE",
      "to": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature" }
  },
  "aggregation": {
    "sourceStage": { "accumulator": "LAST", "sourceDataProduct": "RAW" },
    "timeStage": { "accumulator": "LAST" },
    "devicesStage": { "accumulator": "LAST" }
  }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** v3 returned an array with one entry. In v4, read `results[0]`. It contains the `dataPoint` and `timestamp` of the latest sample.

### Latest sample for several devices

Replaces `POST /v3/devices/query/batch/raw/latest-sample` with `POST /v4/query/single-metrics/by-device`, `LAST` and `RAW`. The same time window rule applies as for [one device](#latest-sample-for-one-device).

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/devices/query/batch/raw/latest-sample" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "deviceIds": ["$DEVICE_ID", "$DEVICE_ID_2"],
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "LAST"
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/single-metrics/by-device" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "deviceIds": ["$DEVICE_ID", "$DEVICE_ID_2"] },
    "timestamps": {
      "from": "$SINCE",
      "to": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature" }
  },
  "aggregation": {
    "sourceStage": { "accumulator": "LAST", "sourceDataProduct": "RAW" },
    "timeStage": { "accumulator": "LAST" },
    "devicesStage": { "accumulator": "LAST" }
  }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 array is `content`, with one entry per device.

## Tags

In v4, you select the devices of a tag with `match.devices.tagId` instead of the `{tagId}` path segment.

The per-device tag endpoints (`.../by-device`) return one page of the devices of the tag at a time. They **require** the `page` and `size` query parameters.

### Hourly single metric per device of a tag

Replaces `POST /v3/tags/{tagId}/query/aggregated/hourly/kpi` with `POST /v4/query/single-metrics/by-device`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/tags/$TAG_ID/query/aggregated/hourly/kpi?page=0&size=20" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "interval": {
    "from": "2026-10-14T00:00:00Z",
    "to": "2026-10-16T00:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/single-metrics/by-device?page=0&size=20" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "tagId": "$TAG_ID" },
    "timestamps": {
      "from": "2026-10-14T00:00:00Z",
      "to": "2026-10-16T00:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature", "mergeMeasurementTypes": true }
  },
  "aggregation": { "accumulator": "AVG" }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the page structure is the same as in v3. In each entry of `content`, `kpi` is now `dataPoint`.

### Hourly single metric across a tag

Replaces `POST /v3/tags/{tagId}/query/aggregated/hourly/accumulated-kpi` with `POST /v4/query/single-metrics`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/tags/$TAG_ID/query/aggregated/hourly/accumulated-kpi" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "interval": {
    "from": "2026-10-14T00:00:00Z",
    "to": "2026-10-16T00:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/single-metrics" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "tagId": "$TAG_ID" },
    "timestamps": {
      "from": "2026-10-14T00:00:00Z",
      "to": "2026-10-16T00:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature", "mergeMeasurementTypes": true }
  },
  "aggregation": { "accumulator": "AVG" }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 `kpi` is `results[0].dataPoint`.

### Hourly time series across a tag

Replaces `POST /v3/tags/{tagId}/query/aggregated/hourly/accumulated-time-series` with `POST /v4/query/time-series`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/tags/$TAG_ID/query/aggregated/hourly/accumulated-time-series" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "bucketInterval": "PT1H",
  "interval": {
    "from": "2026-10-14T00:00:00Z",
    "to": "2026-10-16T00:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/time-series" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "tagId": "$TAG_ID" },
    "timestamps": {
      "from": "2026-10-14T00:00:00Z",
      "to": "2026-10-16T00:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature", "mergeMeasurementTypes": true }
  },
  "aggregation": { "accumulator": "AVG", "interval": "PT1H" }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 `dataPoints` and `timestamps` are `results[0].dataPoints` and `results[0].timestamps`.

### Raw single metric per device of a tag

Replaces `POST /v3/tags/{tagId}/query/raw/kpi` with `POST /v4/query/single-metrics/by-device` and `RAW`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/tags/$TAG_ID/query/raw/kpi?page=0&size=20" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "interval": {
    "from": "2026-10-15T08:00:00Z",
    "to": "2026-10-15T20:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/single-metrics/by-device?page=0&size=20" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "tagId": "$TAG_ID" },
    "timestamps": {
      "from": "2026-10-15T08:00:00Z",
      "to": "2026-10-15T20:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature" }
  },
  "aggregation": {
    "sourceStage": { "accumulator": "AVG", "sourceDataProduct": "RAW" },
    "timeStage": { "accumulator": "AVG" },
    "devicesStage": { "accumulator": "AVG" }
  }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the page structure is the same as in v3. `kpi` is now `dataPoint`.

### Latest sample per device of a tag

Replaces `POST /v3/tags/{tagId}/query/raw/latest-samples` with `POST /v4/query/single-metrics/by-device`, `LAST` and `RAW`. The same time window rule applies as for [one device](#latest-sample-for-one-device): up to 30 days.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/tags/$TAG_ID/query/raw/latest-samples?page=0&size=20" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "LAST",
  "interval": {
    "from": "$SINCE",
    "to": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/single-metrics/by-device?page=0&size=20" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "tagId": "$TAG_ID" },
    "timestamps": {
      "from": "$SINCE",
      "to": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature" }
  },
  "aggregation": {
    "sourceStage": { "accumulator": "LAST", "sourceDataProduct": "RAW" },
    "timeStage": { "accumulator": "LAST" },
    "devicesStage": { "accumulator": "LAST" }
  }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the page structure is the same as in v3. `kpi` is now `dataPoint`.

### Raw single metric across a tag

Replaces `POST /v3/tags/{tagId}/query/raw/accumulated-kpi` with `POST /v4/query/single-metrics` and `RAW`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/tags/$TAG_ID/query/raw/accumulated-kpi" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "interval": {
    "from": "2026-10-15T08:00:00Z",
    "to": "2026-10-15T20:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/single-metrics" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "tagId": "$TAG_ID" },
    "timestamps": {
      "from": "2026-10-15T08:00:00Z",
      "to": "2026-10-15T20:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature" }
  },
  "aggregation": {
    "sourceStage": { "accumulator": "AVG", "sourceDataProduct": "RAW" },
    "timeStage": { "accumulator": "AVG" },
    "devicesStage": { "accumulator": "AVG" }
  }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 `kpi` is `results[0].dataPoint`. For raw tag queries, `metric.akenzaDeviceIds` is empty.

### Raw time series across a tag

Replaces `POST /v3/tags/{tagId}/query/raw/accumulated-time-series` with `POST /v4/query/time-series` and `RAW`.

{% tabs %}
{% tab title="v3" %}

```bash
curl -X POST "$AKENZA_API/v3/tags/$TAG_ID/query/raw/accumulated-time-series" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "topic": "climate",
  "dataKey": "temperature",
  "accumulator": "AVG",
  "bucketInterval": "PT15M",
  "interval": {
    "from": "2026-10-15T08:00:00Z",
    "to": "2026-10-15T20:00:00Z"
  }
}
EOF
```

{% endtab %}

{% tab title="v4" %}

```bash
curl -X POST "$AKENZA_API/v4/query/time-series" \
  -H "x-api-key: $API_KEY" \
  -H "Content-Type: application/json" \
  --data @- <<EOF
{
  "match": {
    "devices": { "tagId": "$TAG_ID" },
    "timestamps": {
      "from": "2026-10-15T08:00:00Z",
      "to": "2026-10-15T20:00:00Z"
    },
    "dataPointSelector": { "topic": "climate", "dataKey": "temperature" }
  },
  "aggregation": {
    "sourceStage": { "accumulator": "AVG", "sourceDataProduct": "RAW" },
    "timeStage": { "accumulator": "AVG", "interval": "PT15M" },
    "devicesStage": { "accumulator": "AVG" }
  }
}
EOF
```

{% endtab %}
{% endtabs %}

**Response:** the v3 `dataPoints` and `timestamps` are `results[0].dataPoints` and `results[0].timestamps`. For raw tag queries, `metric.akenzaDeviceIds` is empty.

## Endpoints that stay supported

These v3 endpoints have no full v4 replacement yet. They stay supported:

* `POST /v3/devices/{deviceId}/query/raw/trend-status`: `/v4/query/trend-status` only supports hourly data for now.
* `POST /v3/devices/query/batch/raw/accumulated-kpi`: `/v4/query/single-metrics` with `RAW` accepts one device ID (or a `tagId`), not a list of devices.
* `POST /v3/tags/{tagId}/query/latest-samples`: it returns whole samples (all data keys of a topic) and supports `assetPropertyFilters`. v4 always selects one `topic` and `dataKey`.
* `POST /v3/tags/{tagId}/query/aggregate/latest-samples`: v4 has no way to group or match latest samples.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.akenza.io/reference/api-documentation/querying-device-data/querying-aggregations/migrating-aggregations-v3-to-v4.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
