Kling 文字轉圖片
生成標準圖片、有序系列圖片,以及電商或網頁長圖。
概覽
使用文字轉圖像端點搭配支援的模型。使用回傳的 Task ID 擷取其狀態,或提供 callback_url 以接收下方說明的回呼通知。
快速入門
- 建立 API 金鑰並將其設定為 RUNAPI_API_KEY。
- 選擇支援的模型,並傳送本文符合該模型結構描述的 POST 請求。
- 儲存返回的任務 ID,然後輪詢直到終止狀態,或處理下方說明的回呼。
端點
- 基礎 URL
https://runapi.ai- API 版本
v1- 身分驗證
Authorization: Bearer YOUR_API_TOKEN
支援的模型
開啟模型頁面以查看目前的定價、速率限制及商業用途詳情。
請求綱要
欄位及允許值取決於所選模型。若提供 callback_url,其將接收下方記錄的 Task 交付內容。
kling-v4-image13 個欄位
aspect_ratiostring輸出長寬比。
允許的值:auto, 9:16, 2:3, 3:4, 1:1, 4:3, 3:2, 16:9, 21:9
預設值: auto
callback_urlstring用於非同步通知的 Webhook URL。
elementsarray具名引用,每個計為一份輸入素材。名稱不區分大小寫且必須唯一;image_N 和 object_N 為保留名稱。
項目數上限:10
elements[].descriptionstring100
elements[].image_urlsarray2 - 4
elements[].namestring1 - 20
elements[].tagstringcharacter, animal, prop, costume, scene, effect, others
modestring單張/預設生成或相關聯的系列圖片。
允許的值:default, series
預設值: default
modelstringKling 4.0 圖片模型 slug。
output_countinteger預設模式下為 1–9;系列模式下必須為 2–10。
預設值:1
範圍: 1 - 10
output_resolutionstring輸出解析度。
允許的值:2k, 4k, 8k
預設值: 2k
promptstring圖片描述;使用 @image_N 引用已提供的圖片,或使用 @name 引用元素。
範圍:1 - 20000
source_image_urlsarray可公開存取的 HTTP/HTTPS JPG 或 PNG 圖片,每張不超過 20 MB。圖片與元素合計不超過 10 個。
項目數上限:10
kling-v4-long-image9 個欄位
callback_urlstring用於非同步通知的 Webhook URL。
lengthstring電商圖片長度。
允許的值:short, medium, long
預設值: medium
modelstringKling 4.0 長圖模型 slug。
output_resolutionstring輸出解析度。
允許的值:1k, 2k
預設值: 2k
promptstring長圖描述。引用必須指向已提供的圖片:@image_N、@style 或 @subject。
範圍:1 - 20000
skillstring長圖技能;不區分大小寫。
允許的值:ecommerce, webpage
source_image_urlsarray電商商品圖片:可公開存取的 HTTP/HTTPS JPG 或 PNG,每張不超過 20 MB。
項目數範圍:1 - 3
style_image_urlstring選用的網頁風格參考圖:可公開存取的 HTTP/HTTPS JPG 或 PNG,不超過 20 MB。
subject_image_urlstring選用的網頁主體參考圖:可公開存取的 HTTP/HTTPS JPG 或 PNG,不超過 20 MB。
條件規則
當所有列出的條件均符合時,這些需求即適用。
-
當
model = kling-v4-image- 必填
- 不接受
skill,length,style_image_url,subject_image_url
-
當
mode = series,model = kling-v4-image- 必填
output_count- 不接受
-
當
mode = default,model = kling-v4-image- 必填
- 不接受
-
當
mode absent,model = kling-v4-image- 必填
- 不接受
-
當
model = kling-v4-long-image- 必填
- 不接受
mode,output_count,elements,aspect_ratio
-
當
model = kling-v4-long-image,skill = ecommerce- 必填
source_image_urls- 不接受
style_image_url,subject_image_url
-
當
model = kling-v4-long-image,skill = webpage- 必填
- 不接受
source_image_urls,length
建立接受記錄
POST /api/v1/kling/text_to_image
回應綱要
{
"properties": {
"id": {
"type": "string"
},
"status": {
"enum": [
"processing"
],
"type": "string"
},
"task_replayed": {
"type": "boolean"
}
},
"required": [
"id",
"status"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"id": "tsk_reference_demo",
"status": "processing"
}
輪詢處理中
GET /api/v1/kling/text_to_image/:id
回應綱要
{
"properties": {
"id": {
"type": "string"
},
"status": {
"enum": [
"processing"
],
"type": "string"
}
},
"required": [
"id",
"status"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"id": "tsk_reference_demo",
"status": "processing"
}
輪詢已完成
GET /api/v1/kling/text_to_image/:id
回應綱要
{
"properties": {
"id": {
"type": "string"
},
"images": {
"items": {
"properties": {
"url": {
"type": "string",
"x-runapi-generated-media": true
}
},
"required": [
"url"
],
"type": "object",
"unevaluatedProperties": false
},
"type": "array"
},
"status": {
"enum": [
"completed"
],
"type": "string"
},
"usage": {
"properties": {
"cost": {
"type": "number"
}
},
"required": [
"cost"
],
"type": "object",
"unevaluatedProperties": false
}
},
"required": [
"id",
"status",
"images",
"usage"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"id": "tsk_reference_demo",
"images": [
{
"url": "https://cdn.runapi.ai/public/samples/image.jpg"
}
],
"status": "completed",
"usage": {
"cost": 0.05
}
}
輪詢失敗
GET /api/v1/kling/text_to_image/:id
回應綱要
{
"properties": {
"error": {
"type": "string"
},
"id": {
"type": "string"
},
"status": {
"enum": [
"failed"
],
"type": "string"
}
},
"required": [
"id",
"status",
"error"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"error": "Task processing failed.",
"id": "tsk_reference_demo",
"status": "failed"
}
客戶回呼:已完成
POST callback_url
回應綱要
{
"properties": {
"id": {
"type": "string"
},
"images": {
"items": {
"properties": {
"url": {
"type": "string",
"x-runapi-generated-media": true
}
},
"required": [
"url"
],
"type": "object",
"unevaluatedProperties": false
},
"type": "array"
},
"status": {
"enum": [
"completed"
],
"type": "string"
},
"usage": {
"properties": {
"cost": {
"type": "number"
}
},
"required": [
"cost"
],
"type": "object",
"unevaluatedProperties": false
}
},
"required": [
"id",
"status",
"images",
"usage"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"id": "tsk_reference_demo",
"images": [
{
"url": "https://cdn.runapi.ai/public/samples/image.jpg"
}
],
"status": "completed",
"usage": {
"cost": 0.05
}
}
客戶回呼:失敗
POST callback_url
回應綱要
{
"properties": {
"error": {
"oneOf": [
{
"type": "string"
},
{
"properties": {
"code": {
"type": "string"
},
"limit_cents": {
"type": "integer"
},
"message": {
"type": "string"
},
"reset_at": {
"format": "date-time",
"type": [
"string",
"null"
]
},
"used_cents": {
"type": "integer"
},
"window": {
"enum": [
"1h",
"1d",
"7d",
"daily",
"weekly",
"monthly",
"lifetime"
],
"type": "string"
}
},
"required": [
"code",
"message"
],
"type": "object",
"unevaluatedProperties": false
}
]
},
"id": {
"type": "string"
},
"status": {
"enum": [
"failed"
],
"type": "string"
}
},
"required": [
"id",
"status",
"error"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"error": {
"code": "generation_failed",
"message": "Task processing failed."
},
"id": "tsk_reference_demo",
"status": "failed"
}
錯誤
POST /api/v1/kling/text_to_image
回應綱要
{
"properties": {
"error": {
"description": "人類可讀的錯誤訊息。",
"type": "string"
}
},
"required": [
"error"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"error": "Authentication required"
}
錯誤
GET /api/v1/kling/text_to_image/:id
回應綱要
{
"properties": {
"error": {
"description": "人類可讀的錯誤訊息。",
"type": "string"
}
},
"required": [
"error"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"error": "Authentication required"
}
錯誤
POST /api/v1/kling/text_to_image
回應綱要
{
"oneOf": [
{
"properties": {
"error": {
"description": "人類可讀的錯誤訊息。",
"type": "string"
}
},
"required": [
"error"
],
"type": "object",
"unevaluatedProperties": false
},
{
"properties": {
"error": {
"description": "人類可讀的驗證摘要。",
"type": "string"
},
"errors": {
"additionalProperties": {
"items": {
"type": "string"
},
"type": "array"
},
"description": "以公開請求欄位為索引鍵的驗證訊息,每個欄位包含一組人類可讀的字串陣列。",
"type": "object"
}
},
"required": [
"error",
"errors"
],
"type": "object",
"unevaluatedProperties": false
}
]
}
回應範例
{
"error": "model must be one of: kling-v4-image, kling-v4-long-image"
}
錯誤
POST /api/v1/kling/text_to_image
回應綱要
{
"properties": {
"error": {
"description": "人類可讀的錯誤訊息。",
"type": "string"
}
},
"required": [
"error"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"error": "Insufficient balance"
}
錯誤
POST /api/v1/kling/text_to_image
回應綱要
{
"properties": {
"error": {
"description": "人類可讀的錯誤訊息。",
"type": "string"
}
},
"required": [
"error"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"error": "API key credit limit exceeded"
}
錯誤
POST /api/v1/kling/text_to_image
回應綱要
{
"properties": {
"error": {
"description": "人類可讀的錯誤訊息。",
"type": "string"
}
},
"required": [
"error"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"error": "The request uses features that are not supported for the selected model"
}
錯誤
POST /api/v1/kling/text_to_image
回應綱要
{
"properties": {
"error": {
"description": "人類可讀的錯誤訊息。",
"type": "string"
}
},
"required": [
"error"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"error": "Rate limit reached. Please retry later."
}
錯誤
POST /api/v1/kling/text_to_image
回應綱要
{
"properties": {
"error": {
"description": "人類可讀的錯誤訊息。",
"type": "string"
}
},
"required": [
"error"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"error": "Service under maintenance, please try again later"
}
錯誤
POST /api/v1/kling/text_to_image
回應綱要
{
"properties": {
"error": {
"description": "人類可讀的錯誤訊息。",
"type": "string"
}
},
"required": [
"error"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"error": "The request timed out"
}
錯誤
GET /api/v1/kling/text_to_image/:id
回應綱要
{
"properties": {
"error": {
"description": "人類可讀的錯誤訊息。",
"type": "string"
}
},
"required": [
"error"
],
"type": "object",
"unevaluatedProperties": false
}
回應範例
{
"error": "Task with id 'tsk_reference_demo' not found"
}
產生的程式碼範例
直接使用 cURL,或為您的程式語言安裝 SDK。每個範例均會提交本參考中所示的已驗證請求。
安裝
pip install runapi-kling
import os
from runapi.kling import KlingClient
client = KlingClient(api_key=os.environ["RUNAPI_API_KEY"])
task = client.text_to_image.create(
model="kling-v4-image",
prompt="A red apple on a white table",
output_resolution="2k",
output_count=1
)