# 检查 Feature 访问权益

Operation / SDK: `checkEntitlement` · `check_entitlement` · `CheckEntitlement`

检查 Feature 需求是否满足；只观察，不扣减用量。

更新于 2026-11-06

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

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

## 请求

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

响应为示例数据。

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/entitlements/check" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "requirement": {
    "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
    "quantity": "1",
    "properties": {}
  },
  "commerce": {
    "provider": "stripe",
    "currency": "USD"
  },
  "paywallId": "31a2b083-2ecf-4b52-8431-a3141dab0bc1",
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d"
}'
```

**TypeScript: checkEntitlement.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 checkEntitlement() {
  return longplay.checkEntitlement({
    requirement: {
      internal_feature_id: "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
      quantity: "1",
      properties: {},
    },
    commerce: {
      provider: "stripe",
      currency: "USD",
    },
    paywallId: "31a2b083-2ecf-4b52-8431-a3141dab0bc1",
    customerId: "8b258f8b-4228-40fa-8d24-5b16428e686d",
  });
}
```

**请求体: request.json**

```json
{
  "requirement": {
    "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
    "quantity": "1",
    "properties": {}
  },
  "commerce": {
    "provider": "stripe",
    "currency": "USD"
  },
  "paywallId": "31a2b083-2ecf-4b52-8431-a3141dab0bc1",
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d"
}
```

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

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "check": {
    "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
    "name": "Export",
    "kind": "access",
    "quantity": "1",
    "allowed": false,
    "reason": "insufficient_balance",
    "preview": {
      "scenario": "feature_flag",
      "candidates": [
        {
          "productId": "2fd314d6-0f91-46cc-8882-ae8b3cc3bd21",
          "productVersionId": "8c489a1a-5542-44fe-8e80-da2f6a319d01",
          "name": "Pro",
          "action": "purchase",
          "membershipId": null,
          "currency": "USD",
          "amountMinor": "1000",
          "interval": "month",
          "intervalCount": 1
        }
      ]
    }
  }
}
```

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

## 请求体参数

**application/json**

- `requirement` (object, required): 要检查的需求：Feature、数量和计价属性。
- `requirement.internal_feature_id` (uuid, required): Feature 的 ID。
- `requirement.quantity` (string, required): 数量，十进制字符串，避免浮点误差。 pattern: ^(0|[1-9]\d{0,11})(\.\d{1,6})?$
- `requirement.properties` (object, required): 服务端校验的计价属性，用于匹配维度和倍率。
- `requirement.entityId` (uuid, optional): 当前客户范围内的 Entity ID；只用于已配置的 Entity 和许可 Feature。
- `commerce` (object, optional): 支付渠道与币种，例如 `{ "provider": "stripe", "currency": "USD" }`。
- `commerce.provider` (enum, required): 支付渠道，取值见允许值。 Values: `"stripe"`, `"apple"`, `"google"`, `"merchant"`.
- `commerce.currency` (enum, required): 已发布价格支持的币种代码。 Values: `"USD"`, `"EUR"`, `"CNY"`, `"GBP"`, `"JPY"`, `"CAD"`, `"AUD"`, `"NZD"`, `"CHF"`, `"HKD"`, `"SGD"`, `"KRW"`, `"INR"`, `"BRL"`, `"MXN"`, `"TWD"`, `"THB"`, `"IDR"`, `"MYR"`, `"PHP"`, `"VND"`, `"ZAR"`, `"AED"`, `"SAR"`, `"SEK"`, `"NOK"`, `"DKK"`, `"PLN"`, `"CZK"`, `"HUF"`, `"ILS"`, `"TRY"`, `"RON"`.
- `commerce.intent` ("subscription_change", optional): 操作意图，取值见允许值。
- `paywallId` (uuid, optional): Paywall 的 ID。
- `customerId` (string, optional): Customer 的 UUID。用 Server Key 调用时指定 Customer；Customer Token 调用时省略。 minLength: 1

<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"`.
- `check` (object, required): 权益检查结果。
- `check.internal_feature_id` (string, required): Feature 的 ID。 minLength: 1
- `check.name` (string, required): 展示名称，不作为唯一标识。 minLength: 1 · maxLength: 120
- `check.kind` (enum, required): 类型，取值见允许值。 Values: `"access"`, `"metered"`, `"credit"`.
- `check.quantity` (string, required): 数量，十进制字符串，避免浮点误差。 pattern: ^(0|[1-9]\d{0,11})(\.\d{1,6})?$
- `check.allowed` (boolean, required): 当前评估时是否允许访问此 Feature。仅用于展示，业务操作仍需后端授权。
- `check.observation` (object, optional): 本次检查的权威来源、版本和观察时间；商户判定时必须返回。
- `check.observation.subject` (object, required): 本次权益判定对应的 App、环境和 Customer；须与原请求一致。
- `check.observation.subject.scope` (object, required): 响应实际所属的 App 和环境。Header 只能核对，不能切换 Key 范围。
- `check.observation.subject.customerId` (uuid, required): Customer 的 UUID。
- `check.observation.authority` (enum, required): 给出这次判定的权威方。 Values: `"longplay"`, `"merchant"`.
- `check.observation.observedAt` (date-time, required): 时间（ISO 8601）。
- `check.observation.sourceRevision` (string, required): 权威数据的版本，用来判断结果是否过期。 minLength: 1 · maxLength: 128
- `check.observation.requirementDigest` (string, required): 原检查要求的摘要；响应必须和原要求一致。 pattern: ^[a-f0-9]{64}$
- `check.reason` (enum | null, required): 拒绝原因或账本操作说明；结合 accepted / 状态判断结果。
- `check.preview` (object | null, required): 访问被拒绝且请求带有 `commerce` 时的购买建议。Customer 持有该 Feature（或为其供额的积分 Feature）的余额时 `scenario` 为 `usage_limit`，否则为 `feature_flag`。`candidates` 按顺序列出最多 20 个授予该 Feature 或积分 Feature 的 Product，按 `commerce.currency` 计价：先是 Customer 未持有、相对其当前主 Product 属于升级的主 Product（未持有主 Product 时为全部主 Product），再是其未持有的附加 Product 和一次性附加 Product。在该币种下没有单一固定价格的 Product 不会列出；传入 `paywallId` 或通过 Paywall、Offering 检查时只列出其展示的 Product，因此 `candidates` 可能为空。`allowed` 为 `true` 或请求未带 `commerce` 时为 `null`。
- `check.preview.scenario` (enum, required): 场景，取值见允许值。 Values: `"feature_flag"`, `"usage_limit"`.
- `check.preview.candidates` (array, required): 候选 Product。 maxItems: 20
- `check.preview.candidates.productId` (string, required): Product 的 ID。 minLength: 1
- `check.preview.candidates.productVersionId` (string, required): Product Version 的 ID。 minLength: 1
- `check.preview.candidates.name` (string, required): 展示名称，不作为唯一标识。 minLength: 1 · maxLength: 120
- `check.preview.candidates.action` (enum, required): 本次业务动作。不同动作的目标字段和数量要求不同，见请求结构与示例。 Values: `"purchase"`, `"upgrade"`, `"addon"`, `"topup"`.
- `check.preview.candidates.membershipId` (string | null, required): 会员的 ID。
- `check.preview.candidates.featureQuantities` (object, optional): 按内部 Feature ID 记录的预付 Feature 数量。
- `check.preview.candidates.currency` (enum, required): 已发布价格支持的币种代码。 Values: `"USD"`, `"EUR"`, `"CNY"`, `"GBP"`, `"JPY"`, `"CAD"`, `"AUD"`, `"NZD"`, `"CHF"`, `"HKD"`, `"SGD"`, `"KRW"`, `"INR"`, `"BRL"`, `"MXN"`, `"TWD"`, `"THB"`, `"IDR"`, `"MYR"`, `"PHP"`, `"VND"`, `"ZAR"`, `"AED"`, `"SAR"`, `"SEK"`, `"NOK"`, `"DKK"`, `"PLN"`, `"CZK"`, `"HUF"`, `"ILS"`, `"TRY"`, `"RON"`.
- `check.preview.candidates.amountMinor` (string, required): 金额，以最小货币单位计（如美分）。 pattern: ^(0|[1-9]\d{0,8})$
- `check.preview.candidates.interval` (enum, required): 周期，取值见允许值。 Values: `"free"`, `"week"`, `"month"`, `"year"`, `"one_time"`.
- `check.preview.candidates.intervalCount` (integer, required): 收费周期的次数：week 最多 156、month 最多 36、year 最多 3，省略为 1；free 和 one_time 不接受。额度重置周期独立。 min: 1 · max: 156

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