🚫 No need to pay for any image AI. Use this to get ~100,000 free AI image generations per day powered by Cloudflare Workers AI — no credit card, no Midjourney subscription, no GPU, no server needed.
Cloudflare gives you free AI power through their Workers platform.
This project turns that free AI power into a simple image generation API — you send it a text description ("a cat sitting on the moon"), and it sends back an AI-generated image. Just like Midjourney or DALL-E, but 100% free.
You can use this with n8n, Make.com, your own website, Python scripts — anything.
| Feature | Details |
|---|---|
| 🆓 100% Free | Cloudflare free tier — no credit card needed |
| 🔐 Auth Protected | Your own secret API key — only you can use it |
| 🌐 CORS Enabled | Works from any website, n8n, Make.com, Postman |
| 🎨 Multiple Models | 4 different AI image models to choose from |
| 📦 OpenAI Format | Same response format as OpenAI — easy to integrate anywhere |
| 🖼️ Base64 Output | Image comes back as base64 — works everywhere, no storage needed |
@cf/black-forest-labs/flux-1-schnell ← Recommended (Best + Fastest)
@cf/bytedance/stable-diffusion-xl-lightning ← Very fast, realistic
@cf/stabilityai/stable-diffusion-xl-base-1.0 ← High quality
@cf/lykon/dreamshaper-8-lcm ← Artistic / stylized
📌 You don't need to know coding. Just follow these steps one by one.
Go to 👉 https://dash.cloudflare.com
If you don't have an account, click Sign Up — it's free, no credit card needed.
After logging in, look at the left sidebar and click on "Workers & Pages".
Click the "Create" button (top right area).
You will see options. Click "Start with Hello World!".
Fill in:
- Worker name → Type any name you like. Example:
my-image-ai
Click "Deploy".
✅ You just created your worker! Now let's set it up properly.
After deploying, you'll land on your worker's overview page.
Look for a section called "Bindings" and click on it.
A panel will open. Look for "Workers AI" and click it.
A form will appear. In the "Variable name" field, type exactly:
AI
⚠️ Very important: It must beAIin ALL CAPITAL LETTERS. This is how the code talks to the AI.
Click "Save" or "Add binding".
Now click on the "Settings" tab of your Worker.
Scroll down to find "Variables and Secrets".
Click "Add". A small form appears. Fill it like this:
| Field | What to type |
|---|---|
| Type | Select Secret |
| Variable name | API_KEY |
| Value | Make up your own password. Example: my-secret-key-2024 |
🔐 Think of API_KEY like a password for your worker. Anyone who has this key can use your worker to generate images. Keep it private!
⚠️ Important: Variable name MUST be exactlyAPI_KEY(capital letters, underscore in middle).
Click "Deploy" to save.
Click "Edit Code" (button at the top right of the worker page).
A code editor will open. Do this:
- Click anywhere inside the code box
- Press
Ctrl+A(Windows) orCmd+A(Mac) to select ALL existing code - Press
DeleteorBackspaceto delete it - Copy the entire code from
worker.jsin this repo - Paste it into the editor
Click "Deploy" (top right button).
At the top of the worker page, you'll see your URL. It looks like this:
https://my-image-ai.YOUR-SUBDOMAIN.workers.dev
That's your free image generation API! You're done with setup.
📌 Think of the API like a vending machine:
- You put in a prompt (your image description) + your API key
- It gives you back a generated image (as base64 data)
Open this in your browser (no login needed):
https://my-image-ai.your-subdomain.workers.dev/health
You should see:
{
"status": "ok",
"models": ["@cf/black-forest-labs/flux-1-schnell", "..."]
}If you see this, your worker is alive and working! ✅
Send a POST request to your worker:
curl -X POST https://my-image-ai.your-subdomain.workers.dev/v1/images/generations \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"prompt": "a cat sitting on the moon, photorealistic, 4K",
"model": "@cf/black-forest-labs/flux-1-schnell",
"n": 1
}'{
"created": 1700000000,
"data": [
{
"b64_json": "iVBORw0KGgoAAAANSUhEUgAA...",
"revised_prompt": "a cat sitting on the moon, photorealistic, 4K"
}
],
"model": "@cf/black-forest-labs/flux-1-schnell"
}📌 What is
b64_json? It's your image — but converted into text (called Base64). To actually see the image, you need to decode it. Every tool (n8n, Make.com, Python, JavaScript) knows how to decode it. Examples are below!
In your n8n workflow, click the + button and search for "HTTP Request". Add it.
Inside the HTTP Request node, set:
| Setting | Value |
|---|---|
| Method | POST |
| URL | https://my-image-ai.your-subdomain.workers.dev/v1/images/generations |
Click "Add Header" and fill:
| Header Name | Header Value |
|---|---|
Authorization |
Bearer YOUR_API_KEY |
Content-Type |
application/json |
📌 What is "Bearer"? It's just a word that tells the server "here comes my password". You always write
Bearerthen your actual key. Example:Bearer my-secret-key-2024
Set Body type to JSON and paste:
{
"prompt": "{{ $json.prompt }}",
"model": "@cf/black-forest-labs/flux-1-schnell",
"n": 1
}
{{ $json.prompt }}means "take the prompt from the previous n8n step". You can also hardcode text like"a sunset over mountains".
The image is inside: {{ $json.data[0].b64_json }}
To convert base64 to an actual image file, add a "Move Binary Data" node after this:
- From → JSON
- Property name →
data[0].b64_json - MIME type →
image/png
Now you can send this image to Google Drive, email, Telegram, Discord — anywhere!
In your Make.com scenario, click + and search for "HTTP". Add "Make a request".
| Field | Value |
|---|---|
| URL | https://my-image-ai.your-subdomain.workers.dev/v1/images/generations |
| Method | POST |
Click "Add item" under Headers:
| Header Name | Value |
|---|---|
Authorization |
Bearer YOUR_API_KEY |
Content-Type |
application/json |
Set Body type to Raw → format: JSON, then paste:
{
"prompt": "{{1.prompt}}",
"model": "@cf/black-forest-labs/flux-1-schnell"
}The image is at: data > 0 > b64_json
Add a Tools → Base64 Decode module to convert it to binary. Then you can upload to Google Drive, send via Gmail, post to Slack, etc.
const response = await fetch('https://my-image-ai.your-subdomain.workers.dev/v1/images/generations', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY',
},
body: JSON.stringify({
prompt: 'a dragon flying over mountains at sunset',
model: '@cf/black-forest-labs/flux-1-schnell',
}),
});
const data = await response.json();
const base64 = data.data[0].b64_json;
// Show image in browser
document.querySelector('img').src = `data:image/png;base64,${base64}`;import requests
import base64
response = requests.post(
'https://my-image-ai.your-subdomain.workers.dev/v1/images/generations',
headers={
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
json={
'prompt': 'a dragon flying over mountains at sunset',
'model': '@cf/black-forest-labs/flux-1-schnell',
}
)
b64 = response.json()['data'][0]['b64_json']
# Save to file
with open('my_image.png', 'wb') as f:
f.write(base64.b64decode(b64))
print("Image saved!")When sending a request, you can include these options:
| Parameter | Required? | Default | What It Does |
|---|---|---|---|
prompt |
✅ Yes | — | Describe what image you want |
model |
No | flux-1-schnell |
Which AI model to use |
n |
No | 1 |
How many images to generate (max 4) |
num_inference_steps |
No | 4 |
Higher = better quality but slower (max 20) |
guidance_scale |
No | 7.5 |
How closely AI follows your prompt (1–20) |
negative_prompt |
No | — | What you DON'T want in the image |
- Be descriptive:
"a futuristic Tokyo street at night, neon lights, rain, cinematic" - Mention style: add
photorealistic,anime style,oil painting,3D render - Mention quality: add
4K,ultra detailed,high resolution - Use negative prompt to avoid:
"blurry, low quality, watermark, text"
Open test-ui/index.html directly in your browser — no installation needed.
Enter your Worker URL and API key, pick a model, type a prompt, and click Generate. You can see the image, download it, copy the base64, and view the raw API response.
cloudflare-image-worker/
├── worker.js ← The main code — paste this into Cloudflare
├── wrangler.toml ← Config file (for developers using CLI)
├── package.json ← Package info (for developers using CLI)
├── test-ui/
│ └── index.html ← Test the API in your browser (no install needed)
└── README.md ← This guide
Q: Is this really free? No hidden charges? A: Yes! Cloudflare's free tier gives 100,000 Worker requests per day. Workers AI is currently free in beta. Just make sure you're on the free plan and you won't be charged.
Q: Do I need coding knowledge to set this up? A: No! Just follow the 6 steps above. You're only copy-pasting code, not writing any.
Q: My API key — where do I use it?
A: Whenever you call your worker URL, you add this header:
Authorization: Bearer YOUR_API_KEY
Replace YOUR_API_KEY with the password you set in Step 4. In n8n, Make.com — same thing, you add it as a header.
Q: What if I lose my API key? A: Go to Cloudflare → Your Worker → Settings → Variables and Secrets → click edit, set a new value, deploy again.
Q: The image is b64_json — how do I see it?
A: Copy the b64_json value, go to base64.guru/converter/decode/image, paste it, and click Decode — you'll see your image!
Q: Can I use this with ChatGPT plugins or LangChain?
A: Yes! Set the base URL to https://your-worker.workers.dev/v1 and your API key. It speaks OpenAI format.
Q: Difference between this and the text generation worker? A: cloudflare-worker-template is for text/chat (like ChatGPT). This repo is for image generation (like DALL-E / Midjourney).
- Store your
API_KEYas a Secret in Cloudflare — never put it directly in the code - Don't share your API key publicly (don't paste it in GitHub, Discord, etc.)
- Anyone with your key can use your free quota — treat it like a password
Found a bug? Want to add a feature? Pull requests are welcome!
- Fork this repo
- Create a branch:
git checkout -b my-new-feature - Make your changes and test them
- Open a Pull Request with a clear description
Ideas we'd love:
- Cloudflare R2 storage (save images and return a URL instead of base64)
- Rate limiting per API key
- More model support as Cloudflare adds them
- Image-to-image support
Usman Ali — @dotusmanali
Give it a star ⭐ on GitHub and share it with your friends! It really helps 🙏
- 📝 cloudflare-worker-template — Same thing but for text / chat generation
- 📖 Cloudflare Workers AI Docs
- 🤖 All Available Models