This project lets you deploy your own free AI image generation API using Cloudflare Workers, with up to 100,000 API calls per day. Generate stunning images from text prompts using powerful models like Stable Diffusion XL! π¨
- π 100,000 free API calls per day (Cloudflare Workers AI free tier)
- β‘ Lightning-fast image generation from text prompts
- π οΈ Easy to deploy - no coding experience required
- π Secure with API key authentication
- π― Multiple AI models available
- π€ You deploy a Cloudflare Worker using the provided
worker.jsfile - π The Worker exposes a simple API endpoint for image generation
- π You authenticate using your own API key
- π€ The Worker uses Cloudflare's free AI models to generate images
- Sign up at Cloudflare if you don't have one
- Go to the Cloudflare Workers dashboard
- Click "Create application" π―
- Choose "Create Worker"
- Give it a name like
free-image-generation-apiπ - Click "Deploy" to create a Hello World worker π
- In the worker editor, replace the default Hello World code with the
worker.jscode from this repo π - Click "Save and Deploy" β
- In your worker dashboard, go to "Settings" > "Variables" βοΈ
- Under "Environment Variables", click "Add variable" β
- Name:
API_KEYπ·οΈ - Value:
your-secret-api-key(replace with a strong secret key) π - Click "Save and Deploy" πΎ
- In the Cloudflare dashboard, go to "Workers & Pages" > "AI" π§
- Enable Workers AI for your account (free tier is enough) π
- Go back to your worker's dashboard
- Click on "Settings" > "Variables" βοΈ
- Scroll down to "Service bindings" section
- Click "Add binding" β
- Variable name:
AIπ·οΈ - Service: Select "Workers AI" from dropdown π€
- Click "Save and Deploy" β
β οΈ Important: Without this AI binding, your worker won't be able to access Cloudflare's AI models!
- Your worker will be available at:
https://<your-worker-name>.<your-subdomain>.workers.devπ - You can find the exact URL in your worker's dashboard π
curl -X POST https://<your-worker-name>.<your-subdomain>.workers.dev \
-H "Authorization: Bearer your-secret-api-key" \
-H "Content-Type: application/json" \
-d '{"prompt": "A cute robot cooking breakfast"}' \
--output image.jpgconst res = await fetch("https://<your-worker-name>.<your-subdomain>.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" }),
});
const blob = await res.blob();
const img = document.createElement("img");
img.src = URL.createObjectURL(blob);
img.style.height = "500px";
document.body.appendChild(img);- π Free Tier: Cloudflare Workers AI free tier allows 100,000 AI requests per day. See Cloudflare pricing for details.
- π¨ Models: You can change the model in
worker.jsto use other available models (see comments in the file). - π Security: Keep your API key secret. Rotate it if needed.
MIT License β
β Star this repo if it helped you! β