# Nano Banana 2 > 使用 Nano Banana 2 生成图像——兼具速度与 4K 分辨率,输出快速且高保真,并保持出色的角色一致性。描述任意场景,几秒内即可获得精致图像。免费试用——按次生成付费,无需订阅。 ## 概览 - **端点**: `POST https://api.muapi.ai/api/v1/nano-banana-2` - **模型 ID**: `nano-banana-2` - **类别**: 文生图 - **变体**: v2 Text to Image - **系列**: nano - **费用**: 每次调用 0.06 credits(部分模型会根据参数动态计算费用) ## API 使用 MuApi 使用**提交后轮询**模式:提交任务,获取 `request_id`,然后轮询预测端点,直到 `status` 为 `completed`。也可以在提交请求中传入 `?webhook=YOUR_URL`,任务完成后接收 POST 回调(无需轮询)。 **身份验证**:在 `x-api-key` 请求头中发送 MuApi 密钥。可在 https://muapi.ai/access-keys 获取。 ### 1. 提交任务 ```http POST https://api.muapi.ai/api/v1/nano-banana-2 Content-Type: application/json x-api-key: YOUR_API_KEY ``` **最小请求(仅必填参数):** ```json { "prompt": "" } ``` **完整示例(全部参数):** ```json { "prompt": "", "aspect_ratio": "1:1", "google_search": false, "resolution": "1k", "output_format": "jpg" } ``` **响应:** ```json { "request_id": "abc123", "status": "processing" } ``` ### 2. 轮询结果 ```http GET https://api.muapi.ai/api/v1/predictions/{request_id}/result x-api-key: YOUR_API_KEY ``` 可能的 `status` 值:`queued`、`pending`、`processing`、`completed`、`failed`、`cancelled`。每 2–5 秒轮询一次,直到进入终止状态。状态为 `completed` 时,结果 URL 位于 `outputs` 数组中。 **`completed` 状态的响应示例:** ```json { "id": "abc123", "status": "completed", "outputs": [ "https://cdn.muapi.ai/.../output.png" ], "urls": { "get": "https://api.muapi.ai/api/v1/predictions/abc123/result" }, "created_at": "2026-05-08T12:34:56Z", "has_nsfw_contents": [] } ``` ### cURL ```bash 1. 提交 REQUEST_ID=$(curl -s -X POST https://api.muapi.ai/api/v1/nano-banana-2 \ -H "x-api-key: $MUAPI_API_KEY" \ -H "Content-Type: application/json" \ -d '{"prompt":""}' | jq -r .request_id) 2. 轮询直到完成 while :; do RESP=$(curl -s https://api.muapi.ai/api/v1/predictions/$REQUEST_ID/result -H "x-api-key: $MUAPI_API_KEY") STATUS=$(echo "$RESP" | jq -r .status) [ "$STATUS" = "completed" ] && echo "$RESP" | jq .outputs && break [ "$STATUS" = "failed" ] && echo "$RESP" && exit 1 sleep 3 done ``` ### Python ```python import os, time, requests API = "https://api.muapi.ai/api/v1" headers = {"x-api-key": os.environ["MUAPI_API_KEY"]} r = requests.post(f"{API}/nano-banana-2", headers=headers, json={"prompt":""}) request_id = r.json()["request_id"] while True: res = requests.get(f"{API}/predictions/{request_id}/result", headers=headers).json() if res["status"] == "completed": print(res["outputs"]); break if res["status"] == "failed": raise RuntimeError(res.get("error")); time.sleep(3) ``` ## 输入结构 API 接受以下输入参数: - **`prompt`** (`string`, _必填_): 描述目标图像内容的文本提示词。 - **`aspect_ratio`** (`string`, _可选_): The aspect ratio of the 生成的图像. - 默认值: `"1:1"` - 选项: `"1:1"`, `"1:4"`, `"1:8"`, `"2:3"`, `"3:2"`, `"3:4"`, `"4:1"`, `"4:3"`, `"4:5"`, `"5:4"`, `"8:1"`, `"9:16"`, `"16:9"`, `"21:9"`, `"Auto"` - **`google_search`** (`boolean`, _可选_): 是否使用 Google Search 增强提示词。 - 默认值: `false` - **`resolution`** (`string`, _可选_): The resolution of the 生成的图像. - 默认值: `"1k"` - 选项: `"1k"`, `"2k"`, `"4k"` - **`output_format`** (`string`, _可选_): The format of the 输出图像. - 默认值: `"jpg"` - 选项: `"jpg"`, `"png"` ## 输出结构 轮询端点会返回以下字段: - **`id`** (`string`): 请求 ID。 - **`status`** (`string`): 取值之一:`queued`、`pending`、`processing`、`completed`、`failed`、`cancelled`。 - **`outputs`** (`array`): 生成的图像、视频或音频 URL。`status` 为 `completed` 前为空。 - **`urls.get`** (`string`): 用于重新获取此预测结果的自身链接。 - **`error`** (`string` | `null`): `status` 为 `failed` 时的错误消息。 - **`created_at`** (`string`): 创建请求时的 ISO-8601 时间戳。 - **`has_nsfw_contents`** (`array of boolean`): 每个输出对应的 NSFW 检测标记。 ## Webhook(可选) 在提交 URL 后附加 `?webhook=https://your-server/path`。任务进入终止状态后,MuApi 会将与轮询响应相同结构的 POST 请求发送到你的 URL,无需轮询。 ## Agent 集成 MuApi 提供 MCP 服务器和 CLI,让 Agent(Claude Code、Cursor 或自定义 Agent)无需编写 HTTP 代码即可调用此端点: ```bash # 安装 CLI npm install -g muapi-cli # 完成一次身份验证 muapi auth login # 将所有 MuApi 模型作为 MCP 工具提供给 Agent muapi mcp serve ``` MCP 服务器为每个模型提供封装提交与轮询的工具,包括 `nano-banana-2`。使用 `muapi --help` 查看按类别划分的快捷命令(`muapi image generate`、`muapi video from-image` 等)。 ## 相关模型 - [v2.1 Image Edit](https://muapi.ai/zh/playground/nano-banana-2-1-edit) - [Photo Packs](https://muapi.ai/zh/playground/photo-pack) - [Edit Image](https://muapi.ai/zh/playground/nano-banana-edit) - [文生图](https://muapi.ai/zh/playground/nano-banana) - [Image Effects](https://muapi.ai/zh/playground/nano-banana-effects) - [Pro Edit Image](https://muapi.ai/zh/playground/nano-banana-pro-edit) - [v2 Text to Image](https://muapi.ai/zh/playground/nano-banana-2) - [Lite Edit](https://muapi.ai/zh/playground/nano-banana-2-lite-edit) - [Lite](https://muapi.ai/zh/playground/nano-banana-2-lite) - [Text to Image Pro](https://muapi.ai/zh/playground/nano-banana-pro) - [v2 Image Edit](https://muapi.ai/zh/playground/nano-banana-2-edit) - [v2.1 Text to Image](https://muapi.ai/zh/playground/nano-banana-2-1) ## 资源 - [Playground 页面](https://muapi.ai/zh/playground/nano-banana-2) - [API 文档](https://muapi.ai/zh/playground/nano-banana-2/api) - [全站 llms.txt](https://muapi.ai/zh/llms.txt)