Public
Proxy for Google Gemini image generation API
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.

gemini-image-proxy

An HTTP proxy for the Google Gemini image-generation API. It accepts a simple JSON request, forwards it to Gemini, and returns the first generated image as base64.

Endpoint

POST / with a JSON body:

FieldTypeRequiredDescription
promptstringyesThe text prompt.
modestringyes"generate" or "edit".
imageDatastringyes for editBase64-encoded source image (no data URI).
imageMimestringoptionalMIME type of the source image (image/png).
  • generate — sends only the text prompt.
  • edit — sends the image data first, then the text prompt.

generationConfig.responseModalities is set to ["TEXT", "IMAGE"].

Success response

{ "imageData": "<base64>", "mimeType": "image/png" }

Other behaviors

  • GET / returns a JSON status message.
  • OPTIONS / handles the CORS preflight.
  • Every response returns HTTP 200 with permissive CORS headers — errors are reported in the JSON body ({ "error": "..." }), never via the status code.

Configuration

The API key is read from an environment variable (never hardcoded):

šŸ‘‰ Add GEMINI_API_KEY here: https://www.val.town/x/maoyvi/gemini-image-proxy/environment-variables?key=GEMINI_API_KEY

Model note

The originally requested model gemini-2.0-flash-preview-image-generation is no longer served at generateContent (returns 404). This proxy uses gemini-2.5-flash-image, the current model supporting image output via generateContent. Image generation requires a Gemini API key with billing enabled — free-tier keys return 429 RESOURCE_EXHAUSTED.

Example

curl -X POST https://maoyvi--36ff510265c311f1ba5c1607ee4eb77e.web.val.run \ -H "Content-Type: application/json" \ -d '{"prompt":"a tiny red cube on a white background","mode":"generate"}'