Your First Workers Project (From Hello World to Deployment)
On this page18 items
The previous three pages were about protecting things that already exist. From here on, we move to building things yourself.
In Chapter 5’s Cloudflare Workers page, we explained serverless computing that runs at the edge. On this page, you’ll actually write code and deploy it across Cloudflare’s network around the world. It takes about five minutes and costs nothing.
You don’t need to rent a server, choose an OS, or prepare a certificate. Run two commands and your code starts running in more than 300 cities worldwide. It’s an experience worth trying at least once.
What you’ll learn on this page#
- What you need to prepare for Workers development (Node.js and Wrangler)
- How to create a project and run it locally
- The command to deploy worldwide, and the URL it’s published at
- How to read the configuration file
wrangler.jsonc - How to assign a custom domain, and how to handle secrets
What you need#
You only need two things.
- A Cloudflare account (free; if you already have one, that’s fine)
- Node.js (version 16.17.0 or later)
You can check whether Node.js is installed by running the following command in your terminal.
node -vIf a version like v20.11.0 is displayed, you’re good. If you see “command not found,” install the LTS version from the official Node.js website.
You don’t need a domain. At first, you can run it at a workers.dev URL provided by Cloudflare. Assigning a custom domain is covered later on this page.
Step 1: Create a project#
In your terminal, move to the folder you want to work in and run the following command.
npm create cloudflare@latest -- my-first-workermy-first-worker is both the project name and the folder name. Any name is fine (letters, numbers, and hyphens only).
This command is called C3 (create-cloudflare CLI), and it takes care of everything from creating the template to deploying. When you run it, you’ll be asked a few questions.
| Question | What to choose |
|---|---|
| What would you like to start with? | Hello World example |
| Which template would you like to use? | Worker only |
| Which language do you want to use? | JavaScript |
| Do you want to use git for version control? | Yes |
| Do you want to deploy your application? | No (to check it locally first) |
You can choose TypeScript, but for your first time, JavaScript has fewer extra pieces and is easier to read.
The files created#
When it finishes, the my-first-worker folder contains the following.
my-first-worker/
├── src/
│ └── index.js ← your code
├── wrangler.jsonc ← configuration file
├── package.json
└── node_modules/Here’s everything in src/index.js.
export default {
async fetch(request, env, ctx) {
return new Response("Hello World!");
},
};This brevity is the essence of Workers. When a request arrives, fetch is called, and the Response you return becomes the response as is. There’s no server startup code, no listening on a port, no routing framework.
The three arguments mean the following.
request: The incoming request. It contains the URL, method, headers, and bodyenv: External resources connected via bindings (KV, D1, R2, etc.) and environment variablesctx: Execution control. You can use things likewaitUntil, which keeps processing after the response has been returned
Step 2: Run it locally#
Move into the project folder and start the development server.
cd my-first-worker
npx wrangler devhttp://localhost:8787 will be displayed; open it in your browser. If you see “Hello World!”, it worked.
What’s important here is that this is not just a Node.js emulation. Wrangler runs workerd, the same runtime as Cloudflare’s production environment, on your machine. In other words, if it works here, it will almost certainly work the same way in production.
When you edit and save src/index.js, the change is applied automatically. Try changing the string.
return new Response("Hello, Workers!");If non-ASCII text such as Japanese comes out garbled, specify the character encoding explicitly.
return new Response("Hello, Workers!", {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});To stop it, press Ctrl + C.

