# 查询费用趋势

```{raw} html
<div class="mo-api-page-show-toc" aria-hidden="true"></div>
```

按小时、天或月汇总计费用量费用，并按计量项、工作区或服务分组。

```text
GET https://billing.moi.matrixorigin.cn/api/v1/billing/accounts/$ACCOUNT_ID/cost-trends
```

## 调用前准备

1. 准备计费账户所属用户的有效[个人访问令牌](../../../../../guides/genesis/api-keys.md#创建和管理个人访问令牌)。
2. 调用[查询当前账户](../accounts/current.md)接口，获取目标账户的 ID。

## 请求示例

将示例中的 `$MOI_PERSONAL_ACCESS_TOKEN` 和 `$ACCOUNT_ID` 分别替换为个人访问令牌和所选账户的 ID。

```bash
curl -X GET \
  "https://billing.moi.matrixorigin.cn/api/v1/billing/accounts/$ACCOUNT_ID/cost-trends?granularity=month&dimension=meter" \
  -H "X-API-Key: $MOI_PERSONAL_ACCESS_TOKEN"
```

## 路径参数

::::{div} mo-api-parameter-table

| 参数 | 类型 | 是否必填 | 说明 |
| --- | --- | --- | --- |
| `accountID` | string | 是 | 目标账户的 ID，取自“查询当前账户”响应中的 `items[].billing_account_id`。 |

::::

## 查询参数

::::{div} mo-api-parameter-table

| 参数 | 类型 | 是否必填 | 说明 |
| --- | --- | --- | --- |
| `from` | string | 否 | 按记录创建时间筛选的起点，包含该时刻；支持 RFC 3339、`YYYY-MM-DD` 或 Unix 秒字符串。省略时从包含当天在内的最近 30 天起点开始。 |
| `to` | string | 否 | 按记录创建时间筛选的终点，不包含该时刻。日期值扩展到所选日期的次日零点。省略时使用计费时区的明日零点。 |
| `timezone` | string | 否 | IANA 时区；省略时使用账户地区的计费时区。 |
| `granularity` | string | 否 | 时间粒度：hour、day、month，默认 day。 |
| `dimension` | string | 否 | 分组维度：none（不分组）、meter（计量项）、workspace（工作区）、service（服务），默认 meter。 |
| `period` | string | 否 | 按入账账期筛选，格式为 `YYYY-MM`。 |
| `meter_code` | string | 否 | 按计量项代码筛选。 |
| `billing_item_code` | string | 否 | 按计费项代码筛选；与 `meter_code` 同时提供时须匹配同一计量项。 |
| `workspace_id` | string | 否 | 按用量事件所属工作区筛选。 |
| `status` | string | 否 | 按用量记录状态筛选；查询已扣费记录时使用 charged。省略时不按状态筛选。 |
| `subject_type` | string | 否 | 按账户归属主体类型筛选。 |
| `subject_id` | string | 否 | 按账户归属主体 ID 筛选。 |
| `q` | string | 否 | 搜索计费用量记录 ID、用量事件 ID、计量项代码或事件键，不区分大小写。 |
| `search` | string | 否 | q 的兼容参数，仅在 q 为空时使用。 |

::::

## 成功响应

请求成功时返回 HTTP `200` 和按时间和所选维度分组的费用趋势。

:::::::{div} mo-api-tabs mo-api-response-tabs
::::::{tab-set}
:::::{tab-item} 响应示例

```json
{
  "items": [
    {
      "bucket_start": "2026-09-01T00:00:00+08:00",
      "bucket_end": "2026-10-01T00:00:00+08:00",
      "bucket_start_unix": 1788192000,
      "bucket_end_unix": 1790784000,
      "dimension": "meter",
      "group_key": "meter_example",
      "group_label": "Example usage",
      "meter_code": "meter_example",
      "quantity": "1000",
      "gross_credit": "1.000000000000",
      "discount_credit": "0.100000000000",
      "net_credit": "0.900000000000",
      "source_record_ids": [
        "rated_record_example"
      ],
      "source_record_count": 1
    }
  ],
  "total_credit": "0.900000000000",
  "granularity": "month",
  "dimension": "meter",
  "timezone": "Asia/Shanghai",
  "from": "2026-09-01T00:00:00+08:00",
  "to": "2026-10-01T00:00:00+08:00"
}
```

:::::
:::::{tab-item} 字段说明

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `items` | array of object | 费用趋势。 |
| `items[].bucket_start` | string | 时间桶起点，RFC 3339 时间。 |
| `items[].bucket_end` | string | 时间桶终点，不包含该时刻。 |
| `items[].bucket_start_unix` | integer | 时间桶起点，Unix 秒。 |
| `items[].bucket_end_unix` | integer | 时间桶终点，Unix 秒。 |
| `items[].dimension` | string | 聚合维度。 |
| `items[].group_key` | string | 分组标识。 |
| `items[].group_label` | string | 分组显示名称。 |
| `items[].meter_code` | string | 计量项代码。 |
| `items[].workspace_id` | string | 按工作区聚合时的工作区 ID，存在时返回。 |
| `items[].service_code` | string | 按服务聚合时的服务代码，存在时返回。 |
| `items[].quantity` | string | 分组内的用量合计。 |
| `items[].gross_credit` | string | 折扣前费用，单位为 Credit。 |
| `items[].discount_credit` | string | 折扣抵扣，单位为 Credit。 |
| `items[].net_credit` | string | 折扣后费用，单位为 Credit。 |
| `items[].source_record_ids` | array of string | 参与聚合的计费用量记录 ID。 |
| `items[].source_record_count` | integer | 参与聚合的记录数。 |
| `total_credit` | string | 返回各组折扣后费用之和，单位为 Credit。 |
| `granularity` | string | 实际使用的时间粒度。 |
| `dimension` | string | 实际使用的分组维度。 |
| `timezone` | string | 实际使用的 IANA 时区。 |
| `from` | string | 实际查询起点，包含该时刻。 |
| `to` | string | 实际查询终点，不包含该时刻。 |

字段路径中的 `[]` 表示数组中的每一项。例如，`items[].bucket_start` 表示数组中每一项的对应字段。

:::::
::::::
:::::::

## 错误响应

时间粒度不受支持时返回 HTTP `400`，请修改查询参数。

:::::::{div} mo-api-tabs mo-api-response-tabs
::::::{tab-set}
:::::{tab-item} 响应示例

```json
{
  "code": "invalid_request",
  "message": "invalid request",
  "error": "invalid request"
}
```

:::::
:::::{tab-item} 字段说明

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `code` | string | 错误代码。 |
| `message` | string | 错误说明。 |
| `error` | string | 兼容错误说明，与 message 内容相同。 |

:::::
::::::
:::::::
