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

# Gemini 图片生成示例

> Gemini 图片生成与编辑示例。

## 推荐 endpoint

* [Gemini /v1beta/models/{model}:generateContent](/api-reference/endpoints/gemini/image)

## 最小请求

```json theme={null}
{
  "model": "gemini-2.5-flash-image",
  "contents": [
    {
      "role": "user",
      "parts": [{ "text": "生成一张简洁的 ApiGo 产品海报" }]
    }
  ],
  "generationConfig": {
    "responseModalities": ["TEXT", "IMAGE"]
  }
}
```

## cURL 示例

```bash theme={null}
curl -s "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "parts": [{ "text": "生成一张简洁的 ApiGo 产品海报" }]
      }
    ]
  }'
```

## Python 示例

```python theme={null}
import base64
import requests

response = requests.post(
    "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent",
    headers={
        "x-goog-api-key": "<GEMINI_API_KEY>",
        "Content-Type": "application/json",
    },
    json={
        "contents": [
            {
                "parts": [{"text": "生成一张简洁的 ApiGo 产品海报"}],
            }
        ]
    },
    timeout=60,
)
response.raise_for_status()

for part in response.json()["candidates"][0]["content"]["parts"]:
    if "inlineData" in part:
        image_bytes = base64.b64decode(part["inlineData"]["data"])
        with open("apigo-poster.png", "wb") as f:
            f.write(image_bytes)
        print("saved apigo-poster.png")
```

## Node.js 示例

```js theme={null}
import { writeFileSync } from "node:fs";

const response = await fetch(
  "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image:generateContent",
  {
    method: "POST",
    headers: {
      "x-goog-api-key": process.env.GEMINI_API_KEY,
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      contents: [
        {
          parts: [{ text: "生成一张简洁的 ApiGo 产品海报" }]
        }
      ]
    })
  }
);

const data = await response.json();
for (const part of data.candidates[0].content.parts) {
  if (part.inlineData) {
    writeFileSync(
      "apigo-poster.png",
      Buffer.from(part.inlineData.data, "base64")
    );
    console.log("saved apigo-poster.png");
  }
}
```

## 最佳实践

* 图片理解和图片生成都走 `generateContent`，但结果解析方式不同
* 图片结果通常在 `inlineData` 中，服务端先解码再落盘
* 需要复用参考图时，优先用 Files API 而不是每次内联上传
