Skip to main content
POST
/
v1beta
/
models
/
gemini-3.1-flash-lite-image:generateContent
Text-to-image: generate an image from a text description
curl --request POST \
  --url https://api.apiyi.com/v1beta/models/gemini-3.1-flash-lite-image:generateContent \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "contents": [
    {
      "parts": [
        {
          "text": "A cute Shiba Inu sitting under cherry blossoms, watercolor style, high detail"
        }
      ]
    }
  ],
  "generationConfig": {
    "responseModalities": [
      "IMAGE"
    ],
    "imageConfig": {
      "aspectRatio": "16:9",
      "imageSize": "1K"
    }
  }
}
'
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "inlineData": {
              "mimeType": "image/png",
              "data": "<string>"
            }
          }
        ]
      },
      "finishReason": "STOP"
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 10,
    "candidatesTokenCount": 258
  }
}
The interactive Playground on the right supports dropdown selection for parameters (aspect ratio, response type, etc.). Enter your API Key in the Authorization field (format: Bearer sk-xxx) to send test requests with one click.
Scope: This page is for text-to-image generation. Just enter a prompt — no image upload required. For editing an existing image, use the Image Editing endpoint.
🖥️ Browser Playground limitation (important)This endpoint returns a base64-encoded image (inlineData.data, typically several MB) in the response. Due to browser rendering limits, the Playground on the right may show 请求时发生错误: unable to complete request after the response arrives — the request actually succeeded; the browser just can’t render such a long base64 string.Recommended workflow (beginner-friendly):
  • Copy the Python / Node.js / cURL sample below and run it locally. The code automatically base64.b64decodes the response and writes the image to a file.
  • Nano Banana 2 Lite is focused on the 1K canvas, so the response is moderate in size, but running locally is still the safest way to save the image.

Code Examples

Python

import requests
import base64

API_KEY = "sk-your-api-key"
PROMPT = "A cute Shiba Inu sitting under cherry blossom trees, watercolor style, HD details"

response = requests.post(
    "https://api.apiyi.com/v1beta/models/gemini-3.1-flash-lite-image:generateContent",
    headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
    json={
        "contents": [{"parts": [{"text": PROMPT}]}],
        "generationConfig": {
            "responseModalities": ["IMAGE"],
            "imageConfig": {"aspectRatio": "16:9", "imageSize": "1K"}
        }
    },
    timeout=300
).json()

img_data = response["candidates"][0]["content"]["parts"][0]["inlineData"]["data"]
with open("output.png", 'wb') as f:
    f.write(base64.b64decode(img_data))
print("Image saved to output.png")

cURL

curl -X POST "https://api.apiyi.com/v1beta/models/gemini-3.1-flash-lite-image:generateContent" \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"parts": [{"text": "Futuristic city night view, neon lights, cyberpunk style"}]}],
    "generationConfig": {
      "responseModalities": ["IMAGE"],
      "imageConfig": {"aspectRatio": "16:9", "imageSize": "1K"}
    }
  }'

Node.js

import fs from "fs";

const API_KEY = "sk-your-api-key";

const response = await fetch(
  "https://api.apiyi.com/v1beta/models/gemini-3.1-flash-lite-image:generateContent",
  {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${API_KEY}`,
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      contents: [{ parts: [{ text: "Futuristic city night view, neon lights, cyberpunk style" }] }],
      generationConfig: {
        responseModalities: ["IMAGE"],
        imageConfig: { aspectRatio: "16:9", imageSize: "1K" }
      }
    })
  }
);

const data = await response.json();
const imgBase64 = data.candidates[0].content.parts[0].inlineData.data;
fs.writeFileSync("output.png", Buffer.from(imgBase64, "base64"));

OpenAI-compatible mode

from openai import OpenAI

client = OpenAI(api_key="sk-your-api-key", base_url="https://api.apiyi.com/v1")

response = client.chat.completions.create(
    model="gemini-3.1-flash-lite-image",
    stream=False,
    messages=[{"role": "user", "content": "An autumn landscape painting with red leaves and birds in the distance"}]
)

print(response.choices[0].message.content)

Parameter Quick Reference

ParameterTypeRequiredDescription
contents[].parts[].textstringYesText prompt
generationConfig.responseModalitiesarrayYes["IMAGE"] or ["TEXT","IMAGE"]
generationConfig.imageConfig.aspectRatiostringNo14 ratios, default 1:1
generationConfig.imageConfig.imageSizestringNo1K only (Lite is focused on the 1K canvas)
See the field descriptions in the Playground on the right for detailed parameter docs, allowed values, and defaults. All enum-type fields (like aspectRatio) support dropdown selection — no manual typing needed.
Migrating from Nano Banana 2: just change the model name from gemini-3.1-flash-image to gemini-3.1-flash-lite-image; keep other parameters unchanged. Note Lite supports 1K only — if you pass 2K/4K, switch back to 1K.

Authorizations

Authorization
string
header
required

API Key obtained from the APIYI console

Body

application/json
contents
object[]
required

Content array containing the text prompt

generationConfig
object
required

Response

Image generated successfully

candidates
object[]

Array of generation results

usageMetadata
object