# AI Image Generator (/docs/experiments/ai-image-generator)



Generate PNG images from text prompts using [Workers AI](https://developers.cloudflare.com/workers-ai/) and the `@cf/black-forest-labs/flux-1-schnell` model at the edge.

## Features [#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 [#api-reference]

### GET /generate [#get-generate]

Generates a PNG image from the provided text prompt.

<TypeTable
  type="{
  prompt: {
    description: &#x22;Text prompt describing the image (max 1,000 characters)&#x22;,
    type: &#x22;string&#x22;,
    required: true,
  },
}"
/>

#### Example Request [#example-request]

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

#### Success Response [#success-response]

Returns a PNG image with the following header:

```http
Content-Type: image/png
```

#### Error Response [#error-response]

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

#### Error Codes [#error-codes]

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

## Use Cases [#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 [#limitations]

* Workers AI image generation is subject to [usage limits](https://developers.cloudflare.com/workers-ai/platform/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

## Deployment [#deployment]

<Steps>
  <Step>
    ### Click the deploy button [#click-the-deploy-button]

    [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/shrinathsnayak/cloudflare-experiments/tree/main/apps/experiments/ai-image-generator)
  </Step>

  <Step>
    ### Deploy [#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.
  </Step>

  <Step>
    ### Test your deployment [#test-your-deployment]

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

## Local Development [#local-development]

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

Test locally:

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

## Cloudflare Features Used [#cloudflare-features-used]

* **[Workers](https://developers.cloudflare.com/workers/)** - Edge compute runtime
* **[Workers AI](https://developers.cloudflare.com/workers-ai/)** - `@cf/black-forest-labs/flux-1-schnell` image generation model
