Get a key and identify the model
Create a Gemini API key in Google AI Studio and check project billing and model availability. Key creation does not guarantee free image generation. Keep keys on the server in environment variables. Official key guide ↗
- Nano Banana 2:
gemini-3.1-flash-image - Nano Banana Pro:
gemini-3-pro-image - Nano Banana 2.1:
gemini-nano-banana-2.1 - Nano Banana Lite:
gemini-3.1-flash-lite-image
Gemini uses https://generativelanguage.googleapis.com and the /v1beta/models/{model}:generateContent path. Provider base URLs and aliases must follow their own documentation. The site model test supports OpenAI-compatible synchronous APIs; ImgAPI uses an OpenAI-compatible endpoint; native Gemini uses the official SDK below.
Provider example: connect to ImgAPI
Use Node.js 18+ and set IMGAPI_CARD_KEY on the server. The example generates one Nano Banana 2 image. To use Nano Banana Pro, change model to nano-banana-pro.
| Setting | Value |
|---|---|
| Base URL | https://imgapi.vip/prod-api/v1 |
| Authentication | Authorization: Bearer $IMGAPI_CARD_KEY |
| Nano Banana 2 | nano-banana-2 |
| Nano Banana Pro | nano-banana-pro |
curl --fail-with-body --max-time 300 \
'https://imgapi.vip/prod-api/v1/images/generations' \
-H "Authorization: Bearer $IMGAPI_CARD_KEY" \
-H 'Content-Type: application/json' \
--data '{
"model": "nano-banana-2",
"prompt": "A flower shop after rain.",
"size": "1024x1024",
"quality": "auto",
"n": 1,
"response_format": "url"
}'This request is billed by ImgAPI. Keep the key on the server; do not automatically resubmit after a timeout. Check the original task status first. ImgAPI API documentation
Official Gemini SDK example
Install @google/genai. Set GEMINI_API_KEY on the server; change GEMINI_IMAGE_MODEL explicitly when switching models. This example uses the Gemini generateContent protocol.
import { GoogleGenAI } from '@google/genai';
import { writeFile } from 'node:fs/promises';
import { randomUUID } from 'node:crypto';
const key = process.env.GEMINI_API_KEY;
if (!key) throw new Error('Set GEMINI_API_KEY on the server');
const ai = new GoogleGenAI({ apiKey: key });
const response = await ai.models.generateContent({
model: process.env.GEMINI_IMAGE_MODEL || 'gemini-3.1-flash-image',
contents: 'Generate an image of a flower shop after rain.'
});
async function saveGeminiImages(response, writeImage, prefix) {
const extensions = { 'image/png': 'png', 'image/jpeg': 'jpg', 'image/webp': 'webp' };
const images = (response.candidates || []).flatMap(candidate => candidate.content?.parts || [])
.map(part => part.inlineData).filter(Boolean);
if (!images.length) throw new Error('No image returned');
for (const [index, image] of images.entries()) {
const ext = extensions[image.mimeType];
if (!ext) throw new Error('Unsupported image format: ' + image.mimeType);
if (typeof image.data !== 'string' || !image.data) throw new Error('Empty image data');
const bytes = Buffer.from(image.data, 'base64');
if (!bytes.length) throw new Error('Empty image data');
await writeImage(prefix + '-' + (index + 1) + '.' + ext, bytes, { flag: 'wx' });
}
}
await saveGeminiImages(response, writeFile, 'result-' + randomUUID());Official image SDK documentation ↗Troubleshoot before retrying
401: verify the key and authentication format. 403: check permissions and regional availability. 404: check model alias and endpoint. 429: check quota and request limits. A timeout can occur after billing; check the request status before submitting another job.
Guide revised:2026-10-10
