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
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.
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.
See it running
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.
How it fits together
Your own image endpoint on Cloudflare
- Your appScript, app or automationPOST /Bearer key + promptprompt + key
- CF Workerworker.js on your Cloudflare account401, 405, 400 checksAI bindingenv.AI.run
- Workers AI SDXLStable Diffusion XL base 1.0Prompt only
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:
- In the Cloudflare dashboard, create a Worker and paste
worker.jsinto it. - Add
API_KEYwith a long random value, ideally as an encrypted secret. - Add a Workers AI binding named
AI. - Deploy, then call your Worker's URL with
curlorfetch, 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.
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.
Comments
Loading comments...