文字生成影像 API
文字生成影像 API 是一項功能,可讓您將 Meshy 的 AI 影像生成能力整合到您自己的應用程式中。使用我們強大的 AI 模型,根據文字提示生成高品質的影像。
建立文字生成影像任務
此 endpoint 允許您建立新的文字生成影像任務。請參閱 文字生成影像任務物件以了解文字生成影像任務物件包含哪些屬性。
參數
- Name
- ai_model
- Type
- string
- 必選
- Description
用於影像生成的模型 ID。
可用值:
nano-banana:標準模型(每張影像 3 積分)nano-banana-2:性能均衡模型,能力強於標準模型(每張影像 6 積分)nano-banana-pro:專業模型,畫質更佳(每張影像 9 積分)gpt-image-2:OpenAI GPT Image 2,一款高保真影像模型(每張影像 9 積分)
- Name
- prompt
- Type
- string
- 必選
- Description
您想要生成的影像的文字描述。描述越詳細,效果越好。
- Name
- generate_multi_view
- Type
- boolean
- 預設值 false
- Description
設為
true時,將生成一張多視角影像,從多個角度展示主體。當
generate_multi_view為true時,無法設定aspect_ratio參數。
- Name
- pose_mode
- Type
- string
- Description
指定角色生成的姿勢模式。省略時,影像將不使用任何姿勢預設生成。
可用值:
a-pose、t-pose
- Name
- aspect_ratio
- Type
- string
- 預設值 1:1
- Description
指定生成影像的寬高比。允許的值取決於所選的
ai_model:nano-banana、nano-banana-2、nano-banana-pro:1:1、16:9、9:16、4:3、3:4gpt-image-2:1:1、16:9、9:16、3:2、2:3
可用值:
1:1:正方形格式16:9:寬螢幕橫向9:16:寬螢幕縱向4:3:標準橫向(gpt-image-2不支援)3:4:標準縱向(gpt-image-2不支援)3:2:橫向(僅gpt-image-2支援)2:3:縱向(僅gpt-image-2支援)
- Name
- remove_background
- Type
- boolean
- 預設值 false
- Description
設為
true時,生成的影像將以已移除背景的透明 RGBA PNG 格式傳回,以便您將主體合成到任意背景上。
傳回值
回應中的 result 屬性包含新建立的文字生成影像任務的任務 id。
失敗模式
- Name
400 - Bad Request- Description
請求不可接受。常見原因:
- 缺少參數:缺少必要參數(例如
ai_model、prompt)。 - 無效參數:
ai_model或aspect_ratio不是允許的值之一。 - 衝突:
generate_multi_view和aspect_ratio不能同時使用。
- 缺少參數:缺少必要參數(例如
- Name
401 - Unauthorized- Description
身份驗證失敗。請檢查您的 API key。
- Name
402 - Payment Required- Description
積分不足,無法執行此任務。
- Name
429 - Too Many Requests- Description
您已超出速率限制。
Request
# Generate an image from a text prompt
curl https://api.meshy.ai/openapi/v1/text-to-image \
-X POST \
-H "Authorization: Bearer ${YOUR_API_KEY}" \
-H 'Content-Type: application/json' \
-d '{
"ai_model": "nano-banana",
"prompt": "A majestic dragon soaring through clouds at sunset",
"aspect_ratio": "16:9"
}'
Response
{
"result": "018a210d-8ba4-705c-b111-1f1776f7f578"
}
檢索文字生成影像任務
此 endpoint 允許你透過有效的任務 id 檢索文字生成影像任務。
請參閱文字生成影像任務物件,了解文字生成影像任務物件包含哪些屬性。
參數
- Name
- id
- Type
- path
- Description
要檢索的文字生成影像任務的唯一識別碼。
回傳值
回應包含文字生成影像任務物件。詳情請查看 文字生成影像任務物件部分。
Request
curl https://api.meshy.ai/openapi/v1/text-to-image/018a210d-8ba4-705c-b111-1f1776f7f578 \
-H "Authorization: Bearer ${YOUR_API_KEY}"
Response
{
"id": "018a210d-8ba4-705c-b111-1f1776f7f578",
"type": "text-to-image",
"ai_model": "nano-banana",
"prompt": "A majestic dragon soaring through clouds at sunset",
"status": "SUCCEEDED",
"progress": 100,
"created_at": 1692771650657,
"started_at": 1692771667037,
"finished_at": 1692771669037,
"expires_at": 1692771679037,
"image_urls": [
"https://assets.meshy.ai/***/tasks/018a210d-8ba4-705c-b111-1f1776f7f578/output/image.png?Expires=***"
]
}
刪除文字生成影像任務
此 endpoint 會永久刪除一個文字生成影像任務,包括所有相關的圖像和資料。此操作不可逆。
路徑參數
- Name
- id
- Type
- path
- Description
要刪除的文字生成影像任務的 ID。
回傳值
成功時回傳 200 OK。
Request
curl --request DELETE \
--url https://api.meshy.ai/openapi/v1/text-to-image/018a210d-8ba4-705c-b111-1f1776f7f578 \
-H "Authorization: Bearer ${YOUR_API_KEY}"
Response
// Returns 200 Ok on success.
列出文字生成影像任務
此 endpoint 允許您檢索文字生成影像任務的清單。
參數
- Name
- page_num
- Type
- integer
- Description
用於分頁的頁碼。起始值及預設值為
1。
- Name
- page_size
- Type
- integer
- Description
每頁數量上限。預設為
10項。最多允許50項。
- Name
- sort_by
- Type
- string
- Description
排序所依據的欄位。可用值:
+created_at:依建立時間遞增排序。-created_at:依建立時間遞減排序。
返回值
返回一個分頁的文字生成影像任務物件清單。
Request
curl https://api.meshy.ai/openapi/v1/text-to-image?page_size=10 \
-H "Authorization: Bearer ${YOUR_API_KEY}"
Response
[
{
"id": "018a210d-8ba4-705c-b111-1f1776f7f578",
"type": "text-to-image",
"ai_model": "nano-banana",
"prompt": "A majestic dragon soaring through clouds at sunset",
"status": "SUCCEEDED",
"progress": 100,
"created_at": 1692771650657,
"started_at": 1692771667037,
"finished_at": 1692771669037,
"expires_at": 1692771679037,
"image_urls": [
"https://assets.meshy.ai/***/tasks/018a210d-8ba4-705c-b111-1f1776f7f578/output/image.png?Expires=***"
]
}
]
流式取得文字生成影像任務
此 endpoint 使用 Server-Sent Events(SSE)串流傳輸文字生成影像任務的即時更新。
參數
- Name
- id
- Type
- path
- Description
要流式取得的文字生成影像任務的唯一識別碼。
回傳
以 Server-Sent Events 的形式回傳 文字生成影像任務物件 的串流。
對於 PENDING 或 IN_PROGRESS 狀態的任務,回應串流將僅包含必要的 progress 和 status 欄位。
Request
curl -N https://api.meshy.ai/openapi/v1/text-to-image/018a210d-8ba4-705c-b111-1f1776f7f578/stream \
-H "Authorization: Bearer ${YOUR_API_KEY}"
Response Stream
// Error event example
event: error
data: {
"status_code": 404,
"message": "Task not found"
}
// Message event examples illustrate task progress.
// For PENDING or IN_PROGRESS tasks, the response stream will not include all fields.
event: message
data: {
"id": "018a210d-8ba4-705c-b111-1f1776f7f578",
"progress": 0,
"status": "PENDING"
}
event: message
data: {
"id": "018a210d-8ba4-705c-b111-1f1776f7f578",
"type": "text-to-image",
"ai_model": "nano-banana",
"prompt": "A majestic dragon soaring through clouds at sunset",
"status": "SUCCEEDED",
"progress": 100,
"created_at": 1692771650657,
"started_at": 1692771667037,
"finished_at": 1692771669037,
"expires_at": 1692771679037,
"image_urls": [
"https://assets.meshy.ai/***/tasks/018a210d-8ba4-705c-b111-1f1776f7f578/output/image.png?Expires=***"
]
}
The Text to Image Task Object
Text to Image Task 物件是 Meshy 追蹤的一個工作單元,用於根據文字 prompt 輸入生成圖片。 該物件具有以下屬性:
Properties
- Name
- id
- Type
- string
- Description
任務的唯一識別碼。雖然我們在實作細節上使用 k-可排序的 UUID 作為任務 id, 但你不應對 id 的格式做任何假設。
- Name
- type
- Type
- string
- Description
圖片生成任務的類型。對於文字生成影象任務,此值始終為
text-to-image。
- Name
- ai_model
- Type
- string
- Description
此任務使用的 AI 模型。可能的值為
nano-banana、nano-banana-2、nano-banana-pro或gpt-image-2。
- Name
- prompt
- Type
- string
- Description
用於生成圖片的文字 prompt。
- Name
- status
- Type
- string
- Description
任務的狀態。可能的值為
PENDING、IN_PROGRESS、SUCCEEDED、FAILED、CANCELED之一。
- Name
- progress
- Type
- integer
- Description
任務的進度。如果任務尚未開始,此屬性將為
0。一旦任務成功完成,此值將變為100。
- Name
- created_at
- Type
- timestamp
- Description
任務建立時的時間戳,以毫秒為單位。
時間戳表示自 1970 年 1 月 1 日 UTC 以來經過的毫秒數,遵循 RFC 3339
標準。 例如,2023 年 9 月 1 日星期五格林威治標準時間中午 12:00:00 表示為1693569600000。這適用於 Meshy API 中的所有時間戳。
- Name
- started_at
- Type
- timestamp
- Description
任務開始時的時間戳,以毫秒為單位。如果任務尚未開始,此屬性將為
0。
- Name
- finished_at
- Type
- timestamp
- Description
任務完成時的時間戳,以毫秒為單位。如果任務尚未完成,此屬性將為
0。
- Name
- expires_at
- Type
- timestamp
- Description
任務結果過期時的時間戳,以毫秒為單位。
- Name
- preceding_tasks
- Type
- integer
- Description
前置任務的數量。
此欄位的值僅在任務狀態為
PENDING時才有意義。
- Name
- image_urls
- Type
- array
- Description
生成圖片的可下載 URL 陣列。當啟用
generate_multi_view時,此陣列包含代表不同視角的三個圖片 URL。否則,它只包含單一圖片 URL。
- Name
- task_error
- Type
- object
- Description
失敗任務的錯誤詳情。請參閱 Errors 取得完整的
task_error物件參考。
- Name
- consumed_credits
- Type
- integer
- Description
此任務消耗的積分數量。當任務狀態為
PENDING、IN_PROGRESS或SUCCEEDED時會顯示此值。對於FAILED任務,會回傳0(失敗時會退還積分)。
Example Text to Image Task Object
{
"id": "018a210d-8ba4-705c-b111-1f1776f7f578",
"type": "text-to-image",
"ai_model": "nano-banana",
"prompt": "A majestic dragon soaring through clouds at sunset",
"status": "SUCCEEDED",
"progress": 100,
"created_at": 1692771650657,
"started_at": 1692771667037,
"finished_at": 1692771669037,
"expires_at": 1692771679037,
"preceding_tasks": 0,
"image_urls": [
"https://assets.meshy.ai/***/tasks/018a210d-8ba4-705c-b111-1f1776f7f578/output/image.png?Expires=***"
],
"task_error": {
"message": ""
},
"consumed_credits": 3
}