文字生成影像 API
文字生成影像 API 是一项功能,可让您将 Meshy 的 AI 影像生成能力集成到您自己的应用程序中。使用我们强大的 AI 模型,根据文字提示生成高质量的影像。
创建文字生成影像任务
此 endpoint 允许您创建新的文字生成影像任务。请参阅 文字生成影像任务对象以了解文字生成影像任务对象包含哪些属性。
参数
- Name
- ai_model
- Type
- string
- 必选
- Description
用于图像生成的模型 ID。
可用值:
nano-banana:标准模型(每张图像 3 credits)nano-banana-2:性能均衡模型,能力强于标准模型(每张图像 6 credits)nano-banana-pro:专业模型,画质更佳(每张图像 9 credits)gpt-image-2:OpenAI GPT Image 2,一款高保真图像模型(每张图像 9 credits)
- 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
credits 不足,无法执行此任务。
- 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
任务创建时的 timestamp,以毫秒为单位。
timestamp 表示自 1970 年 1 月 1 日 UTC 以来经过的毫秒数,遵循 RFC 3339
标准。 例如,2023 年 9 月 1 日星期五格林尼治标准时间中午 12:00:00 表示为1693569600000。这适用于 Meshy API 中的所有 timestamp。
- Name
- started_at
- Type
- timestamp
- Description
任务开始时的 timestamp,以毫秒为单位。如果任务尚未开始,此属性将为
0。
- Name
- finished_at
- Type
- timestamp
- Description
任务完成时的 timestamp,以毫秒为单位。如果任务尚未完成,此属性将为
0。
- Name
- expires_at
- Type
- timestamp
- Description
任务结果过期时的 timestamp,以毫秒为单位。
- 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
此任务消耗的 credits 数量。当任务状态为
PENDING、IN_PROGRESS或SUCCEEDED时会显示此值。对于FAILED任务,返回0(失败时会退还 credits)。
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
}