Skip to main content
← Back to Projects
AI Automation Self-host Cloudflare Worker

Free AI Image Generation API

Your own text-to-image endpoint on Cloudflare Workers

A 51-line Cloudflare Worker you deploy on your own account: send a prompt with your key, get a Stable Diffusion XL image back. Self-host only.

Solo build

Cloudflare WorkersWorkers AIStable DiffusionImage GenerationAPI
01 / Problem

The problem

I wanted images from scripts, apps and automations without running a GPU server or signing up for a per-image API. Cloudflare already runs image models next to its Workers, so the missing piece was a small, private endpoint in front of them.

02 / Solution

What I built

One Cloudflare Worker, 51 lines of JavaScript, that you deploy on your own Cloudflare account. You send it a prompt with your key and it answers with an image. There is no shared public endpoint: this page shows you how to run your own copy, and it runs inside your account's own Workers AI allowance.

03 / Demo

See it running

Demo coming

A recorded walkthrough of Free AI Image Generation API is on the way. Until then, the write-up and the diagram below show how it works.

04 / Architecture

How it fits together

Free AI Image Generation API

Your own image endpoint on Cloudflare

  1. Your appScript, app or automationPOST /Bearer key + prompt
    prompt + key
  2. CF Workerworker.js on your Cloudflare account401, 405, 400 checksAI binding
    env.AI.run
  3. Workers AI SDXLStable Diffusion XL base 1.0Prompt only
The image bytes go straight back to your app; nothing is stored and there is no shared public endpoint.
05 / Write-up

How it works

Every request passes three checks before any model runs. The Authorization header must equal Bearer <API_KEY>, compared with a variable on your Worker; anything else gets 401. Only POST to / is allowed (405 otherwise), and the JSON body needs a prompt (400 if it is missing). Then the Worker calls the model through the AI binding and returns the result as the response body:

            const result = await env.AI.run(
                "@cf/stabilityai/stable-diffusion-xl-base-1.0",
                { prompt }
            );

            return new Response(result, {
                headers: { "Content-Type": "image/jpeg" },
            });

Full file: worker.js. Only the prompt is passed; there are no size, seed or step options. Nothing is stored, and there is no rate limiting or logging in the Worker, so treat the key as the only gate.

To deploy your own:

  1. In the Cloudflare dashboard, create a Worker and paste worker.js into it.
  2. Add API_KEY with a long random value, ideally as an encrypted secret.
  3. Add a Workers AI binding named AI.
  4. Deploy, then call your Worker's URL with curl or fetch, the bearer header and {"prompt": "..."}.

Set the key before you share the URL with anything. If API_KEY is unset, the Worker compares the header with the literal string Bearer undefined, so a request sending exactly that would get through.

Status and links

Self-host only: there is no public endpoint to call, and I don't quote a free-tier number because Cloudflare's allowances change; check your own account's Workers AI limits. The code is on GitHub (link above). It has no tests or CI yet, and the other model IDs listed in a code comment are not drop-in swaps: most return a different format or need an input image.

Work with me

Need something like this built?

I build AI automations, agents and Salesforce solutions for teams. Tell me what you want to automate and I’ll tell you honestly whether it’s a fit.

Share
Discussion

Comments

Loading comments...

Add a comment

Comments are reviewed before they appear.