Skip to main content
This guide uses Google’s official Python SDK. The only BananaHub-specific step is overriding base_url. See Gemini-compatible API for the concept.
You’ll need an active API key with permission for the model you call. See Authentication.

1. Install and configure

Point the client at BananaHub and pass your API key:
Any Gemini client works the same way — set the base URL to https://bananahub.io/api/gemini and send your key as x-goog-api-key.

2. Text-to-image

Request the IMAGE modality and read the base64 image from the first candidate.
The response is standard Gemini JSON. The image is base64 in candidates[0].content.parts[0].inlineData.data.

Resolution

For NanoBanana 2 and Pro, select the output size with imageSize1K, 2K, 4K, plus 512px on NanoBanana 2:
imageSize is validated by the model — supported values are 1K, 2K, 4K (plus 512px on NanoBanana 2); anything else returns 400 INVALID_ARGUMENT. NanoBanana (gemini-2.5-flash-image) outputs 1K. Sizes below 1K (e.g. 512px) are billed at the 1K rate.

3. Image-to-image (edit)

Pass one or more input images plus an instruction in contents.

4. Streaming

Use generate_content_stream to receive the response as it’s produced.
The raw HTTP endpoint is :streamGenerateContent?alt=sse, which returns Server-Sent Events (data: {json} frames) — exactly what the SDK consumes.

5. Count tokens

countTokens is proxied straight to Google, so you get a real token count for your request — useful for budgeting before you generate.
Preview model ids are supported here too — see Models.

6. Handle errors

BananaHub returns errors in the Gemini envelope, so the SDK raises errors.APIError with the canonical status:
See Errors for the full status list.