Cloudflare Workers Tutorial for Beginners: Deploy Your First Edge Tool
Cloudflare Workers let you run JavaScript at the edge — on Cloudflare's global network of 300+ data centers — without managing a single server. They're fast, they scale automatically, and the free tier gives you 100,000 requests per day. If you've never used them before, this tutorial will get you from zero to deployed in under 10 minutes.
What Are Cloudflare Workers?
Workers are serverless functions that execute on Cloudflare's edge network. When someone makes a request to your Worker, it runs in the data center closest to the user — not in a centralized region like traditional cloud functions. This means lower latency and faster response times for users everywhere in the world.
Think of Workers as lightweight API endpoints. They can:
- Return HTML pages, JSON responses, or redirect URLs
- Transform requests and responses on the fly
- Connect to databases, external APIs, and storage
- Run scheduled tasks with Cron Triggers
- Handle authentication, rate limiting, and caching logic
Unlike traditional servers, you don't pay for idle time. You pay per request (and the first 100,000 per day are free). There's no cold start problem like AWS Lambda — Workers spin up in under 5 milliseconds.
Prerequisites
You'll need three things:
- A free Cloudflare account — Sign up at
cloudflare.com - Node.js installed — Version 16 or later
- A terminal — Any command line will work
Step-by-Step: Deploy Your First Worker
Install Wrangler (the Cloudflare CLI)
Wrangler is the official CLI for building and deploying Workers.
npm install -g wrangler
Once installed, authenticate with your Cloudflare account:
wrangler login
This opens your browser. Click "Allow" to connect Wrangler to your account.
Create a New Worker Project
Generate a fresh project scaffold:
wrangler init my-first-worker
cd my-first-worker
Select "Hello World" when prompted for a template. This creates a minimal Worker with one file: src/index.js.
Write Your Worker Logic
Open src/index.js and replace the contents with a simple JSON API:
export default {
async fetch(request) {
const url = new URL(request.url);
// Simple routing
if (url.pathname === "/api/hello") {
return new Response(
JSON.stringify({
message: "Hello from the edge!",
timestamp: new Date().toISOString(),
location: request.cf?.colo || "unknown"
}),
{
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
}
}
);
}
// Default response
return new Response("My First Cloudflare Worker", {
headers: { "Content-Type": "text/plain" }
});
}
};
This Worker responds to /api/hello with a JSON payload that includes the Cloudflare data center code serving the request. Every other path returns a plain text greeting.
Test Locally
Run the Worker on your machine before deploying:
wrangler dev
Open http://localhost:8787/api/hello in your browser. You should see the JSON response. Make changes to the code and they'll hot-reload instantly.
Deploy to Production
When you're ready, deploy with one command:
wrangler deploy
Wrangler outputs your live URL, something like my-first-worker.your-subdomain.workers.dev. Your Worker is now running on Cloudflare's global network. Every request hits the nearest edge node automatically.
What to Build Next
Now that you've deployed your first Worker, here are practical projects to try next:
- URL shortener — Store short URLs in Workers KV (Cloudflare's key-value store) and redirect on the edge
- Password generator API — Generate cryptographically secure passwords server-side and return them as JSON
- Image optimizer — Accept image uploads, resize/compress using Workers, return optimized versions
- Rate limiter — Protect any API with token bucket rate limiting at the edge
- Webhook relay — Accept webhooks from one service and transform/forward them to another
Each of these projects is already built, tested, and ready to deploy in the SpunkArt Store. The collection includes 18 Cloudflare Workers covering the most common use cases.
Skip the Tutorial — Deploy 18 Workers Today
SpunkArt's complete collection includes password generators, URL shorteners, PDF tools, rate limiters, and more. Production-ready code, one-click deploy, full documentation.
Browse All 18 WorkersKey Concepts to Learn
As you build more Workers, these concepts will become important:
- Workers KV — A global key-value store for persistent data. Free tier includes 100,000 reads/day.
- Durable Objects — For stateful applications that need coordination (chat rooms, game servers, counters).
- R2 Storage — S3-compatible object storage with zero egress fees. Ideal for file uploads.
- Cron Triggers — Schedule Workers to run on a recurring basis without external schedulers.
Cloudflare's free tier is generous enough to run serious production tools. SpunkArt runs its entire 18-tool suite and multiple sites on Workers, with zero infrastructure costs.
Further Reading
If you want to understand the broader strategy of building an entire business on top of free hosting and edge computing, read the full case study: How I Built 120+ Websites in 7 Days Using AI.
For the complete playbook with source code and deployment scripts, grab the SpunkArt Ebook. And follow @SpunkArt13 for new tutorials and tool launches.