# 查询积分对账单

Operation / SDK: `readCreditStatement` · `read_credit_statement` · `ReadCreditStatement`

按时间区间和快照查询一致的积分对账单。

更新于 2026-11-06

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

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

## 请求

- **凭据**: Server Key（传 `customerId`）或 Customer Token，权限 `credits:read`。
- **SDK**: JavaScript `createLongplayServer().readCreditStatement` · JavaScript `createLongplay().readCreditStatement` · Python `read_credit_statement` · Go `ReadCreditStatement`
- **成功状态**: HTTP 200

响应为示例数据。

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/credit-statements/read" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
  "from": "2026-10-01T00:00:00Z",
  "to": "2026-11-01T00:00:00Z",
  "snapshotAt": null,
  "after": null,
  "limit": 50
}'
```

**TypeScript: readCreditStatement.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 readCreditStatement() {
  return longplay.readCreditStatement({
    customerId: "8b258f8b-4228-40fa-8d24-5b16428e686d",
    from: "2026-10-01T00:00:00Z",
    to: "2026-11-01T00:00:00Z",
    snapshotAt: null,
    after: null,
    limit: 50,
  });
}
```

**请求体: request.json**

```json
{
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
  "from": "2026-10-01T00:00:00Z",
  "to": "2026-11-01T00:00:00Z",
  "snapshotAt": null,
  "after": null,
  "limit": 50
}
```

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

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "statement": {
    "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
    "entityId": "3c287fb0-fbf3-4e56-8a3b-0cad92605411",
    "from": "2026-10-03T12:00:00.000Z",
    "to": "2026-10-03T12:00:00.000Z",
    "snapshotAt": "2026-10-03T12:00:00.000Z",
    "totals": [
      {
        "internal_credit_feature_id": "e6b730c5-64ec-4232-8fd2-a24ce9a6b5c9",
        "name": "Pro",
        "consumed": "1",
        "refunded": "1",
        "net": "-1"
      }
    ],
    "rows": [
      {
        "operationId": "a8b14e0c-6576-49a4-812c-7a1587e55255",
        "position": 0,
        "kind": "consume",
        "originalId": "f9cc89b8-f161-49f2-84d5-7e1109f316ad",
        "occurredAt": "2026-10-03T12:00:00.000Z",
        "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
        "entitlementName": "1Example",
        "sourceId": "31b22d42-ce50-47ea-8da0-553fd0a8a869",
        "internal_credit_feature_id": "e6b730c5-64ec-4232-8fd2-a24ce9a6b5c9",
        "creditName": "1Example",
        "units": "1",
        "amount": "1",
        "pricing": [
          {
            "internal_credit_feature_id": "e6b730c5-64ec-4232-8fd2-a24ce9a6b5c9",
            "internal_metered_feature_id": "977890b8-84f9-4320-8e7b-f6b142cf2d15",
            "dimension": "1Example",
            "counterKey": "1Example",
            "cycleStart": "2026-10-03T12:00:00.000Z",
            "unitCost": "1",
            "units": "1",
            "amount": "1",
            "tier": 1
          }
        ],
        "properties": {}
      }
    ],
    "nextCursor": {
      "at": "2026-10-03T12:00:00.000Z",
      "id": "82072b10-12fb-46f6-83a9-1e51025445ed",
      "position": 0
    }
  }
}
```

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

## 请求体参数

**application/json**

- `from` (date-time, required): 时间范围起点（ISO 8601）。
- `to` (date-time, required): 时间范围终点（ISO 8601）。
- `entityId` (uuid, optional): 当前客户范围内的 Entity ID；只用于已配置的 Entity 和许可 Feature。
- `snapshotAt` (date-time | null, required): 对账单快照时间；翻页时传首页返回的值，首页传 `null`。
- `after` (object | null, required): 分页游标：传上一页返回的值获取下一页；首页传 `null`。
- `after.at` (date-time, required): 游标字段：上一页最后一行的发生时间（ISO 8601）。原样传回上一页的 `nextCursor`。
- `after.id` (uuid, required): 游标字段：上一页最后一行的 ID（账本中为条目 `id`，积分对账单中为 `operationId`）。原样传回上一页的 `nextCursor`。
- `after.position` (integer, required): 位置。 min: 0 · max: 9007199254740991
- `limit` (integer, required): 一页返回的最大记录数量，受契约上限限制。 min: 1 · max: 100
- `customerId` (uuid, optional): Customer 的 UUID。

<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"`.
- `statement` (object, required): 截至 `snapshotAt`、[`from`, `to`) 区间内的积分对账单，范围为 Customer 自身的用量；指定 `entityId` 时为该 Entity 的用量。包含按积分 Feature 的合计和一页扣减了积分的用量操作。
- `statement.customerId` (uuid, required): Customer 的 UUID。
- `statement.entityId` (uuid | null, required): 当前客户范围内的 Entity ID；只用于已配置的 Entity 和许可 Feature。
- `statement.from` (date-time, required): 时间范围起点（ISO 8601）。
- `statement.to` (date-time, required): 时间范围终点（ISO 8601）。
- `statement.snapshotAt` (date-time, required): 对账单快照时间；翻页时传首页返回的值，首页传 `null`。
- `statement.totals` (array, required): 整个区间按积分 Feature 汇总的合计，不限于本页。 maxItems: 1000
- `statement.totals.internal_credit_feature_id` (uuid, required): 积分 Feature 的 ID。
- `statement.totals.name` (string, required): 展示名称，不作为唯一标识。
- `statement.totals.consumed` (string, required): 区间内因消耗和已确认预留而扣减的积分数；十进制字符串。 pattern: ^(0|[1-9]\d{0,12})(\.\d{1,12})?$
- `statement.totals.refunded` (string, required): 已退款部分。 pattern: ^(0|[1-9]\d{0,12})(\.\d{1,12})?$
- `statement.totals.net` (string, required): `consumed` 减去 `refunded`；十进制字符串。 pattern: ^-?(0|[1-9]\d{0,17})(\.\d{1,12})?$
- `statement.rows` (array, required): 数据行。 maxItems: 100
- `statement.rows.operationId` (uuid, required): 业务操作 UUID；订阅执行引用 previewAttach 返回的 operationId，余额退款引用原消费操作。
- `statement.rows.position` (integer, required): 位置。 min: 0 · max: 9007199254740991
- `statement.rows.kind` (enum, required): 类型，取值见允许值。 Values: `"consume"`, `"confirm"`, `"refund"`.
- `statement.rows.originalId` (uuid | null, required): 原始记录的 ID。
- `statement.rows.occurredAt` (date-time, required): 时间（ISO 8601）。
- `statement.rows.internal_feature_id` (uuid, required): Feature 的 ID。
- `statement.rows.entitlementName` (string, required): 记录该用量的 Feature 的名称（未设名称时为其 key）。
- `statement.rows.sourceId` (uuid, required): 余额来源的 ID。
- `statement.rows.internal_credit_feature_id` (uuid, required): 积分 Feature 的 ID。
- `statement.rows.creditName` (string, required): 被扣减的积分 Feature 的名称（未设名称时为其 key）。
- `statement.rows.units` (string, required): 数量，十进制字符串。 pattern: ^(0|[1-9]\d{0,17})(\.\d{1,12})?$
- `statement.rows.amount` (string, required): 积分或账本金额，按字段的十进制字符串或整数类型传入。 pattern: ^(0|[1-9]\d{0,12})(\.\d{1,12})?$
- `statement.rows.pricing` (array, required): 计价明细。 maxItems: 12
- `statement.rows.pricing.internal_credit_feature_id` (uuid | null, required): 积分 Feature 的 ID。
- `statement.rows.pricing.internal_metered_feature_id` (uuid, required): 计量 Feature 的 ID。
- `statement.rows.pricing.dimension` (string, required): 计价维度。 minLength: 1 · maxLength: 64
- `statement.rows.pricing.counterKey` (string, required): 计数器键。 minLength: 1 · maxLength: 240
- `statement.rows.pricing.cycleStart` (date-time, required): 周期开始时间。
- `statement.rows.pricing.unitCost` (string, required): 单价。 pattern: ^(0|[1-9]\d{0,11})(\.\d{1,6})?$
- `statement.rows.pricing.units` (string, required): 数量，十进制字符串。 pattern: ^(0|[1-9]\d{0,11})(\.\d{1,6})?$
- `statement.rows.pricing.amount` (string, required): 积分或账本金额，按字段的十进制字符串或整数类型传入。 pattern: ^(0|[1-9]\d{0,12})(\.\d{1,12})?$
- `statement.rows.pricing.tier` (integer | null, required): 阶梯档位。
- `statement.rows.properties` (object, required): 服务端校验的计价属性，用于匹配维度和倍率。
- `statement.nextCursor` (object | null, required): 下一页游标；null 表示结束。原样传回，改筛选条件时清空。
- `statement.nextCursor.at` (date-time, required): 本页最后一行的发生时间（ISO 8601）；属于游标的一部分，原样作为 `after` 传回。
- `statement.nextCursor.id` (uuid, required): 本页最后一行的 ID。读取下一页时，原样传回整个 `nextCursor` 对象。
- `statement.nextCursor.position` (integer, required): 位置。 min: 0 · max: 9007199254740991

<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)
