快速开始
这页介绍如何使用 OpenAPI 完成一次基础的内容创建流程。
下面示例用 slides_video 演示基础流程;真实可用类型、比例、页数、质量、语言、音色和风格,仍然都要先以 /catalog 为准。
开始前先拿到 API Key
OpenAPI 使用用户自己的 API Key。
如果你还没有 key,先去主站的 API Key 页面:
- 激活 API Key
- 复制 API Key
- 回到这里继续
GET /me校验
如果你在为终端用户做接入,建议把这一步放进你的连接向导里。
准备
export KGP_BASE_URL="https://api.kpainter.ai/openapi/v1"export KGP_API_KEY="<your_api_key>"
import requests BASE_URL = "https://api.kpainter.ai/openapi/v1"HEADERS = { "X-KGP-Api-Key": "<your_api_key>",}
第 1 步:校验 Key
拿到 key 后,第一步不是直接创建,而是先校验:
curl -s "$KGP_BASE_URL/me" \ -H "X-KGP-Api-Key: $KGP_API_KEY"
me_response = requests.get(f"{BASE_URL}/me", headers=HEADERS, timeout=30)me_response.raise_for_status()print(me_response.json())
至少确认:
- 当前 key 有效
- 这是能创建内容的用户级 key
第 2 步:读取 Catalog
curl -s "$KGP_BASE_URL/catalog" \ -H "X-KGP-Api-Key: $KGP_API_KEY"
catalog_response = requests.get(f"{BASE_URL}/catalog", headers=HEADERS, timeout=30)catalog_response.raise_for_status()catalog = catalog_response.json()print(catalog["content_types"])
从这里拿:
- 支持的知识类型
- 每种类型可用的比例、质量、时长或页数
- 语言、音色、风格
第 3 步:创建内容
curl -s "$KGP_BASE_URL/creations" \ -H "Content-Type: application/json" \ -H "X-KGP-Api-Key: $KGP_API_KEY" \ -d '{ "type": "slides_video", "prompt": "用 6 页讲清楚 transformer 的 attention 是怎么工作的", "language": "zh", "aspect_ratio": "16:9", "scene_count": 6 }'
create_payload = { "type": "slides_video", "prompt": "用 6 页讲清楚 transformer 的 attention 是怎么工作的", "language": "zh", "aspect_ratio": "16:9", "scene_count": 6,} create_response = requests.post( f"{BASE_URL}/creations", headers={**HEADERS, "Content-Type": "application/json"}, json=create_payload, timeout=30,)create_response.raise_for_status() creation = create_response.json()creation_id = creation["creation_id"]job_id = creation["job"]["job_id"] print({"creation_id": creation_id, "job_id": job_id})
一次基础请求通常至少包含:
typeprompt- 其他该类型必要字段
第 4 步:轮询任务
curl -s "$KGP_BASE_URL/creations/jobs/<job_id>" \ -H "X-KGP-Api-Key: $KGP_API_KEY"
import time while True: job_response = requests.get( f"{BASE_URL}/creations/jobs/{job_id}", headers=HEADERS, timeout=30, ) job_response.raise_for_status() job = job_response.json() print(job["status"], job.get("progress_pct"), job.get("current_step")) if job["status"] == "succeeded": break if job["status"] in {"failed", "cancelled"}: raise RuntimeError(job) time.sleep(3)
当任务成功后,再去读详情。
第 5 步:读取详情
curl -s "$KGP_BASE_URL/creations/<creation_id>" \ -H "X-KGP-Api-Key: $KGP_API_KEY"
detail_response = requests.get( f"{BASE_URL}/creations/{creation_id}", headers=HEADERS, timeout=30,)detail_response.raise_for_status()detail = detail_response.json() print(detail["main_url"])print(detail.get("artifacts", []))print(detail.get("scenes", []))
这里直接拿:
main_urlartifacts[]scenes[]
这次流程会用到的接口
GET /meGET /catalogPOST /creationsGET /creations/jobs/{job_id}GET /creations/{creation_id}
接下来读什么
- 要查接口分组,读
API 参考 - 要处理 key 校验、错误码和安全要求,读
身份认证 - 要让用户知道去哪里查看、激活或重置 key,打开 API Key 页面
- 要按具体知识类型接入,读对应场景页
