> ## Documentation Index
> Fetch the complete documentation index at: https://docs.yingtu.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 生成第一张 Nano Banana 图片

> 向 YingTu 发送 Gemini 原生同步请求，并解码响应中的图片数据。

下面使用 `gemini-3.1-flash-image` 和同步 `:generateContent` 方法。开始前请准备
YingTu API Key 和能够发送 HTTPS 请求的服务端环境。

<Info>
  这里选择 Flash Image，是因为它同时覆盖 1K、2K、4K，适合作为通用起点。
  如果任务只需 1K、必须维持旧接入或准备使用 Pro，请先看
  [模型对比](/cn/models/image/nano-banana/comparison)。
</Info>

<Warning>
  API Key 只能保存在服务端。不要把它写入浏览器代码、移动端应用、截图或公开仓库。
</Warning>

## 发送请求

<CodeGroup>
  ```bash cURL theme={"system"}
  curl --request POST \
    --url 'https://api.yingtu.ai/v1beta/models/gemini-3.1-flash-image:generateContent' \
    --header 'x-goog-api-key: YOUR_YINGTU_API_KEY' \
    --header 'Content-Type: application/json' \
    --data '{
      "contents": [{
        "role": "user",
        "parts": [{"text": "柔和影棚光线下的剪纸风景"}]
      }],
      "generationConfig": {
        "responseModalities": ["TEXT", "IMAGE"],
        "imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"}
      }
    }'
  ```

  ```python Python theme={"system"}
  import base64
  import os
  import requests

  response = requests.post(
      "https://api.yingtu.ai/v1beta/models/"
      "gemini-3.1-flash-image:generateContent",
      headers={"x-goog-api-key": os.environ["YINGTU_API_KEY"]},
      json={
          "contents": [{
              "role": "user",
              "parts": [{"text": "柔和影棚光线下的剪纸风景"}],
          }],
          "generationConfig": {
              "responseModalities": ["TEXT", "IMAGE"],
              "imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"},
          },
      },
      timeout=120,
  )
  response.raise_for_status()
  result = response.json()
  ```

  ```javascript Node.js theme={"system"}
  const response = await fetch(
    "https://api.yingtu.ai/v1beta/models/" +
      "gemini-3.1-flash-image:generateContent",
    {
      method: "POST",
      headers: {
        "x-goog-api-key": process.env.YINGTU_API_KEY,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        contents: [{
          role: "user",
          parts: [{ text: "柔和影棚光线下的剪纸风景" }],
        }],
        generationConfig: {
          responseModalities: ["TEXT", "IMAGE"],
          imageConfig: { aspectRatio: "1:1", imageSize: "1K" },
        },
      }),
    },
  );

  if (!response.ok) throw new Error(`Generation failed: ${response.status}`);
  const result = await response.json();
  ```
</CodeGroup>

## 提取图片部分

Gemini 响应中的 `parts` 可能混合文字和图片。查找带有 `inlineData` 的部分，
将 `inlineData.data` 按 Base64 解码，并根据实际返回的 `inlineData.mimeType` 选择文件扩展名。

```python theme={"system"}
image_part = next(
    part
    for candidate in result["candidates"]
    for part in candidate["content"]["parts"]
    if "inlineData" in part
)
mime_type = image_part["inlineData"]["mimeType"]
image_bytes = base64.b64decode(image_part["inlineData"]["data"])
```

<Warning>不要假设图片总是第一个 part，也不要假设返回格式一定是 PNG。</Warning>

实测成功响应使用 `role: "model"` 和 `finishReason: "STOP"`。初代模型曾先返回
文字 part 再返回 PNG，而同一次测试中的新模型可能只返回 PNG 或 JPEG 图片 part。

<CardGroup cols={2}>
  <Card title="同步 Playground" icon="play" href="/cn/api-reference/generate-content">
    查看请求结构并准备同步调用。
  </Card>

  <Card title="改用异步任务" icon="clock-3" href="/cn/unified-api/async-tasks">
    立即获得 HTTP 202，并在稍后读取生成结果。
  </Card>

  <Card title="Flash 模型文档" icon="sparkles" href="/cn/models/image/nano-banana/gemini-3.1-flash-image/overview">
    查看该模型独立的尺寸、价格和示例。
  </Card>
</CardGroup>
