Cloudflare Worker: keep the image model id in KV and swap it live

A Worker that proxies POST /v1/images and reads the Sume model id from Workers KV, so replacing gpt-image-1 is a wrangler command, not a deploy.

5 min readSume
All posts

Store the image model id in a Workers KV key and read it per request. The Worker below forwards a prompt to POST https://api.sume.com/v1/images with the id from KV, then returns Sume's status and JSON untouched. Replacing a retired id is one wrangler kv key put, and the Worker script never changes.

Allow a short delay for a KV write to reach every location. Keep a default in code for the empty-key case so a missing key never sends model: undefined.

The Worker

The X-Image-Model response header tells the caller which id ran. Sume's 200 body has data as an image list; a 202 has the job envelope, and the Worker passes either through.

export default {
  async fetch(request, env) {
    const model =
      (await env.CONFIG.get("image_model")) ?? "openai/gpt-image-2.5-sunburst";
    const { prompt } = await request.json();
    const upstream = await fetch("https://api.sume.com/v1/images", {
      method: "POST",
      headers: {
        Authorization: `Bearer ${env.SUME_API_KEY}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ model, prompt }),
    });
    return new Response(upstream.body, {
      status: upstream.status,
      headers: { "Content-Type": "application/json", "X-Image-Model": model },
    });
  },
};

Swap the id

Bind the namespace as CONFIG in your Wrangler config and write the key from a terminal:

npx wrangler kv key put --binding=CONFIG image_model "openai/gpt-image-2.5-sunburst"

Which ids are valid

An id that Sume does not list returns 404 model_not_found, which the Worker passes through. For the 200 versus 202 handling in a caller, see the status-code post. The id mapping itself is in the model map.

Sume image ids for the KV value, read 2026-10-06
Use caseValue
ChatGPT Image 2.5 (Sunburst)openai/gpt-image-2.5-sunburst
ChatGPT Image 2.5 (Flare)openai/gpt-image-2.5
Gemini-family image modelgoogle/nano-banana-2

Sources

Related posts

More in Integrations

All Integrations posts

Written by Sume