Skip to main content

Nano Banana API in 2026: Official Gemini Models, API Key, Pricing, and Code

A
18 min readAPI Guide

Nano Banana API in 2026 means Gemini API image generation. Start with `gemini-3.1-flash-image`, switch to `gemini-3-pro-image` only for Pro workloads, and check key, billing, limits, and current price rows before you copy code.

Nano Banana API in 2026: Official Gemini Models, API Key, Pricing, and Code

The official developer route for Nano Banana image generation in 2026 is Gemini API image generation. Use gemini-3.1-flash-image for Nano Banana 2 by default, and switch to gemini-3-pro-image only when the workload needs Pro-level text rendering, 4K/pro assets, or more complex layout reasoning.

Create the key in Google AI Studio, but separate the credential from billing, live project limits, consumer app quotas, and provider credits before you copy code. The current official price rows below were checked against Google docs on June 22, 2026, and the image-output rows this setup path uses do not show a free tier.

Start Here: The Official Route And Current Model IDs

Nano Banana is the reader-facing name people use for Google's image models, but the implementation path is not a separate Nano Banana developer console. The official path is the Gemini API image-generation documentation, with Google AI Studio as the quick key and testing surface.

That distinction matters because the wrong surface changes the answer. Gemini Apps and AI Mode are consumer products with their own quotas and interface rules. Provider gateways can be useful infrastructure, but they own their own endpoints, pricing, and terms. The official setup starts with Gemini API, then decides model, key, billing, and code.

Reader jobStart hereWhy it is the right first moveStop rule
Build a product integrationGemini APIIt is the official programmatic route for generateContent and inline image outputDo not treat consumer app limits as API quota
Test prompts before wiring codeGoogle AI StudioIt uses Google's official developer stack and helps inspect output fastDo not treat key creation as free paid usage
Use an everyday UIGemini Apps or AI ModeIt is a consumer workflow, not a developer integrationDo not copy consumer quotas into API math
Centralize vendors or billingProvider or gatewayIt may simplify multi-model infrastructureDo not call provider pricing official Google pricing

The current model mapping is straightforward:

Model familyCurrent model IDBest first use
Nano Banana 2gemini-3.1-flash-imageDefault image generation and editing for speed, cost balance, and volume
Nano Banana Progemini-3-pro-imageProfessional assets, stronger text rendering, 4K output, and complex layout reasoning
Older Nano Banana pathgemini-2.5-flash-imageExisting low-latency workflows already tuned for the older model

Older code and older posts may still use gemini-3.1-flash-image-preview or gemini-3-pro-image-preview. Do not use those as the current default IDs in a new integration unless you are intentionally maintaining an older compatibility path.

Model Choice And Current Official Pricing

Start with Nano Banana 2 unless you can name the reason to pay for Pro. Google positions Gemini 3.1 Flash Image / Nano Banana 2 as the high-efficiency image model for speed and high-volume use cases. Pro is the override when output quality, rendered text, complex visual instructions, or 4K/professional asset production is the real bottleneck.

Model and price chooser comparing Nano Banana 2 and Nano Banana Pro with current Gemini model IDs, Standard pricing, Batch/Flex pricing, and verified date

The model choice and cost choice should be made together. As of June 22, 2026, the Gemini Developer API pricing page lists these image-output rows for the models used in this setup path:

ModelStandard image outputBatch/Flex image outputDefault decision
Nano Banana 2 (gemini-3.1-flash-image)0.5K $0.045, 1K $0.067, 2K $0.101, 4K $0.1510.5K $0.022, 1K $0.034, 2K $0.050, 4K $0.076Use first for most integrations
Nano Banana Pro (gemini-3-pro-image)1K/2K $0.134, 4K $0.241K/2K $0.067, 4K $0.12Use when the workload justifies Pro

The useful interpretation is not "Pro is better, so start there." A better rule is:

  • Start with gemini-3.1-flash-image when you need speed, iteration, broad image editing, or meaningful volume.
  • Move to gemini-3-pro-image when the extra cost is tied to a real output requirement: text, layout, 4K, or professional fidelity.
  • Use Batch/Flex only when the job can tolerate asynchronous or flexible execution. It lowers official cost, but it is not a drop-in answer for every real-time product path.

For a deeper cost-only breakdown, use the sibling Nano Banana 2 pricing guide, Nano Banana Pro pricing guide, or Gemini 3 Pro Image pricing guide. Keep pricing tied to the setup path so the first request uses the right model from the start.

API Key, Billing, And Live Limits Before Code

