---
name: nano-banana-image
skill: true
description: Generate and edit images using Google's Nano Banana models via the Gemini REST API.
tags:
  - image-generation
  - nano-banana
  - gemini-api
---

# Skill: Nano Banana Image Generator

Generate and edit images using Google's Nano Banana models (Gemini native image generation) via the REST API.

## When to Activate

Use this skill when the user asks to:
- "Create an image with nano banana"
- "Generate an image using nano banana API"
- "Make a picture with nano banana / Gemini image"
- "Edit an image with nano banana"
- Any request involving image generation or editing via Nano Banana / Gemini image models

## Prerequisites

- A Gemini API key from [Google AI Studio](https://aistudio.google.com/app/apikey). Set it as `GEMINI_API_KEY` in the environment.

## Available Models

| Nano Banana name | Gemini model ID | Best for |
|---|---|---|
| Nano Banana 2 | `gemini-3.1-flash-image` | Speed, high-volume, 512px–2K |
| Nano Banana Pro | `gemini-3-pro-image` | Professional assets, 4K, complex text |
| Nano Banana | `gemini-2.5-flash-image` | Speed, efficiency, general use |

Default model: `gemini-3.1-flash-image`

## API Endpoint

```
POST https://generativelanguage.googleapis.com/v1beta/models/{MODEL}:generateContent?key={GEMINI_API_KEY}
```

## Process

### 1. Capture the Prompt

Extract the image description from the user's request. If vague, ask for clarification or offer to enhance it.

### 2. Call the API (Text-to-Image)

Use `bash_run` with `curl`:

```bash
MODEL="gemini-3.1-flash-image"
PROMPT="A glossy magazine cover with the words Nano Banana in bold serif font"
API_KEY="${GEMINI_API_KEY:?Missing GEMINI_API_KEY}"

curl -s -X POST \
  "https://generativelanguage.googleapis.com/v1beta/models/${MODEL}:generateContent?key=${API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{
      "parts": [{"text": "'"${PROMPT}"'"}]
    }],
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"]
    }
  }' \
  -o /tmp/nano_banana_response.json
```

> **Note:** The API requires `responseModalities` to include `"IMAGE"` or the model will return text only.

### 3. Parse the Response

Read `/tmp/nano_banana_response.json` with `read`. The generated image(s) are inside `candidates[0].content.parts` as `inlineData` objects:

```json
{
  "candidates": [{
    "content": {
      "parts": [
        {"text": "Optional description"},
        {
          "inlineData": {
            "mimeType": "image/png",
            "data": "iVBORw0KGgoAAAANSUhEUgAAAA..."
          }
        }
      ]
    }
  }]
}
```

Extract and save the image(s) with `bash_run`:

```bash
# Save first image part to a file
jq -r '.candidates[0].content.parts[] | select(.inlineData) | .inlineData.data' /tmp/nano_banana_response.json | head -n 1 | base64 -d > ./generated_image.png

# Optional: list all generated images
jq -r '.candidates[0].content.parts | to_entries[] | select(.value.inlineData) | "image_\(.key).\(.value.inlineData.mimeType | split(\"/\")[1])"' /tmp/nano_banana_response.json
```

### 4. Deliver the Result

- Report the saved file path(s).
- If the response contains `promptFeedback.blockReason`, report the block reason (safety, prohibited content, etc.).
- If no `inlineData` is present, show the raw JSON error or text response.

## Image Editing (Image-to-Image)

To edit an existing image, encode it to base64 and include it as an `inlineData` part:

```bash
IMAGE_B64=$(base64 -i ./input.jpg | tr -d '\n')
MODEL="gemini-3.1-flash-image"
API_KEY="${GEMINI_API_KEY:?Missing GEMINI_API_KEY}"

jq -n \
  --arg prompt "Change the background to a cyberpunk city" \
  --arg image_b64 "$IMAGE_B64" \
  '{
    "contents": [{
      "parts": [
        {"text": $prompt},
        {
          "inlineData": {
            "mimeType": "image/jpeg",
            "data": $image_b64
          }
        }
      ]
    }],
    "generationConfig": {
      "responseModalities": ["TEXT", "IMAGE"]
    }
  }' > /tmp/request.json

curl -s -X POST \
  "https://generativelanguage.googleapis.com/v1beta/models/${MODEL}:generateContent?key=${API_KEY}" \
  -H "Content-Type: application/json" \
  -d @/tmp/request.json \
  -o /tmp/nano_banana_response.json
```

## Optional Parameters

- **Aspect ratio / resolution:** The Gemini 3 image models support `aspectRatio` and `imageSize` (e.g. `"1:1"`, `"16:9"`, `"512"`, `"1K"`, `"2K"`, `"4K"`). If needed, add them inside `generationConfig` or as a `responseFormat` object (check latest docs for exact REST field names).
- **Safety settings:** Add `safetySettings` array to the request body to tune content filtering.
- **Thinking mode:** Nano Banana Pro uses thinking by default for complex prompts.

## Example Interaction

**User:** Create an image of a cyberpunk cat using nano banana

**Action:**
1. Call API with model `gemini-3.1-flash-image` and prompt `"A cyberpunk cat with neon lights, futuristic city background, digital art style"`.
2. Parse response, extract `inlineData` base64, save to `./cyberpunk_cat.png`.
3. Respond: `Generated image: ./cyberpunk_cat.png`

## Error Handling

- **Missing `GEMINI_API_KEY`:** Ask the user to create one at Google AI Studio and export it.
- **BLOCKED / safety:** If `promptFeedback.blockReason` exists, show the reason and ask the user to rephrase.
- **No image returned:** Verify `responseModalities` includes `"IMAGE"`. If still missing, show the raw JSON.
- **API errors:** Show the full JSON error from the response.
