BLACK FRIDAYUntil Dec 8 (Tue) 23:59 JSTUntil Dec 8

DocsImage generation

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 callMethod and pathFormatModelsCharged per
GenerationPOST /v1/images/generationsJSONModels marked "Generation" in the table belowImage
EditingPOST /v1/images/editsForm format (multipart/form-data)The two models whose names end in -editImage
Nano BananaPOST /v1/chat/completionsJSON (same as text models)The three Nano Banana modelsToken

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 IDProviderHow to callFields 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)OpenAIGenerationAlways 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-3Black Forest LabsGenerationThe request that was tested is in "Examples"
ideogram-4.5, ideogram-v3IdeogramGenerationSame as above
kling-image-o3Kuaishou (Kling)GenerationSame as above
qwen-image-2.0Alibaba CloudGenerationSame as above
qwen-image-3.0-proAlibaba CloudGenerationDo 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.7-proAlibaba CloudGenerationThe request that was tested is in "Examples"
recraft-v4.1RecraftGenerationSame as above
seedream-4.5, seedream-5.0-flash, seedream-5.0-lite, seedream-5.5ByteDance (Volcano Engine)GenerationSame as above
seedream-5.5-edit, qwen-image-3.0-pro-editByteDance (Volcano Engine), Alibaba CloudEditingOnly one input image (image)
gemini-nano-banana-2.1 (Nano Banana 2.1), gemini-3.1-flash-image (Nano Banana 2), gemini-3-pro-image (Nano Banana Pro)GoogleNano BananaSee "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).

bash
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.

  • 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-* returned b64_json (or url when "response_format": "url" is set); seedream-5.0-flash, kling-image-o3, wan-image-2.7 and seedream-5.5-edit returned url. The other models return either url or b64_json. Handle both, as in the Python example above
  • Save an image returned as a url as 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).

bash
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 (![...](data:image/jpeg;base64,...); JPEG in testing).

Python
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 as imageConfig returns an HTTP 500 error
  • image_config or google.image_config written at the top level without extra_body is ignored, and the output stays at 1K
  • With the OpenAI SDK, nest it twice: extra_body={"extra_body": {...}}. The SDK merges the contents of the extra_body argument into the top level of the request
  • The image comes back and is extracted the same way as in the example above, and client is created the same way
bash
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"}}}
  }'

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}:generateContent works too)
  • Send the key in the x-goog-api-key: key header
  • Write the size and ratio in generationConfig.imageConfig of the request, as imageSize (1K, 2K, 4K) and aspectRatio
  • The image is in inlineData of candidates[0].content.parts[] in the answer. data is Base64, and mimeType was image/jpeg in testing
bash
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)"

Output per image size (aspect ratio 1:1):

Model5121K2K4K
gemini-nano-banana-2.1Not supported (HTTP 400, not charged)1024×1024, 1,120 tokens2048×2048, 1,680 tokens4096×4096, 3,780 tokens
gemini-3.1-flash-image512×512, 747 tokens1024×1024, 1,120 tokens2048×2048, 1,680 tokens4096×4096, 2,520 tokens
gemini-3-pro-imageNot supported (no image is returned, finishReason is SAFETY; only the input tokens are charged)1024×1024, 1,120 tokens2048×2048, 1,120 tokens4096×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:

aspectRatioOutput
1:11K is 1024×1024
16:92K is 2752×1536, 4K is 5504×3072
9:161K is 768×1376
21:91K is 1584×672
1:41K is 512×2064
8:11K 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).

JSON
{
  "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 IDValue to write in quality
gpt-image-2-lowlow
gpt-image-2-mediummedium
gpt-image-2-highhigh
  • OpenAI's own way of writing it (gpt-image-2 as model, with quality choosing the quality) does not work. The model gpt-image-2 does 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 out quality, or write a quality that differs from the model name, the request returns HTTP 400
  • size must be 1024x1024, 1536x1024 or 1024x1536. n must 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_images and input_fidelity cannot be used
  • Only POST /v1/images/generations works (images cannot be edited)
JSON
{
  "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.

RequestModelWrite this insteadHTTP status and message
model is gpt-image-2GPT Image 2One of gpt-image-2-low, gpt-image-2-medium, gpt-image-2-high503, The model gpt-image-2 does not exist or is temporarily unavailable
quality is left outgpt-image-2-lowWrite low in quality400, 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-lowThe same quality as the model name400, same as above
size other than the three allowed (such as 2048x2048)gpt-image-2-low1024x1024, 1536x1024 or 1024x1536400, size must be one of 1024x1024, 1536x1024, 1024x1536 for this model
size is writtenqwen-image-3.0-proLeave out size400, size is not supported for this model; it always outputs 1K images and the aspect ratio cannot be chosen
  • For gpt-image-2-medium and gpt-image-2-high, the "low" in message is 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.

SituationHTTP statusWhat to do
A value cannot be used with that model (the table above)400Change the request as message says
The model ID is wrong (such as writing gpt-image-2 for GPT Image 2)503Check 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 messages as it is, all of that image string is sent as input and charged as input tokens. Remove the image part before the next request
Python
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.1 and gemini-3.1-flash-image, and 1,428 on gemini-3-pro-image (including some text besides the image). The price per token is on each model's page