# 查询会员、购买与余额

Operation / SDK: `readHoldings` · `read_holdings` · `ReadHoldings`

读取 Customer 或 Entity 的会员、购买和余额事实。

更新于 2026-11-06

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

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

## 请求

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

响应为示例数据。

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/customer-holdings" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d"
}'
```

**TypeScript: readHoldings.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 readHoldings() {
  return longplay.readHoldings({
    customerId: "8b258f8b-4228-40fa-8d24-5b16428e686d",
  });
}
```

**请求体: request.json**

```json
{
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d"
}
```

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

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "holdings": {
    "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
    "snapshotAt": "2026-10-03T12:00:00.000Z",
    "memberships": [
      {
        "id": "5c22044b-a5dd-49fe-83bf-c52eff327451",
        "productId": "1Example",
        "productVersionId": "8c489a1a-5542-44fe-8e80-da2f6a319d01",
        "billingProductVersionId": "aaef229f-4c54-455a-868b-3194e5e1c641",
        "version": 1,
        "key": "1Example",
        "name": "Pro",
        "groupId": null,
        "membershipType": "main",
        "status": "trial",
        "cancelAtPeriodEnd": true,
        "accessEnabled": true,
        "renewalChange": null,
        "period": null
      }
    ],
    "membershipsLimited": true,
    "balances": [
      {
        "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
        "key": "1Example",
        "name": "Pro",
        "kind": "access",
        "usageType": null,
        "accessible": true,
        "unlimited": true,
        "available": "1",
        "held": "1",
        "debt": "1",
        "nextExpiresAt": null,
        "usage": null
      }
    ],
    "balancesLimited": true
  }
}
```

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

## 请求体参数

**application/json**

- `customerId` (uuid, optional): Customer 的 UUID。
- `entityId` (uuid, optional): 当前客户范围内的 Entity ID；只用于已配置的 Entity 和许可 Feature。

<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"`.
- `holdings` (object, required): Customer 在 `snapshotAt` 时刻的持有情况（传入 `entityId` 时限定到该 Entity）：会员资格与各 Feature 的余额。
- `holdings.customerId` (uuid, required): Customer 的 UUID。
- `holdings.snapshotAt` (date-time, required): 对账单快照时间；翻页时传首页返回的值，首页传 `null`。
- `holdings.memberships` (array, required): 会员，字段见下。 maxItems: 100
- `holdings.memberships.id` (uuid, required): 会员 的 ID。
- `holdings.memberships.productId` (string, required): Product 的 ID。 minLength: 1 · maxLength: 255
- `holdings.memberships.productVersionId` (uuid, required): Product Version 的 ID。
- `holdings.memberships.billingProductVersionId` (uuid, required): 该会员资格计费所依据的 Product Version 的 UUID；与 `productVersionId` 相同。
- `holdings.memberships.version` (integer, required): 版本号。 max: 9007199254740991
- `holdings.memberships.key` (string, required): Product 的 key（即其公开 ID），与 `productId` 相同。 minLength: 1 · maxLength: 64
- `holdings.memberships.name` (string, required): 展示名称，不作为唯一标识。 minLength: 1 · maxLength: 120
- `holdings.memberships.groupId` (string | null, required): Product 所属的分组：同一分组内的主 Product 互斥。`null` 表示默认分组。
- `holdings.memberships.membershipType` (enum, required): `main` 为主 Product，会替换同一分组内的其他主 Product；`addon` 为附加 Product，可与其他 Product 同时持有。 Values: `"main"`, `"addon"`.
- `holdings.memberships.status` (enum, required): 状态，取值见允许值。 Values: `"trial"`, `"active"`, `"past_due"`, `"ended"`, `"expired"`, `"trialing"`, `"paused"`, `"pending"`, `"scheduled"`, `"unknown"`.
- `holdings.memberships.cancelAtPeriodEnd` (boolean, required): 是否在当前周期结束时取消。
- `holdings.memberships.accessEnabled` (boolean, required): 该会员资格此刻是否提供其 Product 的权益：已开始、未结束，且处于有效、逾期未付或未到期的试用状态。
- `holdings.memberships.renewalChange` (object | null, required): 该会员资格待生效或已排期的变更（更换 Product Version、调整席位数或 Feature 数量），包含生效时机与状态；没有待处理变更时为 `null`。
- `holdings.memberships.renewalChange.productVersionId` (uuid, required): Product Version 的 ID。
- `holdings.memberships.renewalChange.name` (string, required): 展示名称，不作为唯一标识。 minLength: 1 · maxLength: 120
- `holdings.memberships.renewalChange.version` (integer, required): 版本号。 max: 9007199254740991
- `holdings.memberships.renewalChange.effectiveAt` (date-time, required): 时间（ISO 8601）。
- `holdings.memberships.renewalChange.benefitsAppliedAt` (date-time | null, required): 时间（ISO 8601）。
- `holdings.memberships.renewalChange.timing` (enum, required): 立即变更或在周期末变更；只用于契约允许的套餐变更动作。 Values: `"immediate"`, `"renewal"`.
- `holdings.memberships.renewalChange.changeKind` (enum, required): `plan` 表示更换为另一个 Product Version；`seats` 表示调整席位数（见 `seat`）；`quantity` 表示调整 Feature 的购买数量（见 `feature`）。 Values: `"plan"`, `"seats"`, `"quantity"`.
- `holdings.memberships.renewalChange.feature` (object, optional): Feature，字段见下。
- `holdings.memberships.renewalChange.seat` (object | null, required): `seats` 类变更的席位信息：席位 `key` 及变更前（`fromQuantity`）和变更后（`toQuantity`）的数量；其他变更类型为 `null`。
- `holdings.memberships.renewalChange.status` (enum, required): 状态，取值见允许值。 Values: `"prepared"`, `"quoted"`, `"confirmed"`, `"pending"`, `"scheduled"`.
- `holdings.memberships.period` (object | null, required): 该会员资格的当前周期：`phase`、`startsAt` 和 `endsAt`（ISO 8601；没有结束时间时 `endsAt` 为 `null`）。会员资格没有开始时间时为 `null`。
- `holdings.memberships.period.phase` (enum, required): 免费试用进行中为 `trial`，否则为 `paid`。 Values: `"paid"`, `"trial"`.
- `holdings.memberships.period.startsAt` (date-time, required): 时间（ISO 8601）。
- `holdings.memberships.period.endsAt` (date-time | null, required): 时间（ISO 8601）。
- `holdings.membershipsLimited` (boolean, required): 会员资格是否超过 100 条；为 `true` 时 `memberships` 只包含最近创建的 100 条。
- `holdings.balances` (array, required): Customer（或指定 Entity）当前持有的每个 Feature 各一条，汇总所有当前有效的来源，按 Feature 名称排序，最多 500 条。 maxItems: 500
- `holdings.balances.internal_feature_id` (uuid, required): Feature 的 ID。
- `holdings.balances.key` (string, required): Feature 的 key。 minLength: 1 · pattern: ^[a-zA-Z0-9_-]+$
- `holdings.balances.name` (string, required): 展示名称，不作为唯一标识。 minLength: 1 · maxLength: 120
- `holdings.balances.kind` (enum, required): 类型，取值见允许值。 Values: `"access"`, `"metered"`, `"credit"`.
- `holdings.balances.usageType` (enum | null, required): 仅用于计量型 Feature：`consumable` 表示用量从余额中扣减（如请求次数），`non_consumable` 表示占用型容量（如席位），其状态见 `usage`。`kind` 为 `access` 或 `credit` 时为 `null`。
- `holdings.balances.accessible` (boolean, required): 该余额当前是否可用；为 `false` 时 `available` 和 `held` 均为 `0`。
- `holdings.balances.unlimited` (boolean, required): 是否不限量。
- `holdings.balances.available` (string, required): 可用数量。 pattern: ^(0|[1-9]\d{0,17})(\.\d{1,12})?$
- `holdings.balances.held` (string, required): 持有情况。 pattern: ^(0|[1-9]\d{0,17})(\.\d{1,12})?$
- `holdings.balances.debt` (string, required): 该余额尚未结清的欠款，十进制字符串，由后续付款或退款结清；无欠款时为 `0`。 pattern: ^(0|[1-9]\d{0,17})(\.\d{1,12})?$
- `holdings.balances.nextExpiresAt` (date-time | null, required): 时间（ISO 8601）。
- `holdings.balances.usage` (object | null, required): 用量。
- `holdings.balances.usage.internal_feature_id` (uuid, required): Feature 的 ID。
- `holdings.balances.usage.entityId` (uuid | null, required): 当前客户范围内的 Entity ID；只用于已配置的 Entity 和许可 Feature。
- `holdings.balances.usage.operationId` (uuid | null, required): 业务操作 UUID；订阅执行引用 previewAttach 返回的 operationId，余额退款引用原消费操作。
- `holdings.balances.usage.usage` (string, required): 用量。 pattern: ^(0|[1-9]\d{0,11})(\.\d{1,6})?$
- `holdings.balances.usage.capacity` (string | null, required): 容量。
- `holdings.balances.usage.remaining` (string | null, required): 评估时的剩余额度，不预占，也不保证稍后消费成功。
- `holdings.balances.usage.withinLimit` (boolean, required): 是否在上限内。
- `holdings.balances.usage.revision` (integer, required): 特定资源的版本号；Customer 资料更新不使用版本号。 min: 0 · max: 9007199254740991
- `holdings.balances.usage.updatedAt` (date-time | null, required): 记录最后更新时的 ISO 8601 UTC 时间。
- `holdings.balancesLimited` (boolean, required): 余额是否超过 500 条；为 `true` 时 `balances` 只包含按 Feature 名称排序的前 500 条。

<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 | 凭据没有 `entitlements: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#access)
