Getting Started with Cloudflare Workers

Learn the basics of Cloudflare Workers and how to deploy your first serverless function.

Getting Started with Cloudflare Workers

Cloudflare Workers allow you to execute JavaScript at the edge, closer to your users than traditional cloud platforms. This guide will walk you through setting up your development environment and deploying your first Worker.

Prerequisites

Before you begin, you’ll need:

  1. A Cloudflare account (free tier is fine for getting started)
  2. Node.js installed on your development machine
  3. Basic knowledge of JavaScript

Setting Up Your Development Environment

The easiest way to get started with Cloudflare Workers is to use Wrangler, Cloudflare’s official CLI tool.

Install Wrangler

npm install -g wrangler

Authenticate Wrangler

Run the following command to log in to your Cloudflare account:

wrangler login

This will open a browser window where you can authorize Wrangler to access your Cloudflare account.

Creating Your First Worker

Generate a New Project

wrangler generate my-first-worker
cd my-first-worker

This creates a new directory with a basic Worker template.

Examine the Default Code

Open src/index.js in your editor. You’ll see something like this:

export default {
	async fetch(request, env, ctx) {
		return new Response("Hello World!");
	},
};

This is a simple Worker that responds with “Hello World!” to all requests.

Customize Your Worker

Let’s modify the code to do something a bit more interesting:

export default {
	async fetch(request, env, ctx) {
		const url = new URL(request.url);
		const name = url.searchParams.get("name") || "Anonymous";

		return new Response(`Hello, ${name}! Welcome to Cloudflare Workers.`, {
			headers: {
				"Content-Type": "text/plain",
			},
		});
	},
};

This Worker now looks for a query parameter called name and personalizes the greeting. Testing Locally One of the benefits of Wrangler is that you can test your Workers locally before deploying. Run the following command:

wrangler dev

This starts a local development server at http://localhost:8787. Try visiting:

http://localhost:8787 http://localhost:8787?name=YourName

Deploying to Cloudflare

When you’re ready to deploy your Worker to Cloudflare’s global network, run:

wrangler publish

After deployment, Wrangler will output the URL where your Worker is now live. Next Steps Now that you’ve deployed your first Worker, you might want to explore:

  • Working with Cloudflare KV for key-value storage
  • Using Durable Objects for stateful applications
  • Integrating with R2 for object storage
  • Setting up custom domains for your Workers

Conclusion

Congratulations! You’ve successfully deployed your first Cloudflare Worker. This is just the beginning of what you can build with Cloudflare’s edge computing platform. As you become more comfortable with Workers, you’ll be able to build increasingly sophisticated applications that take advantage of Cloudflare’s global network.

Need implementation help?

Our team of experts can help you implement these solutions for your specific use case.