# 查询积分分析

Operation / SDK: `readCreditAnalytics` · `read_credit_analytics` · `ReadCreditAnalytics`

查询由余额与财务记录得出的积分分析。

更新于 2026-11-06

[网页版本](https://www.longplayai.com/zh-hans/docs/api/read-credit-analytics)

<a id="request"></a>

## 请求

- **凭据**: Server Key，权限 `credits:read`。只在后端调用。
- **SDK**: JavaScript `createLongplayServer().readCreditAnalytics` · Python `read_credit_analytics` · Go `ReadCreditAnalytics`
- **成功状态**: HTTP 200

响应为示例数据。

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/credits/analytics" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "customerId": null,
  "query": {
    "range": {
      "kind": "absolute",
      "from": "2026-10-01T00:00:00.000Z",
      "to": "2026-10-03T12:00:00.000Z"
    },
    "timezone": "UTC",
    "filterBy": [],
    "interval": "day",
    "internal_feature_id": null,
    "groupBy": "customer",
    "entityId": null,
    "topN": null,
    "after": null,
    "limit": 50
  }
}'
```

**TypeScript: readCreditAnalytics.ts**

```ts
import { createLongplayServer } from "@tinkerlandai/longplay-sdk/server";

const longplay = createLongplayServer({
  baseUrl: process.env.LONGPLAY_API_URL!,
  apiKey: process.env.LONGPLAY_SERVER_KEY!,
  scope: { projectId: process.env.LONGPLAY_PROJECT_ID!, environment: "sandbox" },
});

export async function readCreditAnalytics() {
  return longplay.readCreditAnalytics({
    customerId: null,
    query: {
      range: {
        kind: "absolute",
        from: "2026-10-01T00:00:00.000Z",
        to: "2026-10-03T12:00:00.000Z",
      },
      timezone: "UTC",
      filterBy: [],
      interval: "day",
      internal_feature_id: null,
      groupBy: "customer",
      entityId: null,
      topN: null,
      after: null,
      limit: 50,
    },
  });
}
```

**请求体: request.json**

```json
{
  "customerId": null,
  "query": {
    "range": {
      "kind": "absolute",
      "from": "2026-10-01T00:00:00.000Z",
      "to": "2026-10-03T12:00:00.000Z"
    },
    "timezone": "UTC",
    "filterBy": [],
    "interval": "day",
    "internal_feature_id": null,
    "groupBy": "customer",
    "entityId": null,
    "topN": null,
    "after": null,
    "limit": 50
  }
}
```

**响应示例: response.json**

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "analytics": {
    "from": "2026-10-03T12:00:00.000Z",
    "to": "2026-10-03T12:00:00.000Z",
    "interval": "hour",
    "timezone": "UTC",
    "rows": [
      {
        "bucket": "2026-10-03T12:00:00.000Z",
        "group": "1Example",
        "isOther": true,
        "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
        "resourceId": "79322b17-a312-485e-8033-b532cdc16fe9",
        "consumedUnits": "-1",
        "refundedUnits": "-1",
        "reservedUnits": "-1",
        "charged": "-1",
        "refunded": "-1",
        "reserved": "-1",
        "netUnits": "-1",
        "netCharged": "-1"
      }
    ],
    "nextCursor": {
      "bucket": "2026-10-03T12:00:00.000Z",
      "group": "1Example",
      "isOther": true,
      "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
      "resourceId": "79322b17-a312-485e-8033-b532cdc16fe9"
    }
  }
}
```

<a id="parameters"></a>

## 请求体参数

**application/json**

- `customerId` (uuid | null, required): Customer 的 UUID。用 Server Key 调用时指定 Customer；Customer Token 调用时省略。
- `query` (object, required): 查询条件。
- `query.range` (object | object | object, required): 时间范围。
- `query.range.kind` ("absolute" | "last_days" | "billing_cycles", required): 类型，取值见允许值。
- `query.range.from` (date-time, optional): 时间范围起点（ISO 8601）。
- `query.range.to` (date-time, optional): 时间范围终点（ISO 8601）。
- `query.range.days` (1 | 7 | 30 | 90, optional): 天数。
- `query.range.membershipId` (uuid, optional): 会员的 ID。
- `query.range.count` (integer, optional): `billing_cycles` 范围要覆盖的该会员资格最近计费周期数（1–12）；`includeCurrent` 为 `false` 时只计已结束的周期。 min: 1 · max: 12
- `query.range.includeCurrent` (boolean, optional): 是否包含当前周期。
- `query.timezone` (string, required): 时区。 maxLength: 100
- `query.filterBy` (array, required): 筛选条件。 maxItems: 5
- `query.filterBy.key` (string, required): 用于筛选的事件属性名（小写字母、数字和下划线）；只统计该属性值等于 `values` 之一的事件。 pattern: ^[a-z][a-z0-9_]{0,63}$
- `query.filterBy.values` (array, required): 值列表。 minItems: 1 · maxItems: 20
- `query.interval` (enum, required): 周期，取值见允许值。 Values: `"hour"`, `"day"`, `"week"`, `"month"`.
- `query.internal_feature_id` (uuid | null, required): Feature 的 ID。
- `query.entityId` (uuid | null, required): 当前客户范围内的 Entity ID；只用于已配置的 Entity 和许可 Feature。
- `query.topN` (integer | null, required): 返回前 N 项。
- `query.groupBy` (enum | string, required): 分组维度。
- `query.after` (object | null, required): 分页游标：传上一页返回的值获取下一页；首页传 `null`。
- `query.after.bucket` (date-time, required): 统计分桶。
- `query.after.group` (string, required): 分组。 maxLength: 240
- `query.after.isOther` (boolean, required): 是否为“其他”汇总项。
- `query.after.internal_feature_id` (uuid, required): Feature 的 ID。
- `query.after.resourceId` (uuid, required): 游标字段：上一页 `nextCursor` 中的 `resourceId`。原样传回整个游标对象。
- `query.limit` (integer, required): 一页返回的最大记录数量，受契约上限限制。 min: 1 · max: 100

