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; native Gemini and fal calls use the SDKs below.
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 { writeFileSync } from 'node:fs';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_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.'
});
for (const part of response.candidates?.[0]?.content?.parts || []) {
if (part.inlineData?.data) {
const ext = part.inlineData.mimeType === 'image/jpeg' ? 'jpg' : 'png';
writeFileSync('result.' + ext, Buffer.from(part.inlineData.data, 'base64'));
}
}Official image SDK documentation ↗Provider example: fal uses its own queue protocol
Install @fal-ai/client and set FAL_KEY on the server. The provider alias below differs from Google’s model ID. This is not an OpenAI-compatible /images/generations endpoint. Read the provider API documentation before changing providers.
import { fal } from '@fal-ai/client';
const result = await fal.subscribe('fal-ai/nano-banana-2', {
input: { prompt: 'A flower shop after rain.',
num_images: 1, resolution: '1K', aspect_ratio: '1:1' }
});
console.log(result.data);fal API 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. The examples were checked against documentation and syntax only; no paid request was sent.
Content reviewed:2026-10-08 · Price verification dates appear alongside each quote; free eligibility follows the linked provider rules.
