iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💻

How to Use the Nano Banana (gemini-2.5-flash-image-preview) API for Free

に公開
2025/09/07
8件

Introduction

It seems that the Nano Banana (gemini-2.5-flash-image-preview) API is now available for free on OpenRouter, so I'd like to summarize how to use it.

https://openrouter.ai/google/gemini-2.5-flash-image-preview:free

OpenRouter models can basically be used in the OpenAI API format. I'll test whether image generation and image editing models can also be used in the same way!

If you want to use it from Vertex AI, please refer to the article I wrote the other day.
https://zenn.dev/asap/articles/8d5af79e40fa4d

For more information on OpenRouter, please see the article below.
https://zenn.dev/asap/articles/5cda4576fbe7cb

Repository

Please see the repository below.
https://github.com/personabb/langchain_asap_sample/tree/main/gemini_image_generation

Preparation

Clone the Repository

Clone the repository using the following command.

git clone https://github.com/personabb/langchain_asap_sample.git

In this article, we will use the following folder within the repository.

 cd langchain_asap_sample/gemini2.5_image_generation

Environment Variables

Obtain the OpenRouter API key. Please refer to the article below for instructions.
https://zenn.dev/asap/articles/5cda4576fbe7cb#api-keyの取得

Then, rename the .env.example file in the repository to .env and enter the obtained API information in OPENROUTER_API_KEY.

Setting Up a Virtual Environment

Set up the environment using the following command.

pip install -r requirements.txt

If you are using uv, you can probably set up a virtual environment with the following.

uv sync

Execution

Parameters

You can configure the image to create, the path of the reference image, and how many times to call the API by modifying the following parts in the code.

openrouter_image_editing_api_eng.py
    # ========== Specify the number of images to generate at once ==========
    generate_images = 5

    # ========== Path of the image to load ==========
    file_path = "inputs/images3.png"

    # ========== Specify the editing content ==========
    query = "Turn it into a 3D figure. However, make the pose a cooler, more stylish girl's pose."

    # =================================

Execution Command

You can run it with the following command.

./langchain_asap_sample/gemini2.5_image_generation
python openrouter_image_editing_api_eng.py

How to Call Nano Banana on OpenRouter

openrouter_image_editing_api_eng.py

API_KEY = os.getenv("OPENROUTER_API_KEY", "")
ENDPOINT = "https://openrouter.ai/api/v1"

def call_openrouter_api(system_prompt, user_query, image_base64, api_key, endpoint, temperature=0.7):
    """
    Function to call the OpenRouter API directly
    """
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json",
    }
    
    payload = {
        "model": "google/gemini-2.5-flash-image-preview:free",
        "messages": [
            {
                "role": "system",
                "content": system_prompt
            },
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": user_query
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:image/png;base64,{image_base64}"
                        }
                    }
                ]
            }
        ],
        "temperature": temperature,
        # Enable image generation - Follow OpenRouter format
        "modalities": ["image", "text"]
    }
    
    try:
        response = requests.post(
            f"{endpoint}/chat/completions",
            headers=headers,
            json=payload,
            timeout=60  # Timeout setting
        )
        
        # Return the JSON response
        result = response.json()
        
        return result

I referenced the following page.
https://openrouter.ai/docs/features/multimodal/image-generation

I originally wanted to execute it from LangChain as well, but it seems the ChatOpenAI class doesn't handle image output well, so I gave up.
(It appears that image data is returned from the server, but LangChain cannot return that information properly.)

Summary

Both the Gemini API and Vertex AI are paid when using Nano Banana, so I thought, "It can't be used for free like gemini-2.0-flash-exp-image-generation..." but as expected, OpenRouter delivers!

Also, as mentioned in the article below, write your prompts in English!
If you use Japanese, only text may be output, and the image may not be generated.

https://zenn.dev/asap/articles/8d5af79e40fa4d

Discussion

yaaizzyyaaizzy

初めましてこんにちは、有益な記事を有難うございました。
この記事を参考にして画像生成をしてみましたが、何回かやっているうちにリミットに達したという趣旨のエラーが出て生成できなくなってしまいました。
10ドルチャージしてみたのですが、ダメでした。
もしかして、nano bananaは「無料モデルの制限が1日あたり1,000 requestsに改善」には適応されない感じになってますでしょうか?
あまり詳しくないので、自分のプログラムが悪いのか判断できずに困っております。
今でも無料で通用するものか教えて頂けると有難いです。
よろしくお願いします。

1
asapasap

ご質問ありがとうございます!

openrouter起因のレートリミットか、バックで動いているgoogle ai studio起因のレートリミットかどちらなんでしょうか?

正直どちらでも対処しようがない気もしますが、私が今すぐに試せないので・・・ちゃんと回答できず申し訳ないです。

無料で使われすぎて制限をかけられたとかはあると思います
差し支えなければ何回実施したか教えてもらえると、他の方の後学になるのでありがたいです

1
yaaizzyyaaizzy

ご返事いただき、ありがとうございました!
たぶん、5回程度だったかと思いますので、si6さんと同じかもです。
もしかしたら、10ドル課金はNano bananaには関係ないのかもしれない?!
また、明日にでも試してみます!

1
si6si6

こんばんは!
私もasapさんのこの投稿に従い、OpenRouterでNanoBananaAPIを叩くことに成功しました。
私は10ドルの課金はせず、ご案内の通りpython openrouter_image_editing_api_eng.pyを実行したところ、画像が5つ生成された後にそれ以上生成できない状況となりました。
状況とするとyaaizzyさんと同じ状況かと思われます。
取り急ぎ共有いたします。とても参考になる投稿、ありがとうございました!!

2
yaaizzyyaaizzy

共有ありがとうございました!
ちなみにですが、Nano bananaで無料生成したいだけなら、Google AI StudioのBuildでアプリ作ると50枚くらいいけるっぽいですよ!

1
asapasap

@yaaizzy
@si6

お二方ともありがとうございます。
日本時間朝9時解除で5回の制限がかかっているようです。
ご共有ありがとうございます

2
tacca884tacca884

Google: Gemini 2.5 Flash Image Previewのfreeが出てこなかったのですが、もしかしてfreeは無くなったのですかね?Prompt Pricingをfreeにした中には出てこないですね。

1
たけるDたけるD

free版は恐らく今現在使えない状態になっていますね。リストから外されており、model=に入れて画像生成しようとしても404エラーが返ってきてしまいます。
「:free」の部分は消して、「Google: Gemini 2.5 Flash Image Preview」として通常通り使うしか今は仕方なさそうです。

2