<a id="response"></a>

## 响应字段

成功响应都包含 `requestId`（本次请求 ID，排查时提供）和 `scope`（App 与环境）。

**HTTP 200 · application/json**

- `requestId` (uuid, required): 服务端为这次 HTTP 请求生成的 UUID，排查时提供；它不是你的业务 requestId。
- `scope` (object, required): 响应实际所属的 App 和环境。Header 只能核对，不能切换 Key 范围。
- `scope.projectId` (string, required): 凭据绑定的 App ID。 minLength: 1
- `scope.environment` (enum, required): 独立数据范围：sandbox 或 live。 Values: `"sandbox"`, `"live"`.
- `analytics` (object, required): 报表分页结果：实际生效的 `from`/`to` 时间范围、`interval` 和 `timezone`，聚合后的 `rows`，以及用于下一页的 `nextCursor`。
- `analytics.from` (date-time, required): 时间范围起点（ISO 8601）。
- `analytics.to` (date-time, required): 时间范围终点（ISO 8601）。
- `analytics.interval` (enum, required): 周期，取值见允许值。 Values: `"hour"`, `"day"`, `"week"`, `"month"`.
- `analytics.timezone` (string, required): 时区。 maxLength: 100
- `analytics.rows` (array, required): 数据行。 maxItems: 100
- `analytics.rows.bucket` (date-time, required): 统计分桶。
- `analytics.rows.group` (string, required): 分组。 maxLength: 240
- `analytics.rows.isOther` (boolean, required): 是否为“其他”汇总项。
- `analytics.rows.internal_feature_id` (uuid, required): Feature 的 ID。
- `analytics.rows.resourceId` (uuid, required): 为这笔用量扣减余额的 Feature 的 UUID：用积分支付时为对应的积分 Feature，否则与 `internal_feature_id` 相同。
- `analytics.rows.consumedUnits` (string, required): 本分桶内消耗的数量（含已确认的预留），以所记录 Feature（`internal_feature_id`）的单位计；十进制字符串。 pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.refundedUnits` (string, required): 已退还的数量，以所记录 Feature 的单位计；十进制字符串。退还计入原始用量所在的分桶。 pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.reservedUnits` (string, required): 当前仍待确认且未过期的预留所占用的数量，以所记录 Feature 的单位计；十进制字符串。 pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.charged` (string, required): 消耗从 `resourceId` 余额中扣减的数量，以该余额的单位计（积分 Feature 即为积分数）；十进制字符串。 pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.refunded` (string, required): 已退款部分。 pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.reserved` (string, required): 已预留的数量。 pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.netUnits` (string, required): `consumedUnits` 减去 `refundedUnits`；十进制字符串，不含预留。 pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.rows.netCharged` (string, required): `charged` 减去 `refunded`，即从 `resourceId` 余额中净扣减的数量；十进制字符串。 pattern: ^-?(0|[1-9]\d{0,23})(\.\d{1,12})?$
- `analytics.nextCursor` (object | null, required): 下一页游标；null 表示结束。原样传回，改筛选条件时清空。
- `analytics.nextCursor.bucket` (date-time, required): 统计分桶。
- `analytics.nextCursor.group` (string, required): 分组。 maxLength: 240
- `analytics.nextCursor.isOther` (boolean, required): 是否为“其他”汇总项。
- `analytics.nextCursor.internal_feature_id` (uuid, required): Feature 的 ID。
- `analytics.nextCursor.resourceId` (uuid, required): 本页最后一行的 `resourceId`；属于游标的一部分，原样作为 `query.after` 传回。

<a id="errors"></a>

## 错误

失败时返回错误对象，按 `error.code` 与 `error.recovery` 处理。下表是这个接口的凭据、作用域和幂等字段决定的错误；视请求内容还可能返回 `NOT_FOUND`（引用的资源不存在）、`NOT_CONFIGURED`（所需的集成未连接）或 `PAYLOAD_TOO_LARGE`。有些拒绝（如余额不足）不是错误，而是成功响应里的 `reason`。

**可能的错误**

| code | HTTP | 何时 | recovery |
| --- | --- | --- | --- |
| `INVALID_REQUEST` | 400 | 请求不符合本页的参数。缺少或无法解析协议头时分别返回 `UNSUPPORTED_PROTOCOL_VERSION` 或 `INVALID_PROTOCOL_VERSION`。 | `fix_request` |
| `UNAUTHENTICATED` | 401 | 凭据缺失、无效、过期或已撤销。 | `authenticate` |
| `FORBIDDEN` | 403 | 凭据没有 `credits:read` 权限。 | `check_permissions` |
| `SCOPE_MISMATCH` | 403 | `X-Indiana-Project` 或 `X-Indiana-Environment` 与凭据所属的 App 和环境不一致。 | `check_scope` |
| `RATE_LIMITED` | 429 | 请求过多；按 `Retry-After` 等待后用同一请求重试。 | `retry_same_request` |
| `SERVICE_UNAVAILABLE` | 503 | 服务暂时不可用；用同一请求重试。 | `retry_same_request` |
| `INTERNAL_ERROR` | 500 | 结果未知；稍后重试。 | `recover_operation` |

- [错误码与恢复](https://www.longplayai.com/zh-hans/docs/api/errors.md)
- [请求约定](https://www.longplayai.com/zh-hans/docs/api.md)
- [全部接口](https://www.longplayai.com/zh-hans/docs/api/operations.md#credits)
