文字生成影像 API
使用 Meshy 的 AI 模型根据文字 prompt 生成影像。
文字生成影像用于制作影像。若要为影像中的内容制作 3D 模型,请使用 Image to 3D。若要根据照片制作可握持或可打印的物件,请使用 Creative Lab APIs。它们会先制作一张原型影像,然后据此构建 3D 模型,例如 Keychain、冰箱贴、Figure、灯具或键帽。
创建文字生成影像任务
创建一个文字生成影像任务。该任务在后台运行。有关任务通常需要多长时间,请参阅处理时间。
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"
}
参数
- Name
- ai_model
- Type
- string
- 必选
- Description
用于生成图像的 AI 模型。不同模型的价格不同。有关价格,请参阅 Pricing。
可用值:
nano-banana:标准模型nano-banana-2:比nano-banana更强大nano-banana-2-lite:nano-banana-2的精简版本nano-banana-pro:Pro 模型,质量更高。需要 Pro 或更高级别的 plan。若 plan 级别较低,将返回402。gpt-image-2:OpenAI GPT Image 2gpt-image-2-5-flare:OpenAI GPT Image 2.5(Flare)gpt-image-2-5-sunburst:OpenAI GPT Image 2.5(Sunburst)
- Name
- prompt
- Type
- string
- 必选
- Description
描述要生成的图像的文本。描述越详细,效果越好。
- Name
- generate_multi_view
- Type
- boolean
- 默认值 false
- Description
是否生成从多个角度展示主体的多视图图像。当该值为
true时,不能发送aspect_ratio。同时发送两者会返回400。
- Name
- pose_mode
- Type
- string
- Description
角色的姿势,
a-pose或t-pose。不传则表示不设置姿势。姿势的生效方式取决于
ai_model:nano-banana-2和nano-banana-2-lite:Meshy 保持你选择的模型。图像始终为3:4,Meshy 会忽略aspect_ratio。nano-banana和nano-banana-pro:Meshy 使用nano-banana-pro生成图像,因此任务的ai_model回传值为nano-banana-pro。这需要 Pro 或更高级别的 plan。图像始终为3:4,Meshy 会忽略aspect_ratio。- GPT Image 模型(
gpt-image-2、gpt-image-2-5-flare和gpt-image-2-5-sunburst):Meshy 保持你选择的模型和你设置的aspect_ratio。
- Name
- aspect_ratio
- Type
- string
- 默认值 1:1
- Description
图像的形状。可发送的值取决于
ai_model。可用值:
1:1:正方形16:9:宽屏横向9:16:宽屏纵向4:3:标准横向3:4:标准纵向3:2:横向,仅限 GPT Image 模型2:3:纵向,仅限 GPT Image 模型
nano-banana系列模型(nano-banana、nano-banana-2、nano-banana-2-lite和nano-banana-pro)对3:2和2:3会返回400。
- Name
- remove_background
- Type
- boolean
- 默认值 false
- Description
是否移除图像的背景。设为
true会返回透明的 RGBA PNG,这样你可以将主体放置在任意背景上。
返回值
响应中的 result 字段保存新建文字生成影像任务的 ID。
失败情况
- Name
400 - Bad Request- Description
请求无法运行。错误信息会说明发现的问题。例如:
- 缺少参数:缺少必需参数,例如
ai_model或prompt。 - 参数无效:
ai_model或aspect_ratio不属于允许的取值。 - 冲突:
aspect_ratio与generate_multi_view: true一起发送。
修复请求后重新发送。请参阅 Errors。
- 缺少参数:缺少必需参数,例如
- Name
401 - Unauthorized- Description
API key 缺失或无效。请检查
Authorization请求头。
- Name
402 - Payment Required- Description
账户无法运行此任务。例如:
- 免费 plan:创建任务需要付费 plan。请在subscription 设置中升级你的 plan,然后重试。
- 积分不足:账户没有足够的积分来完成此任务(
Insufficient funds),或账户已达到积分限额。 - 需要更高 plan:请求使用了
nano-banana-pro,或在低于 Pro 的 plan 上使用了pose_mode配合nano-banana(Only Pro and above users can use nano-banana-pro,或Only Pro and above users can use A/T pose feature (requires nano-banana-pro))。
请检查你的积分余额和 plan,然后重试。
- Name
429 - Too Many Requests- Description
你已超出速率限制。请稍候后重试。请参阅速率限制。
获取文字生成影像任务
通过任务 ID 返回对应的文字生成影像任务。
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=***"
]
}
删除文字生成影像任务
永久删除一个文字生成影像任务,包括其图像和数据。删除后无法撤销。
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
// 200 OK on success, with an empty body.
//
// 409 Conflict when the task is IN_PROGRESS. The task keeps running:
{
"message": "Task is IN_PROGRESS and cannot be deleted. Credits for a task the worker has already started are not refundable; wait for it to reach SUCCEEDED, FAILED or CANCELED, then delete it."
}
路径参数
- Name
- id
- Type
- path
- Description
要删除的任务的 ID。
任务状态
删除一个 PENDING 状态的任务会退还你创建该任务时所使用的积分。
处于 IN_PROGRESS 状态的任务无法删除。该请求会返回 409 Conflict,
任务会继续运行。Meshy 不会为已经开始运行的任务退款,
因此删除它会让你同时损失积分和结果。请等待任务
变为 SUCCEEDED、FAILED 或 CANCELED 状态后再删除。
删除处于最终状态(SUCCEEDED、FAILED 或 CANCELED)的任务
不会退还其积分。
返回
任务删除成功时返回 200 OK;当任务处于
IN_PROGRESS 状态时返回 409 Conflict。
列出文字生成影像任务
返回你的文字生成影像和影像生成影像任务列表。每个任务的 type 字段表示它属于这两者中的哪一种。
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=***"
]
}
]
参数
- Name
- page_num
- Type
- integer
- Description
要返回的页码。从
1开始,默认为1。
- Name
- page_size
- Type
- integer
- Description
每页的任务数量,最多为
100。默认为10。
- Name
- sort_by
- Type
- string
- Description
排序方式。
可用值:
+created_at:最旧的排在最前-created_at:最新的排在最前
返回值
一个分页的任务对象列表,格式与文字生成影像任务对象相同。
流式传输文字生成影像任务
以服务器发送事件(SSE)的形式流式传输文字生成影像任务的更新。
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.
// These examples are shortened. Each event holds the full task object.
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=***"
]
}
参数
- Name
- id
- Type
- path
- Description
创建响应中返回的任务 ID。
返回
以服务器发送事件形式返回的 文字生成影像任务对象 流。
每个事件都包含完整的任务对象,其结构与 Get endpoint 返回的结构相同。当任务处于 PENDING 或 IN_PROGRESS 状态时,其结果字段尚无取值。
文字生成影像任务对象
一个文字生成影像任务对象描述一次根据文本 prompt 生成影像的过程。它保存了任务的状态、进度,以及任务成功后的影像 URL。
属性
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
}
- Name
- id
- Type
- string
- Description
任务 ID。请将其视为不透明字符串,不要依赖其格式。
- Name
- type
- Type
- string
- Description
任务类型,始终为
text-to-image。
- Name
- ai_model
- Type
- string
- Description
任务所使用的 AI 模型。
可能的值:
nano-banananano-banana-2nano-banana-2-litenano-banana-progpt-image-2gpt-image-2-5-flaregpt-image-2-5-sunburst
- 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
任务创建的时间,以 Unix 毫秒数表示。
- Name
- started_at
- Type
- timestamp
- Description
任务开始的时间,以 Unix 毫秒数表示。任务开始前为
0。
- Name
- finished_at
- Type
- timestamp
- Description
任务完成的时间,以 Unix 毫秒数表示。任务完成前为
0。
- Name
- expires_at
- Type
- timestamp
- Description
任务结果过期的时间,以 Unix 毫秒数表示。
- Name
- preceding_tasks
- Type
- integer
- Description
排在此任务之前的任务数量。该值仅在任务为
PENDING状态时有意义。
- Name
- image_urls
- Type
- array
- Description
生成的影像的下载 URL。当
generate_multi_view: true时,该数组为每个视角保存一个 URL,通常为三个。如果 Meshy 无法分离出每个视角,返回的数量会更少,但任务仍视为成功。否则,该数组只保存一个 URL。
- Name
- task_error
- Type
- object
- Description
任务失败时的错误详情。参见 Errors。
- Name
- consumed_credits
- Type
- integer
- Description
任务消耗的积分数量。当状态为
PENDING、IN_PROGRESS或SUCCEEDED时会显示该字段。对于FAILED的任务,该值为0,因为 Meshy 会在任务失败时退还积分。