# 创建、调整或撤销余额发放

Operation / SDK: `manageBalanceGrant` · `manage_balance_grant` · `ManageBalanceGrant`

创建、更新、调整或撤销余额发放（含过期、重置和结转）。

更新于 2026-11-06

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

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

## 请求

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

响应为示例数据。

**cURL: request.sh**

```bash
curl -X POST "$LONGPLAY_API_URL/api/v1/balances/grants/manage" \
  -H "Authorization: Bearer $LONGPLAY_SERVER_KEY" \
  -H "X-Indiana-Protocol: 0.10" \
  -H "Content-Type: application/json" \
  -d '{
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
  "command": {
    "action": "create",
    "requestId": "0f58d1f8-069f-4288-8ec1-4ed10af55b45",
    "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
    "entityId": null,
    "amount": "100",
    "expiresAt": null,
    "reset": {
      "interval": "month",
      "count": 1
    },
    "rollover": null,
    "nextResetAt": null
  }
}'
```

**TypeScript: manageBalanceGrant.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 manageBalanceGrant() {
  return longplay.manageBalanceGrant({
    customerId: "8b258f8b-4228-40fa-8d24-5b16428e686d",
    command: {
      action: "create",
      requestId: "0f58d1f8-069f-4288-8ec1-4ed10af55b45",
      internal_feature_id: "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
      entityId: null,
      amount: "100",
      expiresAt: null,
      reset: {
        interval: "month",
        count: 1,
      },
      rollover: null,
      nextResetAt: null,
    },
  });
}
```

**请求体: request.json**

```json
{
  "customerId": "8b258f8b-4228-40fa-8d24-5b16428e686d",
  "command": {
    "action": "create",
    "requestId": "0f58d1f8-069f-4288-8ec1-4ed10af55b45",
    "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
    "entityId": null,
    "amount": "100",
    "expiresAt": null,
    "reset": {
      "interval": "month",
      "count": 1
    },
    "rollover": null,
    "nextResetAt": null
  }
}
```

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

```json
{
  "requestId": "44f5fcb8-6ed8-41b8-818f-7571941c5fd5",
  "scope": {
    "projectId": "df5426d6-b335-403c-8f3e-b0f73d2e3511",
    "environment": "sandbox"
  },
  "grant": {
    "id": "f7f48d1f-e11d-4f42-81de-46e41939f739",
    "internal_feature_id": "f317c851-0ef2-4d7a-8e3c-e8a29df676ed",
    "entityId": null,
    "amount": null,
    "reset": null,
    "rollover": null,
    "nextResetAt": null,
    "startsAt": "2026-10-03T12:00:00.000Z",
    "expiresAt": null,
    "revision": 0,
    "revoked": true,
    "available": "1",
    "held": "1",
    "spent": "1"
  }
}
```

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

## 请求体参数

**application/json**

- `customerId` (uuid, required): Customer 的 UUID。
- `command` (object | object | object | object, required): 要执行的命令；其中的 `action` 或 `operation` 决定其余字段。
- `command.action` ("create" | "update" | enum | "revoke", required): 本次业务动作。不同动作的目标字段和数量要求不同，见请求结构与示例。
- `command.requestId` (uuid | uuid | uuid | uuid, required): 你的业务请求 UUID；同一次操作重试复用，新的操作生成新值。
- `command.internal_feature_id` (uuid, optional): Feature 的 ID。
- `command.entityId` (uuid | null, optional): 当前客户范围内的 Entity ID；只用于已配置的 Entity 和许可 Feature。
- `command.amount` (string | null | string | null | string, optional): 积分或账本金额，按字段的十进制字符串或整数类型传入。
- `command.expiresAt` (date-time | null | date-time | null, optional): 到期时间（ISO 8601，UTC）。
- `command.reset` (object | null, optional): 重置规则。
- `command.reset.interval` (enum, required): 周期，取值见允许值。 Values: `"minute"`, `"hour"`, `"day"`, `"week"`, `"month"`, `"quarter"`, `"semi_annual"`, `"year"`.
- `command.reset.count` (integer, required): 两次重置之间间隔的 `interval` 单位数，取值 1–1000；例如 `interval` 为 `month`、`count` 为 `3` 时每三个月重置一次。 min: 1 · max: 1000
- `command.rollover` (object | null | object | null, optional): 结转规则。
- `command.rollover.cap` (string | null | string | null, required): 上限。
- `command.rollover.capPercentage` (number | null | number | null, required): 上限百分比。
- `command.rollover.cycles` (integer | null | integer | null, required): 周期数。
- `command.nextResetAt` (date-time | null | date-time | null, optional): 时间（ISO 8601）。
- `command.grantId` (uuid | uuid | uuid, optional): 发放记录的 ID。
- `command.revision` (integer | integer | integer, optional): 特定资源的版本号；Customer 资料更新不使用版本号。

<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"`.
- `grant` (object, required): 发放记录，字段见下。
- `grant.id` (uuid, required): 发放记录 的 ID。
- `grant.internal_feature_id` (uuid, required): Feature 的 ID。
- `grant.entityId` (uuid | null, required): 当前客户范围内的 Entity ID；只用于已配置的 Entity 和许可 Feature。
- `grant.amount` (string | null, required): 积分或账本金额，按字段的十进制字符串或整数类型传入。
- `grant.reset` (object | null, required): 重置规则。
- `grant.reset.interval` (enum, required): 周期，取值见允许值。 Values: `"minute"`, `"hour"`, `"day"`, `"week"`, `"month"`, `"quarter"`, `"semi_annual"`, `"year"`.
- `grant.reset.count` (integer, required): 两次重置之间间隔的 `interval` 单位数，取值 1–1000；例如 `interval` 为 `month`、`count` 为 `3` 时每三个月重置一次。 min: 1 · max: 1000
- `grant.rollover` (object | null, required): 结转规则。
- `grant.rollover.cap` (string | null, required): 上限。
- `grant.rollover.capPercentage` (number | null, required): 上限百分比。
- `grant.rollover.cycles` (integer | null, required): 周期数。
- `grant.nextResetAt` (date-time | null, required): 时间（ISO 8601）。
- `grant.startsAt` (date-time, required): 时间（ISO 8601）。
- `grant.expiresAt` (date-time | null, required): 到期时间（ISO 8601，UTC）。
- `grant.revision` (integer, required): 特定资源的版本号；Customer 资料更新不使用版本号。 min: 0 · max: 9007199254740991
- `grant.revoked` (boolean, required): 撤销的会话数量。
- `grant.available` (string, required): 可用数量。 pattern: ^(0|[1-9]\d{0,12})(\.\d{1,12})?$
- `grant.held` (string, required): 持有情况。 pattern: ^(0|[1-9]\d{0,12})(\.\d{1,12})?$
- `grant.spent` (string, required): 已使用的数量。 pattern: ^(0|[1-9]\d{0,12})(\.\d{1,12})?$

<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:manage` 权限。 | `check_permissions` |
| `SCOPE_MISMATCH` | 403 | `X-Indiana-Project` 或 `X-Indiana-Environment` 与凭据所属的 App 和环境不一致。 | `check_scope` |
| `CONFLICT` | 409 | 同一个 `requestId` 用了不同的输入（`reason: IDEMPOTENCY_CONFLICT`）。 | `resolve_conflict` |
| `RATE_LIMITED` | 429 | 请求过多；按 `Retry-After` 等待后用同一请求重试。 | `retry_same_request` |
| `SERVICE_UNAVAILABLE` | 503 | 服务暂时不可用；用同一请求重试。 | `retry_same_request` |
| `INTERNAL_ERROR` | 500 | 结果未知；先读取原操作，或用原 ID 和原输入重放。 | `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)
