# 执行官方模板建码方案

执行 gallery/prepare-creation 准备好的方案：后台安装模板并批量创建子码。需要原 plan_id 和固定的幂等键 idempotency_key；返回任务后用 qrcodeTasks/get 和 qrcodeTasks/getResults 核对逐项结果，方案过期或模板变更需重新准备。

## 请求

```text
POST https://open.cli.im/api/v2/rpc/gallery/create-from-plan
```

| 项目 | 要求 |
|---|---|
| 最低权限 | 读写，见[权限档位](https://cli.im/open-api/openapi/v2/auth.md#org-policy) |
| 需要的 scope | `templates.write`、`qrcodes.write` |
| 组织 Key | 不能调用，返回 403 `credential_type_not_allowed`，见[历史组织 Key](https://cli.im/open-api/openapi/v2/auth.md#org-key) |

## 请求参数

请求体为 JSON。

| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `plan_id` | string | 是 | gallery/prepare-creation 返回的方案 ID，原样使用 |
| `idempotency_key` | string | 是 | 幂等键，由调用方生成，1 到 128 位，只能包含字母、数字和 _ . : -。同一个键重复提交会返回第一次的结果，不会重复创建；请求超时或结果不明时用原键和原参数重试，新的请求换一个新键。最长 128 个字符 |

## 请求示例

示例里的 ID、幂等键等换成实际值。

```bash
curl -sS -X POST 'https://open.cli.im/api/v2/rpc/gallery/create-from-plan' \
  -H "Authorization: Bearer ${CAOLIAO_API_KEY}" \
  -H 'Content-Type: application/json' \
  -d '{
  "plan_id": "plan_0123456789abcdef0123456789abcdef",
  "idempotency_key": "gallery-batch-001"
}'
```

## 响应

成功时 HTTP 状态码为 200，顶层 `code` 为 `0`，结果在 `data` 里。失败时的返回见[错误码与调用额度](https://cli.im/open-api/openapi/v2/error-codes.md)和[业务错误码](https://cli.im/open-api/openapi/v2/error-catalog.md)。

| 字段 | 类型 | 一定返回 | 说明 |
|---|---|---|---|
| `data.task_id` | string | 是 | 任务 ID，用于 qrcodeTasks/get 和 qrcodeTasks/getResults |
| `data.batch_id` | string | 是 | 批次 ID |
| `data.template_id` | string | 是 | 模板 ID |
| `data.total` | integer | 是 | 本批提交的条数 |
| `data.receipt_expires_at` | integer | 是 | 任务回执的过期时间（Unix 秒），过期后不能再查询逐项结果 |
| `data.status` | string | 是 | 任务状态：queued 排队中；running 处理中；succeeded 已结束；failed 失败；cancelled 已取消。succeeded 只表示任务结束，每一项是否建好以 qrcodeTasks/getResults 为准。可选值：`queued`、`running`、`succeeded`、`failed`、`cancelled` |
| `data.progress` | integer | 是 | 进度百分比，0 到 100 |
| `data.processed_count` | integer | 是 | 已处理的条数；取不到时为 null |
| `data.created_at` | any | 是 | 任务创建时间；取不到时为 null |
| `data.updated_at` | any | 是 | 任务最近更新时间；取不到时为 null |
| `data.completion_scope` | string | 是 | 固定为 batch_task，表示 status 描述的是整批任务，不是每一项 |
| `data.poll_after_seconds` | integer | 是 | 建议的轮询间隔（秒） |
| `data.errors` | object[] | 否 | 未成功或未确认的部分；全部成功时为空数组 |
| `data.errors[].code` | string | 是 | 错误码，含义见规范顶层的 x-error-catalog |
| `data.errors[].path` | string | 否 | 出错字段在请求体中的位置，如 items.0.values.12345；没有具体位置时为空字符串 |
| `data.errors[].item_key` | string | 否 | 批量数据中出错那一项的 item_key；不针对某一项时为空字符串 |
| `data.errors[].retryable` | boolean | 否 | 原样重试是否可能成功 |
| `data.errors[].next_action` | string | 否 | 建议的下一步：correct_input 修正入参；retry_later 稍后重试；retry_same_idempotency_key 用原幂等键重试；inspect_created_qrcode、inspect_created_form、inspect_result 先读取已保存的内容核对 |
| `data.errors[].message` | string | 否 | 补充说明，部分错误才有 |
| `data.creation_usage` | object | 否 | 本次操作计入当日额度的新建数量 |
| `data.creation_usage.templates` | integer | 否 | 新建的模板数 |
| `data.creation_usage.forms` | integer | 否 | 新建的表单数 |
| `data.creation_usage.qrcodes` | integer | 否 | 新建的码数 |

### 响应示例

```json
{
  "code": 0,
  "message": "ok",
  "data": {
    "task_id": "9031877",
    "batch_id": "b_20261118_7f3a",
    "template_id": "6801300",
    "total": 1,
    "receipt_expires_at": 1795000000,
    "status": "running",
    "progress": 50,
    "processed_count": 1,
    "created_at": "2026-11-18 10:00:00",
    "updated_at": "2026-11-18 10:00:03",
    "completion_scope": "batch_task",
    "poll_after_seconds": 3,
    "creation_usage": {
      "templates": 1,
      "forms": 1,
      "qrcodes": 1
    }
  }
}
```

## 常见错误

`PERMISSION_DENIED`、`ACCESS_LEVEL_READ_ONLY`、`quota_exceeded`、`IDEMPOTENCY_CONFLICT`、`IDEMPOTENCY_IN_PROGRESS`、`IDEMPOTENCY_OUTCOME_UNKNOWN`、`GALLERY_PLAN_EXPIRED`、`GALLERY_TEMPLATE_CHANGED`、`GALLERY_EXTRA_FIELDS_UNSUPPORTED`、`GALLERY_FIELD_MAPPING_UNAVAILABLE`、`GALLERY_INSTALL_FAILED`、`VALIDATION_FAILED`、`AI_ORIGIN_MARK_FAILED`。含义和处理方式见[业务错误码](https://cli.im/open-api/openapi/v2/error-catalog.md)。

---

本页由 [openapi.json](https://cli.im/open-api/openapi/v2/overview.md#%E6%8E%A5%E5%8F%A3%E8%A7%84%E8%8C%83-openapi-json) 生成。全部接口见[接口清单](https://cli.im/open-api/openapi/v2/api-index.md)。