Create the API key in Google AI Studio, then store it as an environment variable. The quickstart path is simple, but the operating boundary is not optional:

  • The API key authenticates your request.
  • The Google Cloud project and billing account own cost.
  • Live limits are project-level, not independent properties of a copied key.
  • Region and eligibility rules still apply.
  • Provider credits or consumer app quotas do not prove official API entitlement.

Google's API key documentation also adds a 2026 key-management caveat: dormant unrestricted keys can be blocked, and new AI Studio keys are created as auth keys. Treat that as a reason to restrict keys and keep them out of source code, not as a reason to bury key setup under wrapper advice.

Use a local .env or your deployment platform's secret manager:

bash
export GEMINI_API_KEY="YOUR_KEY_HERE"

For production, keep three checks next to the key:

CheckWhere to verifyWhy it matters
BillingGoogle billing / Gemini billing docsA working key can still fail or cost differently if the project billing contract is wrong
Rate limitsAI Studio or the rate-limit docsLimits are project-level and can vary by tier and model
Region / eligibilityGemini available regions and termsAvailability is not universal across every country, account, or user profile

This is also where many "free API key" claims break down. A free key creation flow is not the same as free image output. For current Nano Banana image API work, use the official pricing row as the cost owner and treat consumer quotas as a different product surface.

Python First Request

Install the current Python SDK:

bash
pip install -U google-genai pillow

Then make the smallest useful request with the default Nano Banana 2 model:

python
import os from google import genai from google.genai import types client = genai.Client(api_key=os.environ["GEMINI_API_KEY"]) response = client.models.generate_content( model="gemini-3.1-flash-image", contents=[ "Create a clean 16:9 technical illustration of an API key becoming an image output." ], config=types.GenerateContentConfig( response_modalities=["Image"], response_format={ "image": { "aspect_ratio": "16:9", "image_size": "2K", } }, ), ) for part in response.parts: image = part.as_image() if image is not None: image.save("nano-banana-output.png")

The important parts are the model ID, response_modalities=["Image"], and the response_format.image block. If you need text plus image in the response, use both modalities and handle both part.text and image parts. If you need only the image, keep the request narrow and save the returned image explicitly.

Switching to Pro should be a small code change, not a separate architecture:

python
model="gemini-3-pro-image"

Make that swap only after a real output test shows the default model is not enough.

JavaScript First Request

Install the current JavaScript package:

bash
npm install @google/genai

Then call the same default model:

javascript
import { GoogleGenAI } from "@google/genai"; import fs from "node:fs"; const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); const response = await ai.models.generateContent({ model: "gemini-3.1-flash-image", contents: "Create a clean 16:9 technical illustration of an API key becoming an image output.", config: { responseModalities: ["Image"], responseFormat: { image: { aspectRatio: "16:9", imageSize: "2K", }, }, }, }); for (const part of response.candidates?.[0]?.content?.parts ?? []) { const inline = part.inlineData; if (!inline?.data) continue; fs.writeFileSync("nano-banana-output.png", Buffer.from(inline.data, "base64")); }

If an older tutorial imports @google/generative-ai, do not copy it blindly into a new 2026 integration. Use the current @google/genai package unless you are maintaining legacy code and have a compatibility reason.

First request flow from AI Studio API key to GEMINI_API_KEY, current GenAI clients, gemini-3.1-flash-image, generateContent, inline image bytes, and saved PNG

REST Request And Response Shape

REST is useful when you need to verify the wire contract without SDK behavior. Use the stable model ID in the endpoint:

bash
curl -s -X POST \ "https://generativelanguage.googleapis.com/v1/models/gemini-3.1-flash-image:generateContent" \ -H "x-goog-api-key: ${GEMINI_API_KEY}" \ -H "Content-Type: application/json" \ -d '{ "contents": [{ "parts": [{ "text": "Create a clean 16:9 technical illustration of an API key becoming an image output." }] }], "generationConfig": { "responseModalities": ["Image"], "responseFormat": { "image": { "aspectRatio": "16:9", "imageSize": "2K" } } } }'

In raw responses, look for inline image data under candidate content parts. The exact SDK wrapper may differ, but the practical job is the same: find the image part, read the MIME type, decode the base64 payload, and write it as an image file.

Response handling is where many first tests look broken even when the request succeeded. A 200 response with no saved file usually means the client code did not walk the image part correctly, requested a mixed text/image response and ignored the image part, or assumed a URL output when the response actually returned inline data. If you hit that shape, compare your handler with the JavaScript and Python loops above before changing models.

Optional Controls Worth Adding Deliberately

Do not add every advanced option to the first request. Add controls when they solve a real workflow issue:

