接口提示
spark_asset_create_group_intl 通过统一生成接口创建平台任务,模型参数放在 input[0].params
spark_asset_create_group_intl 返回 type: "text" 的结果,text 是 JSON 字符串,需要解析后读取 Result
spark_asset_create_group_intl 使用海外素材资源,请使用同一套海外模型完成后续操作,不假定国内与海外素材 ID 通用
spark_asset_create_group_intl 解析后的 Result.Id 是创建素材所需的 GroupId,不是平台 task_id
认证
authorization string 必需
所有 API 均使用 Bearer Token 鉴权
获取 API Key:
访问 API Key 管理页面 获取 API Key
用法:
将以下 Header 添加到请求中:
Authorization: Bearer YOUR_API_KEY
参数
model string 必需
请求使用的客户模型 ID
值: spark_asset_create_group_intl
input array 必需
传入一个包含 params 的对象,以下参数均位于 input[0].params
Name string 必需
自定义素材组名称
最大长度: 64 个字符
GroupType string
素材组类型,可省略;基础示例不需要传入此字段
可选值: AIGC
ProjectName string
建议省略或使用默认项目;后续查询示例使用默认项目,不承诺其他项目的完整查询链路
默认值: default
轮询
由于结果生成需要时间,您需要在创建任务后轮询任务状态
初始响应只返回任务 ID 和初始状态等信息,最终生成结果需通过使用该任务 ID 轮询任务状态接口获取
轮询请求与响应示例见右侧
创建响应中的 status: "completed" 只表示创建请求完成。使用返回的 id 查询同一个平台任务,任务查询到 completed 且包含 output 后解析结果
| 任务查询状态 | 处理方式 |
|---|---|
in_progress | 继续查询同一个 task_id |
completed | 解析结果中的 text |
failed | 读取 error.code 和 error.message |
响应格式
右侧成功示例仅展示相关字段,实际结果可能包含额外元数据。text 保持 JSON 字符串格式,不能当作已解析对象读取
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 平台任务 ID,用于任务查询 |
output[].content[].type | string | text |
output[].content[].text | string | JSON 序列化字符串,解析后读取 Result |
output[].content[].url | string | 空字符串,不是素材下载地址 |
usage.cost | string | 费用,单位 USD,当前为 0 |
error | object | 任务失败时的错误信息 |
metadata | object | 任务耗时等元数据,返回时可读取 |
解析结果
const content = task.output.flatMap(item => item.content ?? [])
.find(item => item.type === "text");
const result = JSON.parse(content.text).Result;
const groupId = result.Id;
将解析得到的 Result.Id 传给创建素材的 GroupId,不要把它用作任务查询的 task_id
完整流程
平台 task_id 用于任务查询,组的 Result.Id 用作 GroupId,素材的 Result.Id 用作查询素材的 Id;三者不可互换
以下默认项目示例依次运行三个模型,每次查询复用创建返回的任务 ID。替换 API Key 和图片占位 URL 后执行
const baseURL = "https://cloud.vtrix.ai";
const apiKey = "YOUR_API_KEY";
async function runAssetModel(model, params) {
const createdResponse = await fetch(`${baseURL}/model/v1/generation`, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ model, input: [{ params }] }),
});
const created = await createdResponse.json();
if (!createdResponse.ok || created.error || !created.id) {
throw new Error(JSON.stringify(created));
}
const deadline = Date.now() + 10 * 60 * 1000;
while (Date.now() < deadline) {
const response = await fetch(
`${baseURL}/model/v1/generation/task/${encodeURIComponent(created.id)}`,
{ headers: { Authorization: `Bearer ${apiKey}` } },
);
const task = await response.json();
if (!response.ok || task.status === "failed") {
throw new Error(JSON.stringify(task.error ?? task));
}
if (task.status === "completed" && task.output) {
const text = task.output.flatMap(item => item.content ?? [])
.find(item => item.type === "text")?.text;
if (typeof text !== "string") throw new Error("Missing asset result");
return JSON.parse(text).Result;
}
await new Promise(resolve => setTimeout(resolve, 2000));
}
throw new Error(`Polling timed out; query the same task: ${created.id}`);
}
const group = await runAssetModel("spark_asset_create_group_intl", {
Name: "my-assets",
ProjectName: "default",
});
const asset = await runAssetModel("spark_asset_create_intl", {
GroupId: group.Id,
Name: "reference",
AssetType: "Image",
URL: "https://example.com/reference.jpg",
});
const ready = await runAssetModel("spark_asset_get_intl", { Id: asset.Id });
if (ready.Status !== "Active") throw new Error("Asset is not ready");
console.log(ready.Id, ready.URL);
价格
当前该素材模型免费,成功响应中的 usage.cost 为 "0"。创建组、创建素材和查询素材三个模型均为免费;此免费设置不包含后续图片或视频生成费用
错误处理
任务失败时读取 error.code、error.message。检查参数、URL 可访问性及素材或组 ID;包装不保证返回底层接口的具体错误码。默认项目示例应始终使用同一套海外素材模型