Skip to main content
Use the same native Gemini endpoint for text-to-image, reference-image editing, and follow-up edits. Examples use nano-banana-2.1; replace the model in the URL or SDK call to use either of the other two.
BetterToken API does not support search-assisted image generation, including Google Web Search and Image Search grounding. Do not add search tools.

Connection details

Create a BetterToken API Key · model plaza · Nano Banana model launch
These three Nano Banana models do not support POST /v1/images/generations or POST /v1/images/edits; those requests return 400 unsupported_endpoint. Do not reuse GPT Image multipart, image[], or data[].b64_json examples.

Generate your first image

Set BETTERTOKEN_API_KEY in your terminal, then run the request. It waits for generation and saves the full JSON response to response.json.
Change imageSize to 2K or 4K for higher resolution. Start with 1K, 1:1, and one image. Do not add automatic generation retries with --retry.

Common parameters

For product photography, describe the subject, material, lighting, and background. For posters, specify the exact text and layout. For edits, state what should change and what should stay unchanged. Use these fields in native HTTP requests; Python SDK names use snake_case. Place image settings inside generationConfig.imageConfig.

Google-supported aspect ratios

Nano Banana 2 and 2.1 support 14 ratios; Pro supports 10. Start with 1:1 for your first request. Google aspect-ratio and image-size reference The output limit does not guarantee an image count. The model may return one image even if asked for more. For a fixed number of images, send separate requests.

Read and save the image

Images are in candidates[].content.parts[].inlineData. The example below shows the response shape; BASE64_IMAGE_DATA is a placeholder. Read all final image parts and skip intermediate parts marked thought: true.
Run this Python code to save the curl result. Select the extension from mimeType: an image may be JPEG, so do not always save it as .png.

Complete Python SDK example

Use Python 3.10 or later. This example targets google-genai==2.30.1. Install it in a project virtual environment; older SDK releases may omit image_size from the request.
The SDK Base URL is the domain root, without /v1 or /v1beta. timeout is in milliseconds; attempts=1 disables automatic retries. The save_images helper saves every final image.

Send a reference image

Run the following after the Python example to send a new editing request. Replace reference.jpg with your local file. For PNG, use mime_type="image/png". Add further reference images as parts in the same user message.
In native JSON, use inlineData: {"mimeType": "image/jpeg", "data": "BASE64_IMAGE_DATA"}. Encode the original file bytes without a data:image/...;base64, prefix. This integration uses neither multipart nor Files uploads or fileData.

Continue editing

Run this after a successful generation or reference edit. Preserve the entire model response content, including part order and thoughtSignature. Do not rebuild it from the image alone or invent or alter signatures.
Each turn is a new generation request. For further edits, keep the full history, append the new user instruction, then append the returned model content. Do not use the Interactions field previous_interaction_id with this endpoint.

API support

  • Use non-streaming generateContent. Files / fileData, Batch, Interactions, and streaming are not supported by this integration.
  • responseMimeType, responseSchema, and custom image compression formats are not supported. Use only the two imageConfig fields listed above.
  • Omit cachedContent, function calling, code execution, file search, Maps, URL context, and audio input or generation settings.

Using an existing OpenAI Chat client

You can call POST /v1/chat/completions with Base URL https://bettertoken.ai/v1. Use the Gemini native endpoint above for new integrations, reference images, and multi-turn editing. In Chat requests, omit n or set it to integer 1. With raw HTTP, put google.image_config at the JSON root. In the Python OpenAI SDK, pass it through extra_body, which the SDK merges into the root. Do not use generationConfig here.
For the Python OpenAI SDK example, install openai==1.66.2 in the same virtual environment, then run:
Chat returns images as Markdown Data URLs in choices[0].message.content, not the Images API data[] array. Do not reconstruct signed edit history from this text; use the native editing example.

Common errors

Images are delivered in the current response. This workflow has no background image retrieval or task polling endpoint. Save successful results promptly, allow enough time in both SDK and proxy, and disable automatic retries.

Official sources