ControlUse whenSetup note
aspectRatio / aspect_ratioThe product needs a fixed format such as 16:9, 1:1, or vertical mobile outputPut it under the image response format
imageSize / image_sizeYou need a specific output tier such as 1K, 2K, or 4KTie the choice to the pricing table
Image inputsYou need editing, composition, or reference-image workMake sure the chosen model supports the input count and fidelity need
Google Search groundingThe image needs fresh factual contextTreat grounding as a separate capability and cost concern
Pro model swapText rendering, layout reasoning, or 4K/pro asset fidelity is the blockerChange only the model ID first, then retest

Google also documents that generated images include SynthID watermarking. If your product has provenance or disclosure requirements, that is a reason to understand the official route before you decide whether a provider route still helps.

Official Google Route Vs Provider Routes

Provider and gateway routes can be useful. They may give you OpenAI-compatible request formats, a single billing relationship, retry behavior across models, or a preferred operations layer. None of that changes the official Google API contract.

Route boundary map separating official Google API, consumer UI quotas, and provider or gateway contracts, with a stop rule against calling provider pricing official Google pricing

Use this boundary:

RouteWhat it ownsWhat it does not prove
Official Google APIGoogle model IDs, official docs, project billing, official pricing, Google rate limitsProvider flat prices or wrapper guarantees
Consumer UIGemini Apps / AI Mode experience, app quotas, consumer plan behaviorAPI billing, SDK behavior, or project limits
Provider / gatewayWrapper endpoint, wrapper pricing, gateway terms, compatibility layerOfficial Google price rows or official availability

If a provider route materially improves your workflow, compare it after the official setup is clear. The comparison questions are practical:

  • Does it support the current target model?
  • Does it preserve the image controls you need?
  • Does it expose response data in a format your app can handle?
  • Does it state retry, refund, latency, and failure behavior clearly?
  • Does its price belong to the provider contract rather than Google's official price sheet?

That last question is the stop rule. A lower provider price can be real and still not be official Google pricing.

Quick Troubleshooting Checklist

Use this order before changing providers or rewriting the whole integration:

  1. Confirm the model ID is current: gemini-3.1-flash-image or gemini-3-pro-image.
  2. Confirm the key is loaded from GEMINI_API_KEY and not hardcoded or missing in the runtime.
  3. Confirm billing is enabled and the project is the one attached to the key.
  4. Confirm live project limits in AI Studio if you see quota or rate-limit errors.
  5. Confirm your response handler saves inline image bytes instead of expecting a hosted URL.
  6. Confirm your requested image size matches both the model's supported options and your cost expectations.
  7. Confirm you are not mixing consumer app quotas, AI Mode behavior, or provider credits into official API logic.

For error-specific work after setup, use the Gemini image common errors guide or the Nano Banana Pro troubleshooting hub.

Frequently Asked Questions

What is the official Nano Banana API model ID in 2026?

Use gemini-3.1-flash-image for Nano Banana 2 and gemini-3-pro-image for Nano Banana Pro. Treat older *-preview IDs as legacy or historical code unless you have a specific compatibility reason.

Should I start with Nano Banana 2 or Nano Banana Pro?

Start with Nano Banana 2. Move to Pro when the output test shows a real need for stronger text rendering, complex layout reasoning, 4K/pro asset quality, or a higher-fidelity professional result.

How do I get a Nano Banana API key?

Create the key in Google AI Studio, store it in GEMINI_API_KEY, and keep billing plus live project limits separate from the key itself. A key is a credential, not a standalone billing or free-tier promise.

Is the Nano Banana API free?

The current image-output rows used by this setup path do not show a free tier on the Gemini Developer API pricing page. Consumer quotas and provider credits are separate product or provider contracts.

What packages should I use for Python and JavaScript?

Use google-genai for Python and @google/genai for JavaScript. Older package names can still appear in stale examples, but they are not the best current default for a new integration.

Does the API return an image URL or image bytes?

The official setup path above handles inline image bytes. Save the returned image part as a PNG or other MIME-appropriate file. Do not assume the response returns a hosted image URL.

When should I use a provider or gateway?

Use one when you can name the infrastructure benefit: OpenAI-compatible routing, unified billing, retries, governance, or multi-model orchestration. Keep provider pricing and limits separate from official Google pricing.

Where should I go after this setup?

For broader model-family context, read the Gemini Image API guide. For Pro economics, read the Nano Banana Pro API guide. For workload choice, read Nano Banana Pro vs Nano Banana 2.

Share:

laozhang.ai

One API, All AI Models

AI Image

Gemini 3 Pro Image

$0.05/img
80% OFF
AI Video

Sora 2 · Veo 3.1

$0.15/video
Async API
AI Chat

GPT · Claude · Gemini

200+ models
Official Price
Served 100K+ developers
|@laozhang_cn|Get $0.1