Nano Banana 2.1 API
The Gemini API model ID is gemini-nano-banana-2.1. With a key from Google AI Studio, your server can send a prompt to generateContent and save the returned image. The examples below cover a text prompt and a photo edit.
Base 15 + references 0 + prompt 0 = 15 credits
Reference images and longer prompts may add credits. The total is shown before you generate.

ExampleA cozy coffee shop at golden hour, 35mm film photo, warm light through the window, a neon sign in the window that reads "OPEN LATE"
Real results

Prompt prototyped in the generator
A 3D mascot made with Nano Banana 2.1. Use the same prompt in the request below.
Prompt used for this image · 1:1
A friendly 3D banana mascot wearing round sunglasses and headphones, studio lighting, pastel yellow background, animated film style
What this site offers
You can test a prompt in this site’s web generator using site credits. For calls from your own app, create a key for a billing-enabled project in Google AI Studio. Google bills the project that owns the key.
Options that matter
responseModalities must include IMAGE. imageConfig.aspectRatio accepts ratios such as 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3, 5:4, 4:5 and 21:9, plus the 4:1, 8:1, 1:4 and 1:8 strips. imageConfig.imageSize selects 1K, 2K or 4K. For editing, add up to 14 reference images as inlineData parts before the text part.
The response contains candidates with parts. Image parts carry base64 data in inlineData. Skip any interim images flagged as thoughts when saving the output. If no image comes back, read promptFeedback.blockReason and candidate.finishReason.
Request examples
Set GEMINI_API_KEY in your server environment. The curl and Node.js examples pass it to Google in the x-goog-api-key header.
curl -s "https://generativelanguage.googleapis.com/v1beta/models/gemini-nano-banana-2.1:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"role": "user",
"parts": [{ "text": "A cozy coffee shop at golden hour, 35mm film photo" }]
}],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": { "aspectRatio": "3:2", "imageSize": "2K" }
}
}'import { writeFile } from 'node:fs/promises';
const model = 'gemini-nano-banana-2.1';
const res = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-goog-api-key': process.env.GEMINI_API_KEY,
},
body: JSON.stringify({
contents: [{ role: 'user', parts: [{ text: 'A friendly 3D banana mascot, studio lighting' }] }],
generationConfig: {
responseModalities: ['TEXT', 'IMAGE'],
imageConfig: { aspectRatio: '1:1', imageSize: '1K' },
},
}),
}
);
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
const parts = data.candidates?.[0]?.content?.parts ?? [];
// Skip interim "thought" images, keep the final one.
const image = parts.find((p) => p.inlineData?.data && !p.thought);
if (!image) throw new Error('no image returned');
await writeFile('mascot.png', Buffer.from(image.inlineData.data, 'base64'));{
"contents": [{
"role": "user",
"parts": [
{ "inlineData": { "mimeType": "image/jpeg", "data": "<base64 of your photo>" } },
{ "text": "Replace the background with a minimalist white studio. Keep the person exactly the same." }
]
}],
"generationConfig": {
"responseModalities": ["TEXT", "IMAGE"],
"imageConfig": { "aspectRatio": "4:5", "imageSize": "2K" }
},
"tools": [{ "googleSearch": {} }]
}How to do it
- 01
Prototype the prompt
Use the generator on this page. Settle the prompt, the ratio and the resolution before writing code.
- 02
Create a Gemini API key
Create the key in Google AI Studio and keep it on your server. Never ship it in browser code.
- 03
Send the request and store the image
Decode the base64 data and save it to your own storage. Handle errors and blocked prompts explicitly.
Tips
Keep the key on the server
Call the API from your backend and expose your own endpoint to the front end.
Set timeouts and retries
Image generation takes longer than a typical API call. Use a generous timeout and retry only on transient errors.
Mind the cost of 4K
Draft at 1K and render the final at 4K, as in the web generator.
Questions
- Does NanoBanana 2.1 offer an API?
- No. The site is a web generator with credits. To call Nano Banana 2.1 from code, use the Gemini API with your own key.
- Which model ID should I use?
- Use gemini-nano-banana-2.1. In a generateContent REST request, the model ID goes in the URL between models/ and :generateContent.
- How do I get 4K output from the API?
- Set generationConfig.imageConfig.imageSize to "4K". Valid values are 1K, 2K and 4K.
- Can I edit existing photos through the API?
- Yes. Send the photo as an inlineData part followed by the text instruction. The example above shows the shape.
Keep exploring
Make your first Nano Banana 2.1 image
Describe your first image and review the credit total before you generate.