This project lets you deploy your own AI image generation API using Cloudflare Workers AI.
Generate stunning images from text prompts without managing expensive GPU servers or traditional backend infrastructure.
✨ YOUR IDEA
│
▼
📝 TEXT PROMPT
│
▼
┌─────────────────┐
│ ⚡ API CALL │
└────────┬────────┘
│
▼
┌─────────────────┐
│ ☁️ CLOUDFLARE │
│ WORKER │
└────────┬────────┘
│
▼
┌─────────────────┐
│ 🤖 WORKERS AI │
│ AI MODEL │
└────────┬────────┘
│
▼
┌─────────────────┐
│ 🎨 AI IMAGE │
└─────────────────┘
Write a prompt → Send an API request → Get an AI-generated image.
| ⚡ | Feature | Description |
|---|---|---|
| 🆓 | 100,000 Calls/Day | Cloudflare Workers AI free-tier allowance, subject to current limits |
| ⚡ | Lightning Fast | Serverless image generation |
| 🎨 | AI Image Generation | Turn text prompts into images |
| 📏 | Custom Aspect Ratios | 1:1, 16:9, 9:16, 2:3, 3:2, 4:5, 5:4 |
| 🔒 | Secure API | Bearer-token authentication |
| 🤖 | Multiple Models | Use supported Cloudflare AI models |
| ☁️ | Serverless | No VPS or GPU server required |
| 🛠️ | Easy Deployment | Get started in minutes |
| 🌍 | Global Infrastructure | Powered by Cloudflare |
flowchart LR
A["📝 Prompt"] --> B["⚡ API"]
B --> C["☁️ Worker"]
C --> D["🔐 Auth"]
D --> E["🤖 Workers AI"]
E --> F["🎨 Generate"]
F --> G["📦 Image"]
style A stroke-width:2px
style B stroke-width:2px
style C stroke-width:2px
style D stroke-width:2px
style E stroke-width:2px
style F stroke-width:2px
style G stroke-width:2px
Create a simple text prompt describing the image you want.
A futuristic city floating above the clouds,
cinematic lighting, highly detailed
Send your prompt to your Worker API.
POST /
Authorization: Bearer YOUR_API_KEY
Your Cloudflare Worker sends the request to Workers AI.
Prompt
↓
Cloudflare Worker
↓
Workers AI
↓
AI Model
The API returns the generated image.
📝 Prompt
↓
⚡ API
↓
🤖 AI
↓
🎨 IMAGE
If you don't already have one, create an account on Cloudflare.
Go to the Cloudflare Workers Dashboard.
Choose:
Create application
↓
Create Worker
↓
Choose a name
↓
Deploy
Example:
free-image-generation-api
Open the Worker editor and replace the default Hello World code with the worker.js code from this repository.
Then click:
Save and Deploy
✅ Your Worker is now deployed.
Go to:
Worker
↓
Settings
↓
Variables
↓
Environment Variables
Add:
API_KEY=your-secret-api-keyReplace your-secret-api-key with a strong random secret.
🔒 Never expose your API key publicly or commit it to GitHub.
In your Cloudflare dashboard:
Workers & Pages
↓
AI
↓
Workers AI
Enable Workers AI for your account.
⚠️ Free-tier availability and limits may change. Check Cloudflare's current documentation before relying on specific quotas.
Go to:
Worker
↓
Settings
↓
Variables
↓
Service Bindings
Click Add binding.
Use:
Variable name: AI
Service: Workers AI
Then:
Save and Deploy
⚠️ Important: Without theAIbinding, your Worker cannot access Workers AI models.
Once deployed, your endpoint will look like:
https://<your-worker-name>.<your-subdomain>.workers.dev
https://farukhetro.farukhetro.workers.dev
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
prompt |
string |
✅ | — | Description of the image |
aspect_ratio |
string |
❌ | 1:1 |
Desired image aspect ratio |
1:1 → Square
16:9 → Landscape
9:16 → Portrait
2:3 → Portrait
3:2 → Landscape
4:5 → Portrait
5:4 → Landscape
curl -X POST https://farukhetro.farukhetro.workers.dev \
-H "Authorization: Bearer your-secret-api-key" \
-H "Content-Type: application/json" \
-d '{
"prompt": "A cute robot cooking breakfast",
"aspect_ratio": "16:9"
}' \
--output image.jpg 📝 Prompt
│
▼
⚡ API Call
│
▼
🤖 AI
│
▼
┌───────────┐
│ 🎨 IMAGE │
└───────────┘
const res = await fetch(
"https://farukhetro.farukhetro.workers.dev",
{
method: "POST",
headers: {
"Authorization": "Bearer your-secret-api-key",
"Content-Type": "application/json",
},
body: JSON.stringify({
prompt: "A futuristic city in the clouds",
aspect_ratio: "16:9"
}),
}
);
const blob = await res.blob();
const img = document.createElement("img");
img.src = URL.createObjectURL(blob);
img.style.height = "500px";
img.style.maxWidth = "100%";
document.body.appendChild(img);A cute futuristic robot cooking breakfast
inside a modern kitchen, cinematic lighting,
high detail, realistic photography
A futuristic cyberpunk city at night,
neon lights, flying cars, rainy streets,
cinematic atmosphere, ultra detailed
A peaceful mountain landscape during golden hour,
dramatic clouds, beautiful natural lighting,
cinematic photography
An astronaut exploring an alien planet,
massive mountains in the background,
cinematic lighting, highly detailed concept art
This API can power:
🎨 AI Art Platforms
🖼️ Image Generation Websites
📱 Mobile Applications
🤖 AI Assistants
💬 Discord / Telegram Bots
📰 Blog Image Generators
📢 Social Media Content
🛍️ E-Commerce Images
🎮 Game Assets
🧑💻 Developer Tools
Traditional AI image generation can require expensive infrastructure.
This project uses a serverless architecture instead.
TRADITIONAL APPROACH
─────────────────────
💻 VPS
↓
🎮 GPU
↓
💰 Infrastructure Costs
↓
🔧 Server Maintenance
THIS PROJECT
────────────
☁️ Cloudflare
↓
⚡ Worker
↓
🤖 Workers AI
↓
🎨 Generated Image
❌ VPS
❌ Dedicated GPU
❌ Traditional backend server
❌ Always-running process
❌ Complex infrastructure
✅ Cloudflare
✅ Worker
✅ AI Binding
✅ API Key
Your API key protects your image-generation endpoint.
const API_KEY = "my-secret-key";inside publicly accessible frontend code.
🌐 Frontend
│
▼
🖥️ Your Backend
│
▼
☁️ Cloudflare Worker
│
▼
🤖 Workers AI
Never commit secrets such as:
.env
.env.local
API_KEY
API_SECRET
ACCESS_TOKEN
to your repository.
You can modify the Worker for your own project.
| Component | Customize |
|---|---|
| 🤖 AI Model | Change the Workers AI model |
| 🎨 Prompts | Add prompt processing |
| 📏 Images | Modify aspect ratios |
| 🔐 Authentication | Add custom authentication |
| 🚦 Rate Limits | Add request limits |
| 🌐 CORS | Configure allowed origins |
| 📊 Analytics | Track API usage |
| 🖥️ Frontend | Build your own interface |
🌍 INTERNET
│
▼
┌─────────────────┐
│ Your Client │
│ Web / App / Bot │
└────────┬────────┘
│
│ HTTPS
▼
┌─────────────────┐
│ ☁️ Cloudflare │
│ Worker │
└────────┬────────┘
│
🔐 API KEY
│
▼
┌─────────────────┐
│ 🤖 Workers AI │
│ │
│ AI Model │
└────────┬────────┘
│
▼
┌─────────────────┐
│ 🎨 IMAGE │
│ GENERATED │
└─────────────────┘
Cloudflare Workers AI provides a free usage allowance subject to Cloudflare's current pricing, limits, and account terms.
The commonly referenced 100,000 AI requests/day allowance may change.
Check the latest information on Cloudflare Workers AI and Cloudflare pricing.
You can change the model inside worker.js to use other models supported by Cloudflare Workers AI.
Keep your API key private.
If your key is compromised:
🚨 Revoke
↓
🔄 Rotate
↓
🔐 Generate a new secret
Before using the API in production:
☐ Worker deployed
☐ Workers AI enabled
☐ AI binding configured
☐ API_KEY configured
☐ Authentication tested
☐ Image generation tested
☐ Aspect ratios tested
☐ Error handling tested
☐ API key protected
☐ Current usage limits reviewed
Contributions are welcome! 🎉
1. 🍴 Fork the repository
2. 🌿 Create a branch
3. 🛠️ Make your changes
4. 🧪 Test your changes
5. 💾 Commit your changes
6. 🚀 Push your branch
7. 🔀 Open a Pull Request
Have an idea?
Open an issue and let's build it together. 🚀
If this project helped you:
This project is licensed under the MIT License.
See LICENSE for the complete license.