What Are Cloudflare Workers?
Cloudflare Workers run your JavaScript or TypeScript on Cloudflare's global network, in hundreds of cities, close to your users. There are no servers to manage, and unlike many serverless platforms, Workers start almost instantly because they run in lightweight V8 isolates rather than containers.
They're great for APIs, redirects, authentication checks, webhooks, image or HTML transformations, and small full-stack apps. This tutorial builds and deploys a working API in minutes.
๐ฏ Quick Answer (30-Second Read)
- Create:
npm create cloudflare@latest my-worker - Run locally:
npx wrangler dev - Deploy:
npx wrangler deployโ live atmy-worker.<your-subdomain>.workers.dev - Store data: KV (key-value), D1 (SQLite database), R2 (object storage), Durable Objects (stateful coordination)
- Secrets:
npx wrangler secret put API_KEY - Free plan: includes a daily request allowance (check Cloudflare's current limits) and limited CPU time per request
How Workers Run
Mumbai"] U2["๐ค User in London"] --> E2["Cloudflare edge
London"] E1 --> W1["Your Worker
(V8 isolate)"] E2 --> W2["Your Worker
(V8 isolate)"] W1 --> KV[("KV / D1 / R2")] W2 --> KV W1 -.->|"optional"| O["Your origin API"]
Each request runs your code in the nearest Cloudflare location. Isolates share one runtime process but are sandboxed from each other, which is why cold starts are tiny.
Step 1: Create a Project
npm create cloudflare@latest my-worker
# choose: "Hello World" Worker, TypeScript, yes to git
cd my-workerThis creates src/index.ts and a Wrangler config file (wrangler.jsonc or wrangler.toml).
Step 2: Write a Small API
// src/index.ts
export interface Env {
LINKS: KVNamespace;
API_KEY: string;
}
export default {
async fetch(request: Request, env: Env): Promise {
const url = new URL(request.url);
if (url.pathname === "/health") {
return Response.json({ ok: true, colo: request.cf?.colo });
}
// GET /go/:code โ redirect
if (url.pathname.startsWith("/go/")) {
const code = url.pathname.slice(4);
const target = await env.LINKS.get(code);
return target ? Response.redirect(target, 302) : new Response("Not found", { status: 404 });
}
// POST /links {code, url} (requires API key)
if (url.pathname === "/links" && request.method === "POST") {
if (request.headers.get("Authorization") !== `Bearer ${env.API_KEY}`) {
return new Response("Unauthorized", { status: 401 });
}
const { code, url: target } = await request.json<{ code: string; url: string }>();
await env.LINKS.put(code, target);
return Response.json({ created: code }, { status: 201 });
}
return new Response("Not found", { status: 404 });
},
}; That's a tiny URL shortener running at the edge. (See URL Shortener System Design for the full design.)
Step 3: Add KV Storage
npx wrangler kv namespace create LINKSCopy the ID it prints into your config:
// wrangler.jsonc
{
"name": "my-worker",
"main": "src/index.ts",
"compatibility_date": "2026-11-01",
"kv_namespaces": [{ "binding": "LINKS", "id": "" }]
} The binding name (LINKS) is how your code accesses it: env.LINKS.
Step 4: Add a Secret
npx wrangler secret put API_KEYSecrets are encrypted and available as env.API_KEY. For local development, put them in a .dev.vars file (and add it to .gitignore).
Step 5: Run and Deploy
npx wrangler dev # local dev server
npx wrangler deploy # deploy globally
npx wrangler tail # stream live logsWhich Storage to Use?
| Product | What it is | Good for |
|---|---|---|
| KV | Global key-value store, eventually consistent | Config, redirects, cached data, feature flags |
| D1 | Serverless SQLite database | Relational app data |
| R2 | S3-compatible object storage, no egress fees | Files, images, backups |
| Durable Objects | Single-instance stateful objects | Chat rooms, counters, coordination, WebSockets |
| Queues | Message queues | Background jobs |
Limits to Know
- CPU time per request is limited (higher on paid plans); waiting on network I/O doesn't count as CPU time
- Not full Node.js: many Node APIs work via compatibility flags, but some packages won't
- Free plan has a daily request allowance; check Cloudflare's current pricing page
- Bundle size limits apply to your deployed code
Frequently Asked Questions
Are Cloudflare Workers free?
Cloudflare offers a free Workers plan with a daily request allowance and limited CPU time per request, enough for many side projects. The paid plan raises limits and adds more usage. Check Cloudflare's pricing page for current numbers.
How are Cloudflare Workers different from AWS Lambda?
Workers run in lightweight V8 isolates on Cloudflare's global network, so they start almost instantly and run close to users by default. Lambda runs in containers in chosen regions, supports more runtimes and longer-running tasks, and integrates deeply with AWS services.
Can I use npm packages in Cloudflare Workers?
Many npm packages work, especially those built for browsers or standard web APIs. With Node.js compatibility enabled, more Node packages work too, but packages relying on native binaries or unsupported Node APIs may not.
Conclusion
Cloudflare Workers let you run code at the edge with near-zero cold starts: create a project with Wrangler, write a fetch handler, bind storage like KV or D1, add secrets, and deploy globally with one command. Choose storage based on consistency needs, and keep CPU-heavy work off the request path.
Sources
- Cloudflare Workers docs: Get started
- Cloudflare docs: Workers KV
- Cloudflare docs: Workers limits
- Cloudflare docs: How Workers works (isolates)
Last verified: 2 November 2026.
Related reads: URL Shortener System Design ยท Serverless vs Containers ยท Fix CORS Error ยท Load Balancer Explained
I write practical guides for developers. Follow on LinkedIn or X, or invite me to speak.