Image generation
How to use the image API (generation, editing, Nano Banana), the fields each model accepts, how to write GPT Image 2 requests, requests that are rejected, and errors.
There are three ways to generate images. Each model uses one of them.
Once you have chosen a model, click its name in the table below. Each model has its own code that you can copy and run as is.
| Way to call | Method and path | Format | Models | Charged per |
|---|---|---|---|---|
| Generation | POST / | JSON | Models marked "Generation" in the table below | Image |
| Editing | POST / | Form format (multipart/) | The two models whose names end in -edit | Image |
| Nano Banana | POST / | JSON (same as text models) | The three Nano Banana models | Token |
The base URL and the way you send the key are the same as for the other OpenAI-format endpoints (https://token.echohub.io/v1, Authorization: Bearer key). Put the key in the environment variable ECHO_API_KEY (Quickstart).
The models currently offered are listed in the model list.
Models
| Model ID | Provider | How to call | Fields you must write and must not write |
|---|---|---|---|
gpt-image-2-low, gpt-image-2-medium, gpt-image-2-high (GPT Image 2 at low, medium and high quality) | OpenAI | Generation | Always set quality to the quality in the model name (low, medium, high). size must be 1024x1024, 1536x1024 or 1024x1536. n must be 1. See "GPT Image 2" |
flux-2-flex, flux-2-klein-4b, flux-2-klein-9b, flux-2-max, flux-2-pro, flux-3 | Black Forest Labs | Generation | The request that was tested is in "Examples" |
ideogram-4.5, ideogram-v3 | Ideogram | Generation | Same as above |
kling-image-o3 | Kuaishou (Kling) | Generation | Same as above |
qwen-image-2.0 | Alibaba Cloud | Generation | Same as above |
qwen-image-3. | Alibaba Cloud | Generation | Do not write size (it returns HTTP 400). Output is always 1K and the aspect ratio cannot be chosen |
wan-image-2.6, wan-image-2.7, wan-image-2. | Alibaba Cloud | Generation | The request that was tested is in "Examples" |
recraft-v4.1 | Recraft | Generation | Same as above |
seedream-4.5, seedream-5., seedream-5., seedream-5.5 | ByteDance (Volcano Engine) | Generation | Same as above |
seedream-5., qwen-image-3. | ByteDance (Volcano Engine), Alibaba Cloud | Editing | Only one input image (image) |
gemini-nano-banana-2. (Nano Banana 2.1), gemini-3. (Nano Banana 2), gemini-3-pro-image (Nano Banana Pro) | Nano Banana | See "Notes on Nano Banana" |
- Prices are on each model's page in the model list (you can also open them from "Pricing" on the page that each model ID in the table opens). Generation and editing are charged per image; Nano Banana is charged by output tokens
- The processing region is on each model's page. The prompt and input images are passed to the provider's API that processes in that region (Data handling)
Examples
Generation
This creates one 1024x1024 image with GPT Image 2 (low quality) and saves it as image.png. For the other generation models, change model and leave out quality (qwen-image-3.0-pro also takes no size).
curl "https://token.echohub.io/v1/images/generations" \
-H "Authorization: Bearer $ECHO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2-low",
"prompt": "A Shiba Inu walking on a beach at dusk",
"n": 1,
"size": "1024x1024",
"quality": "low"
}'data[0] in the returned JSON holds either the image address (url) or the image itself (b64_json). b64_json is the image file written as a Base64 string.
# Create and activate a virtual environment
python3 -m venv .venv && source .venv/bin/activate
pip install openaiimport base64
import os
import urllib.request
from openai import OpenAI
client = OpenAI(
base_url="https://token.echohub.io/v1",
api_key=os.environ["ECHO_API_KEY"],
)
result = client.images.generate(
model="gpt-image-2-low",
prompt="A Shiba Inu walking on a beach at dusk",
n=1,
size="1024x1024",
quality="low",
)
# You get either url or b64_json (depends on the model)
item = result.data[0]
if item.b64_json:
with open("image.png", "wb") as f:
f.write(base64.b64decode(item.b64_json))
else:
# Save an image returned as a url as soon as you receive it
urllib.request.urlretrieve(item.url, "image.png")
print("Saved image.png")Save it as image.py and run python image.py.
- Returning an image took 6 to 56 seconds at 1024x1024 in testing (it depends on the model). Set the request timeout to more than 60 seconds
- How the image comes back depends on the model.
gpt-image-2-*returnedb64_json(orurlwhen"response_format": "url"is set);seedream-5.0-flash,kling-image-o3,wan-image-2.7andseedream-5.5-editreturnedurl. The other models return eitherurlorb64_json. Handle both, as in the Python example above - Save an image returned as a
urlas soon as you receive it. How long it stays available has not been verified
Editing
This edits an image with seedream-5.5-edit, attaching one input image (PNG). Send it in form format (JSON does not work).
curl "https://token.echohub.io/v1/images/edits" \
-H "Authorization: Bearer $ECHO_API_KEY" \
-F "model=seedream-5.5-edit" \
-F "prompt=Change the background to a beach at dusk" \
-F "n=1" \
-F "size=1024x1024" \
-F "image=@input.png"The returned JSON has the same shape as for generation (url or b64_json in data[0]). qwen-image-3.0-pro-edit is called the same way. Only one input image is accepted.
Nano Banana
Nano Banana is called with POST /v1/chat/completions, like a text model. The image comes back inside the answer text as a Markdown image (; JPEG in testing).
import base64
import os
import re
from openai import OpenAI
client = OpenAI(
base_url="https://token.echohub.io/v1",
api_key=os.environ["ECHO_API_KEY"],
)
res = client.chat.completions.create(
model="gemini-nano-banana-2.1",
messages=[{"role": "user", "content": "Create an image of a Shiba Inu walking on a beach at dusk"}],
)
text = res.choices[0].message.content
# Take the image out of the answer and save it
m = re.search(r"!\[[^\]]*\]\(data:image/(\w+);base64,([A-Za-z0-9+/=]+)\)", text)
if m:
with open(f"image.{m.group(1)}", "wb") as f:
f.write(base64.b64decode(m.group(2)))
print(f"Saved image.{m.group(1)}")
else:
print(text)2K, 4K and aspect ratio (OpenAI format)
Without a size or aspect ratio the output is 1K (for example 1408×768 and 1,120 tokens in our test). To set 2K, 4K or an aspect ratio while keeping the POST /v1/chat/completions call above, add extra_body at the top level of the request and put image_size and aspect_ratio in its google.image_config. The usable values and the output tokens are the same as in the table in the next section (whether 512 works depends on the model).
- Write the keys with underscores:
image_config,image_size,aspect_ratio. Camel case such asimageConfigreturns an HTTP 500 error image_configorgoogle.image_configwritten at the top level withoutextra_bodyis ignored, and the output stays at 1K- With the OpenAI SDK, nest it twice:
extra_body={"extra_body": {...}}. The SDK merges the contents of theextra_bodyargument into the top level of the request - The image comes back and is extracted the same way as in the example above, and
clientis created the same way
curl -o response.json https://token.echohub.io/v1/chat/completions \
-H "Authorization: Bearer $ECHO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-nano-banana-2.1",
"messages": [{"role": "user", "content": "Create an image of a Shiba Inu walking on a beach at dusk"}],
"extra_body": {"google": {"image_config": {"image_size": "2K", "aspect_ratio": "16:9"}}}
}'res = client.chat.completions.create(
model="gemini-nano-banana-2.1",
messages=[{"role": "user", "content": "Create an image of a Shiba Inu walking on a beach at dusk"}],
# The OpenAI SDK merges the contents of extra_body into the top level of the request, so nest it twice
extra_body={"extra_body": {"google": {"image_config": {"image_size": "2K", "aspect_ratio": "16:9"}}}},
)2K, 4K and aspect ratio (Gemini format)
The Gemini format can also set the image size and aspect ratio. This is the format Google's official SDK uses.
- The address is
POST https://token.echohub.io/v1beta/models/{model ID}:generateContent(/v1/models/{model ID}:generateContentworks too) - Send the key in the
x-goog-api-key: keyheader - Write the size and ratio in
generationConfig.imageConfigof the request, asimageSize(1K,2K,4K) andaspectRatio - The image is in
inlineDataofcandidates[0].content.parts[]in the answer.datais Base64, andmimeTypewasimage/jpegin testing
curl -o response.json "https://token.echohub.io/v1beta/models/gemini-nano-banana-2.1:generateContent" \
-H "x-goog-api-key: $ECHO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{"role": "user", "parts": [{"text": "Create an image of a Shiba Inu walking on a beach at dusk"}]}],
"generationConfig": {
"responseModalities": ["IMAGE"],
"imageConfig": {"imageSize": "2K", "aspectRatio": "16:9"}
}
}'
# Take the image out of the answer and save it (if the request failed, response.json holds the error)
python3 -c "import json, base64; r = json.load(open('response.json')); ps = (r.get('candidates') or [{}])[0].get('content', {}).get('parts', []); d = [p['inlineData']['data'] for p in ps if 'inlineData' in p]; open('image.jpeg', 'wb').write(base64.b64decode(d[0])) if d else print(r)"Google's official Python SDK (google-genai) works once base_url points to Echo. The SDK adds /v1beta itself, so set base_url to https://token.echohub.io with no path.
# pip install google-genai
import os
from google import genai
from google.genai import types
client = genai.Client(
api_key=os.environ["ECHO_API_KEY"],
http_options=types.HttpOptions(base_url="https://token.echohub.io"),
)
res = client.models.generate_content(
model="gemini-nano-banana-2.1",
contents="Create an image of a Shiba Inu walking on a beach at dusk",
config=types.GenerateContentConfig(
response_modalities=["IMAGE"],
image_config=types.ImageConfig(image_size="2K", aspect_ratio="16:9"),
),
)
images = [p.inline_data.data for p in res.candidates[0].content.parts if p.inline_data]
if images:
with open("image.jpeg", "wb") as f:
f.write(images[0])
print("Saved image.jpeg")
else:
print("No image came back")Output per image size (aspect ratio 1:1):
| Model | 512 | 1K | 2K | 4K |
|---|---|---|---|---|
gemini-nano-banana-2. | Not supported (HTTP 400, not charged) | 1024×1024, 1,120 tokens | 2048×2048, 1,680 tokens | 4096×4096, 3,780 tokens |
gemini-3. | 512×512, 747 tokens | 1024×1024, 1,120 tokens | 2048×2048, 1,680 tokens | 4096×4096, 2,520 tokens |
gemini-3-pro-image | Not supported (no image is returned, finishReason is SAFETY; only the input tokens are charged) | 1024×1024, 1,120 tokens | 2048×2048, 1,120 tokens | 4096×4096, 2,000 tokens |
The charge is based on output tokens. On gemini-nano-banana-2.1 and gemini-3.1-flash-image, 2K is about 1.5 times the tokens of 1K. The price per token is on each model's page.
These aspectRatio values were tested on gemini-nano-banana-2.1:
aspectRatio | Output |
|---|---|
1:1 | 1K is 1024×1024 |
16:9 | 2K is 2752×1536, 4K is 5504×3072 |
9:16 | 1K is 768×1376 |
21:9 | 1K is 1584×672 |
1:4 | 1K is 512×2064 |
8:1 | 1K is 2928×352 |
To edit an image, put one image in parts, followed by the text instruction. imageSize works here too. The one input image counted as 1,120 input tokens in testing (gemini-nano-banana-2.1).
{
"contents": [{"role": "user", "parts": [
{"inlineData": {"mimeType": "image/jpeg", "data": "<Base64>"}},
{"text": "Change the background to a sunset"}
]}],
"generationConfig": {"responseModalities": ["IMAGE"], "imageConfig": {"imageSize": "2K"}}
}GPT Image 2
The provider's price for GPT Image 2 differs greatly by quality (low, medium, high), so Echo uses a separate model name for each quality.
| Model ID | Value to write in quality |
|---|---|
gpt-image-2-low | low |
gpt-image-2-medium | medium |
gpt-image-2-high | high |
- OpenAI's own way of writing it (
gpt-image-2asmodel, withqualitychoosing the quality) does not work. The modelgpt-image-2does not exist, so the request returns HTTP 503 - Write the model name that includes the quality, and write the same quality in
quality. If you leave outquality, or write a quality that differs from the model name, the request returns HTTP 400 sizemust be1024x1024,1536x1024or1024x1536.nmust be 1- By default the image comes back as
b64_json. Set"response_format": "url"to get the image URL (url) instead. Save an image returned as a URL as soon as you receive it stream,partial_imagesandinput_fidelitycannot be used- Only
POST /v1/images/generationsworks (images cannot be edited)
{
"model": "gpt-image-2-low",
"prompt": "A Shiba Inu walking on a beach at dusk",
"n": 1,
"size": "1024x1024",
"quality": "low"
}Requests that are rejected
The following requests are refused with an HTTP error. message is the English text exactly as returned.
| Request | Model | Write this instead | HTTP status and message |
|---|---|---|---|
model is gpt-image-2 | GPT Image 2 | One of gpt-image-2-low, gpt-image-2-medium, gpt-image-2-high | 503, The model gpt-image-2 does not exist or is temporarily unavailable |
quality is left out | gpt-image-2-low | Write low in quality | 400, quality must be "low" for this model (each quality level is a separate model) |
A quality that differs from the model name ("quality": "high" with gpt-image-2-low) | gpt-image-2-low | The same quality as the model name | 400, same as above |
size other than the three allowed (such as 2048x2048) | gpt-image-2-low | 1024x1024, 1536x1024 or 1024x1536 | 400, size must be one of 1024x1024, 1536x1024, 1024x1536 for this model |
size is written | qwen-image-3. | Leave out size | 400, size is not supported for this model; it always outputs 1K images and the aspect ratio cannot be chosen |
- For
gpt-image-2-mediumandgpt-image-2-high, the"low"inmessageis that model's own quality - Errors returned for a wrong GPT Image 2 request are not charged
Errors
The error body has the same format as the other OpenAI-format endpoints (Errors and fixes). The errors checked for image generation are as follows.
| Situation | HTTP status | What to do |
|---|---|---|
| A value cannot be used with that model (the table above) | 400 | Change the request as message says |
The model ID is wrong (such as writing gpt-image-2 for GPT Image 2) | 503 | Check the model ID and use the name that includes the quality |
Notes on Nano Banana
- The image is returned in the answer text as a Markdown image with a Base64 string. One image is a string of about 1 MB or more (about 1 to 1.4 MB in testing)
- In a multi-turn conversation, if you put the previous answer into
messagesas it is, all of that image string is sent as input and charged as input tokens. Remove the image part before the next request
import re
def strip_images(text: str) -> str:
# Replace Base64 images that start with data: with a short marker
return re.sub(r"!\[[^\]]*\]\(data:image/[^)]*\)", "[image]", text)
messages.append({"role": "assistant", "content": strip_images(text)})- Charges are calculated on output tokens. In testing, one image used 1,120 output tokens on
gemini-nano-banana-2.1andgemini-3.1-flash-image, and 1,428 ongemini-3-pro-image(including some text besides the image). The price per token is on each model's page