{"slug": "a-free-ai-chat-translation-api-on-cloudflare-workers-ai-no-credit-card-5-minutes", "title": "A free AI chat + translation API on Cloudflare Workers AI (no credit card, ~5 minutes)", "summary": "A developer published two MIT-licensed Cloudflare Workers templates that expose free LLM endpoints on Cloudflare Workers AI without a paid API key: a chat API using Llama 3.1 8B with conversation history, configurable system prompt, CORS and optional bearer-token auth, and a translation endpoint using Meta's m2m100-1.2b covering 100 languages callable via GET or POST. Both rely on the Workers AI `[ai]` binding and deploy with `npx wrangler deploy`, drawing on the free plan's daily neuron allocation for prototyping.", "body_md": "If you're building a side project and just want *an* LLM endpoint without wiring up a paid API key, Cloudflare Workers AI is worth a look. The free plan includes a daily allocation of \"neurons\" (Cloudflare's usage unit) for open models like Llama 3.1 8B and m2m100. That's plenty for prototypes, demos, and low-traffic tools. Limits are set by Cloudflare and can change, so check the current pricing page.\n\nI packaged two small, tested Workers as an MIT-licensed free download: **[cf-ai-starter-lite.zip](https://cf-ai-starter-shop.pages.dev/cf-ai-starter-lite.zip)**. The full code is below too, so you can copy it straight from this post.\n\n`01-chat-api`: a chat endpoint with conversation history, a configurable system prompt, CORS, and optional bearer-token auth`04-translate`: a translation endpoint for 100 languages (m2m100), callable with GET or POST\n`01-chat-api/wrangler.toml`:\n\n```\nname = \"cf-chat-api\"\nmain = \"worker.js\"\ncompatibility_date = \"2026-10-01\"\n\n[ai]\nbinding = \"AI\"\n```\n\nThe `[ai]` binding is the only thing that makes Workers AI available as `env.AI`.\n\n`01-chat-api/worker.js`:\n\n```\n// Free LLM chat API on Cloudflare Workers AI\n// POST /chat {\"message\":\"hi\",\"history\":[{\"role\":\"user\",\"content\":\"...\"}],\"system\":\"optional\"}\nconst MODEL = \"@cf/meta/llama-3.1-8b-instruct\";\nconst cors = { \"Access-Control-Allow-Origin\": \"*\", \"Access-Control-Allow-Headers\": \"Content-Type, Authorization\", \"Access-Control-Allow-Methods\": \"POST, OPTIONS\" };\nconst json = (d, s = 200) => new Response(JSON.stringify(d), { status: s, headers: { \"Content-Type\": \"application/json\", ...cors } });\nexport default {\n  async fetch(req, env) {\n    if (req.method === \"OPTIONS\") return new Response(null, { headers: cors });\n    if (env.API_KEY && req.headers.get(\"Authorization\") !== `Bearer ${env.API_KEY}`) return json({ error: \"unauthorized\" }, 401);\n    if (req.method !== \"POST\") return json({ error: \"POST /chat\" }, 405);\n    let body; try { body = await req.json(); } catch { return json({ error: \"invalid JSON\" }, 400); }\n    if (!body.message || typeof body.message !== \"string\") return json({ error: \"message (string) required\" }, 400);\n    const history = Array.isArray(body.history) ? body.history.slice(-20) : [];\n    const messages = [{ role: \"system\", content: body.system || \"You are a helpful, concise assistant.\" }, ...history, { role: \"user\", content: body.message }];\n    const out = await env.AI.run(env.MODEL || MODEL, { messages, max_tokens: body.max_tokens || 512 });\n    return json({ reply: out.response, model: env.MODEL || MODEL });\n  },\n};\n```\n\n`04-translate/wrangler.toml`:\n\n```\nname = \"cf-translate\"\nmain = \"worker.js\"\ncompatibility_date = \"2026-10-01\"\n\n[ai]\nbinding = \"AI\"\n```\n\n`04-translate/worker.js`:\n\n```\n// Translation endpoint using Meta m2m100 (100 languages)\n// POST {\"text\":\"Hello\",\"target_lang\":\"french\",\"source_lang\":\"english\"}  or GET /?text=Hello&to=spanish\nconst MODEL = \"@cf/meta/m2m100-1.2b\";\nconst json = (d, s = 200) => new Response(JSON.stringify(d), { status: s, headers: { \"Content-Type\": \"application/json\", \"Access-Control-Allow-Origin\": \"*\" } });\nexport default {\n  async fetch(req, env) {\n    let text, to, from;\n    if (req.method === \"GET\") { const u = new URL(req.url); text = u.searchParams.get(\"text\"); to = u.searchParams.get(\"to\"); from = u.searchParams.get(\"from\"); }\n    else { try { ({ text, target_lang: to, source_lang: from } = await req.json()); } catch { return json({ error: \"invalid JSON\" }, 400); } }\n    if (!text || !to) return json({ error: \"text and target_lang required\" }, 400);\n    const o = await env.AI.run(MODEL, { text, target_lang: to, source_lang: from || \"english\" });\n    return json({ translated_text: o.translated_text, target_lang: to });\n  },\n};\n```\n\nPut each pair of files in its own folder (or unzip the free download), then:\n\n```\ncd 01-chat-api\nnpx wrangler login\nnpx wrangler deploy\n```\n\nRepeat in `04-translate`.\n\n```\ncurl -X POST https://<your-chat-worker>.workers.dev \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"message\":\"Explain edge functions in one sentence\"}'\n\ncurl \"https://<your-translate-worker>.workers.dev/?text=Hello%20world&to=french\"\n```\n\n`workers.dev` URL burns through your free allocation quickly if someone finds it. The chat Worker checks an optional `API_KEY` secret (`npx wrangler secret put API_KEY`), then expects `Authorization: Bearer <key>`.` test/run-tests.mjs`, which calls Workers AI over REST with your account ID and token. Mocks wouldn't have caught model-output quirks.\n\n```\nCF_ACCOUNT_ID=... CF_API_TOKEN=... node test/run-tests.mjs\n```\n\nThe free download is complete on its own. I also sell a fuller pack ($7) that adds an OpenAI-compatible `/v1/chat/completions` proxy with streaming (point the OpenAI SDK or LangChain at it), a batch summarizer, a JSON extractor with schema retry, semantic search with BGE embeddings, a prompt library, and a setup guide: [https://cf-ai-starter-shop.pages.dev](https://cf-ai-starter-shop.pages.dev)\n\n*Not affiliated with Cloudflare. Questions welcome in the comments.*\n\n*Written with AI assistance.*", "url": "https://wpnews.pro/news/a-free-ai-chat-translation-api-on-cloudflare-workers-ai-no-credit-card-5-minutes", "canonical_source": "https://dev.to/falingbot/a-free-ai-chat-translation-api-on-cloudflare-workers-ai-no-credit-card-5-minutes-5807", "published_at": "2026-10-26 22:58:54+00:00", "updated_at": "2026-10-26 23:13:03.457295+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools", "large-language-models", "ai-infrastructure", "natural-language-processing"], "entities": ["Cloudflare", "Cloudflare Workers AI", "Meta", "Llama 3.1 8B", "m2m100"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/a-free-ai-chat-translation-api-on-cloudflare-workers-ai-no-credit-card-5-minutes", "markdown": "https://wpnews.pro/news/a-free-ai-chat-translation-api-on-cloudflare-workers-ai-no-credit-card-5-minutes.md", "text": "https://wpnews.pro/news/a-free-ai-chat-translation-api-on-cloudflare-workers-ai-no-credit-card-5-minutes.txt", "jsonld": "https://wpnews.pro/news/a-free-ai-chat-translation-api-on-cloudflare-workers-ai-no-credit-card-5-minutes.jsonld"}}