Step 3: Deploy worldwide#
Now for the real thing. Run the following command.
npx wrangler deployThe first time, a browser opens and asks you to link your Cloudflare account. Once you allow it, you’re back in the terminal and the deployment proceeds.
A few seconds later, a URL like this is displayed.
https://my-first-worker.your-subdomain.workers.devOpen this URL. Your code runs from anywhere in the world.
Let’s go over what happened in those few seconds.
- Your code was uploaded to Cloudflare
- It was distributed to servers in more than 300 cities worldwide
- An HTTPS certificate was set up automatically
- DNS was configured automatically
- Whoever accesses it, it now runs at the location closest to them
You didn’t rent a single server, choose a single OS, or apply for a single certificate. That’s what serverless, running at the edge, means.
How far the free tier goes#
No charges have been incurred by deploying. The free tier is as follows.
| Item | Free plan |
|---|---|
| Requests | 100,000 per day |
| CPU time | 10 milliseconds per request |
| Number of Workers | 100 |
For personal experiments and small tools, this is plenty. See the Workers page for details.
Step 4: Read the configuration file#
Open wrangler.jsonc and you’ll find just this bare minimum.
{
"name": "my-first-worker",
"main": "src/index.js",
"compatibility_date": "2026-08-20"
}name: The Worker’s name. It becomes part of theworkers.devURLmain: The entry-point filecompatibility_date: This one is important
What is compatibility_date?#
compatibility_date pins which point in time’s runtime behavior your Worker runs with.
Cloudflare improves its runtime constantly, and some of those fixes change existing behavior. If the latest behavior were always applied, your Worker could suddenly behave differently one day even though you didn’t touch anything.
By setting compatibility_date, the behavior as of that date is preserved. When you want to use new features, you move the date forward, test, and deploy — you get to choose when that happens.
As for the configuration file format, wrangler.jsonc is recommended for new projects. The traditional wrangler.toml still works, but some new features are only available in the JSON format.
Step 5: Assign a custom domain#
The workers.dev URL is convenient, but when you actually publish something, you’ll want to use your own domain.
If it’s a domain managed on Cloudflare, you just add a few lines to the configuration file.
{
"name": "my-first-worker",
"main": "src/index.js",
"compatibility_date": "2026-08-20",
"routes": [
{ "pattern": "api.example.com", "custom_domain": true }
]
}Save it and run npx wrangler deploy, and the DNS record and certificate are set up automatically. You don’t need to write a CNAME yourself or apply for a certificate.
You can also have a Worker handle just part of a site.
"routes": [
{ "pattern": "example.com/api/*", "zone_name": "example.com" }
]Written this way, only requests under example.com/api/ go to the Worker, and everything else flows to the origin server as usual. You can move just part of the functionality to the edge without touching the existing site.
Making the code a little more practical#
Hello World on its own isn’t very interesting, so let’s change the response depending on the URL.
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
if (url.pathname === "/time") {
return Response.json({
now: new Date().toISOString(),
colo: request.cf?.colo,
country: request.cf?.country,
});
}
if (url.pathname === "/hello") {
const name = url.searchParams.get("name") ?? "World";
return new Response(`Hello, ${name}!`, {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
return new Response("Not Found", { status: 404 });
},
};Access /time and, along with the time, you get colo (which city’s data center ran it) and country (the country the request came from). request.cf is information Cloudflare attaches to the request, and it’s the best proof that your code is running at the edge.
If you access it over your smartphone’s mobile connection, the colo value may change. You should be able to feel that the same code is running in different places depending on who accesses it.
Handling secrets#
Never write things like API keys directly in your code. They’d end up in Git and get published.
npx wrangler secret put API_KEYRunning this command prompts you for the value, which is then stored encrypted on Cloudflare’s side. Your code can read it as env.API_KEY.
export default {
async fetch(request, env, ctx) {
const key = env.API_KEY; // Not left in the config file or in Git
// ...
},
};Settings that aren’t secret (such as an operating mode) can go in vars in the configuration file.
Viewing logs#
You can see what your deployed Worker is doing in production with the following command.
npx wrangler tailLeave it running and access the production URL, and console.log output and errors stream in in real time. This is the replacement for SSHing into a server and tailing log files.
For Workers with a lot of traffic, some entries may be thinned out (sampled).

Where to take it from here#
Once Hello World works, connect it to other services through env.
- Workers KV: Store settings and counters
- D1: Work with data in SQL
- R2: Store images and files
- Workers AI: Call AI models
- Durable Objects: Give it state
The connection pattern is the same for all of them: write a binding in the configuration file and use it from your code as env.something. That’s it. No connection strings, no credentials, no network configuration.
Points beginners should watch out for#
wrangler deploygoes to production immediately: There’s no confirmation screen. When experimenting, use a different name or the environments feature- Don’t misunderstand what 10 ms of CPU time means: It’s “time spent computing,” and doesn’t include time spent waiting for responses from external APIs. You only run out on the free tier if you do heavy computation
- Why
npxis prefixed: It uses the Wrangler installed in the project. If it gets mixed up with a globally installed one, version differences can stop things from working - Don’t commit
node_modulesto Git: It’s already listed in the.gitignorecreated by C3. Don’t remove it - Don’t put secrets in
vars:varsstay in the configuration file in plain text. Always usesecret putfor API keys - If it works locally, it almost always works in production: However, binding targets (KV, D1) are separate between local and production. It’s normal for the data to be empty
- It’s fine to delete your first Worker: You can delete it with
npx wrangler delete. Keeping experimental Workers around eats into the Worker limit (100 on Free)
Related services#
- What Is Cloudflare Workers?: How it works and its limits (background for this page)
- What Is Cloudflare Pages?: When working with static sites
- What Is Workers KV?: Storage that’s easy to understand as your first connection
- What Is D1?: Work with data in SQL
- What Is Durable Objects?: When you want to give it state
- What Is Workers AI?: Build in AI
Summary#
- All you need is a Cloudflare account and Node.js (16.17.0 or later). You can start without a domain
npm create cloudflare@latest -- my-first-workercreates the templatenpx wrangler devstarts it locally. It runs the same workerd runtime as production, so if it works here, it works therenpx wrangler deploydistributes it to more than 300 cities worldwide, with HTTPS set up automaticallycompatibility_dateis a date that pins the runtime behavior, preventing it from changing on its ownwrangler.jsoncis the recommended configuration file for new projects- For a custom domain, just add
custom_domain: trueunderroutes. DNS and certificates are automatic - Secrets go through
wrangler secret put; production logs throughwrangler tail - Connections to other services are all unified in the form
env.something
Two commands and your code is distributed around the world — that sense of accomplishment is the biggest reason to use Cloudflare’s developer platform.
On the next page, you’ll publish not code but a site itself: the steps for publishing a set of HTML files, or a site built with a static site generator, on Cloudflare.