This site is not affiliated with or endorsed by Cloudflare, Inc. It showcases deployable experiments across most Cloudflare products.
AI & Machine Learning

AI Image Generator

Text-to-image generation with Workers AI at the edge - prompt in, image out from a deployable Cloudflare Worker

Generate PNG images from text prompts using Workers AI and the @cf/black-forest-labs/flux-1-schnell model at the edge.

Features

  • Generate PNG images from text prompts at the edge
  • Workers AI image model (@cf/black-forest-labs/flux-1-schnell)
  • Returns binary image responses directly from the Worker
  • No external image API keys required

API Reference

GET /generate

Generates a PNG image from the provided text prompt.

Prop

Type

Example Request

curl "https://your-worker.workers.dev/generate?prompt=a%20sunset%20over%20snow-capped%20mountains" \
  --output image.png

Success Response

Returns a PNG image with the following header:

Content-Type: image/png

Error Response

{
  "error": "Missing or invalid query parameter: prompt",
  "code": "INVALID_PROMPT"
}

Error Codes

  • 400 - Missing or invalid prompt (INVALID_PROMPT)
  • 502 - Workers AI model run failed (AI_ERROR)

Use Cases

  • Prototype AI image generation without external API keys
  • Generate placeholder or marketing visuals from text at the edge
  • Learn Workers AI image models in a stateless Worker
  • Build creative tools that return binary image responses directly

Limitations

  • Workers AI image generation is subject to usage limits by plan
  • Fixed model and parameters; no style, size, or batch controls in this experiment
  • Returns a single PNG per request; no async jobs or storage
  • Public endpoint with no authentication or rate limiting

Use in your project

Copy these files into an existing Worker. Prefer Deployment to try the full experiment first. Source: apps/experiments/ai-image-generator.

DependenciesNoneBindingsWorkers AI (AI binding)PlatformWorkers AI
import type { Env } from "../types/env";import { AI_MODEL } from "../constants/defaults";type ImageResult = {  image?: string;};export async function generateImage(env: Env, prompt: string): Promise<Uint8Array> {  const out = (await env.AI.run(AI_MODEL, { prompt })) as ImageResult;  if (!out.image) {    throw new Error("Image model returned no image");  }  const binaryString = atob(out.image);  return Uint8Array.from(binaryString, (m) => m.codePointAt(0) ?? 0);}

Deployment

Click the deploy button

Deploy to Cloudflare Workers

Deploy

Enable the Workers AI binding (AI) in your Worker settings. The deploy button configures this automatically via wrangler.json. Requires a Cloudflare account with Workers AI enabled.

Test your deployment

curl "https://your-worker.workers.dev/generate?prompt=a%20sunset%20over%20snow-capped%20mountains" \
  --output image.png

Local Development

cd apps/experiments/ai-image-generator
npm install
npm run dev

Test locally:

curl "http://localhost:8787/generate?prompt=a%20cyberpunk%20cityscape" \
  --output image.png

Cloudflare Features Used

  • Workers - Edge compute runtime
  • Workers AI - @cf/black-forest-labs/flux-1-schnell image generation model

On this page