iTranslated by AI
How to Use the Nano Banana (gemini-2.5-flash-image-preview) API for Free
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.
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.
For more information on OpenRouter, please see the article below.
Repository
Please see the repository below.
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.
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.
# ========== 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.
python openrouter_image_editing_api_eng.py
How to Call Nano Banana on OpenRouter
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.
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.
Discussion
初めましてこんにちは、有益な記事を有難うございました。
この記事を参考にして画像生成をしてみましたが、何回かやっているうちにリミットに達したという趣旨のエラーが出て生成できなくなってしまいました。
10ドルチャージしてみたのですが、ダメでした。
もしかして、nano bananaは「無料モデルの制限が1日あたり1,000 requestsに改善」には適応されない感じになってますでしょうか?
あまり詳しくないので、自分のプログラムが悪いのか判断できずに困っております。
今でも無料で通用するものか教えて頂けると有難いです。
よろしくお願いします。
ご質問ありがとうございます!
openrouter起因のレートリミットか、バックで動いているgoogle ai studio起因のレートリミットかどちらなんでしょうか?
正直どちらでも対処しようがない気もしますが、私が今すぐに試せないので・・・ちゃんと回答できず申し訳ないです。
無料で使われすぎて制限をかけられたとかはあると思います
差し支えなければ何回実施したか教えてもらえると、他の方の後学になるのでありがたいです
ご返事いただき、ありがとうございました!
たぶん、5回程度だったかと思いますので、si6さんと同じかもです。
もしかしたら、10ドル課金はNano bananaには関係ないのかもしれない?!
また、明日にでも試してみます!
こんばんは!
私もasapさんのこの投稿に従い、OpenRouterでNanoBananaAPIを叩くことに成功しました。
私は10ドルの課金はせず、ご案内の通りpython openrouter_image_editing_api_eng.pyを実行したところ、画像が5つ生成された後にそれ以上生成できない状況となりました。
状況とするとyaaizzyさんと同じ状況かと思われます。
取り急ぎ共有いたします。とても参考になる投稿、ありがとうございました!!
共有ありがとうございました!
ちなみにですが、Nano bananaで無料生成したいだけなら、Google AI StudioのBuildでアプリ作ると50枚くらいいけるっぽいですよ!
@yaaizzy
@si6
お二方ともありがとうございます。
日本時間朝9時解除で5回の制限がかかっているようです。
ご共有ありがとうございます
Google: Gemini 2.5 Flash Image Previewのfreeが出てこなかったのですが、もしかしてfreeは無くなったのですかね?Prompt Pricingをfreeにした中には出てこないですね。
free版は恐らく今現在使えない状態になっていますね。リストから外されており、model=に入れて画像生成しようとしても404エラーが返ってきてしまいます。
「:free」の部分は消して、「Google: Gemini 2.5 Flash Image Preview」として通常通り使うしか今は仕方なさそうです。