Create Your First Worker
Get started with Cloudflare Workers by creating a simple “Hello World” application. This guide will walk you through creating, developing, and deploying your first Worker.Create a new project
Use Create Cloudflare (C3) to scaffold a new Worker project with a single command:C3 will prompt you with several questions:
Navigate to your project
Change into your newly created project directory:Your project structure should look like this:
Explore the Worker code
Open This is the default Worker template with:
src/index.ts to see your Worker code:src/index.ts
Envinterface: Type definitions for bindings (KV, R2, Durable Objects, etc.)fetchhandler: The main entry point that handles incoming HTTP requests- Request/Response: Standard Web API objects
Start the development server
Start Wrangler’s local development server:You should see output like:Open your browser to
http://localhost:8787 to see your Worker in action!The development server includes:
- Hot reloading: Changes are reflected immediately
- Source maps: Debug TypeScript directly
- DevTools: Press
Din the terminal to open Chrome DevTools
Make your first change
Update your Worker to respond with JSON:Save the file and refresh your browser. You should see the JSON response immediately!
src/index.ts
Understanding the Configuration
Yourwrangler.jsonc file contains the Worker configuration:
wrangler.jsonc
Configuration Options
Configuration Options
name: Your Worker’s name (used in the deployment URL)main: Entry point file for your Workercompatibility_date: The version of the Workers runtime to use$schema: Enables autocomplete in your editor
Adding Bindings
Adding Bindings
You can add bindings to KV, R2, D1, and other Cloudflare services:
wrangler.jsonc
Next Steps
Now that you have your first Worker running, explore these topics:Handle Requests
Learn to parse URLs, read headers, and handle different HTTP methods
Add Storage
Connect to KV for key-value storage or D1 for SQL databases
Use Bindings
Integrate with R2, Durable Objects, and other Cloudflare services
Write Tests
Test your Workers locally with Vitest and
@cloudflare/vitest-pool-workersCommon Commands
Here are the most frequently used Wrangler commands:| Command | Description |
|---|---|
wrangler dev | Start local development server |
wrangler deploy | Deploy to production |
wrangler tail | Stream live logs from production |
wrangler kv:namespace create <name> | Create a KV namespace |
wrangler d1 create <name> | Create a D1 database |
wrangler whoami | Check authentication status |
Alternative: Using Wrangler Init
You can also create a Worker using Wrangler directly:Troubleshooting
Port 8787 already in use
Port 8787 already in use
If port 8787 is already in use, specify a different port:
Authentication issues
Authentication issues
If you have trouble authenticating:
- Run
wrangler logoutto clear credentials - Run
wrangler loginto authenticate again - Alternatively, use an API token with
wrangler login --scopes-list
Module not found errors
Module not found errors
Make sure all dependencies are installed:
Need help? Join the Cloudflare Discord or check the GitHub Discussions.