---
title: "Agent setup"
---

> Documentation Index
> Fetch the complete documentation index at: https://workerd.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Agent setup

These instructions are for a coding agent. They install workerd on this
machine and run a first application.

Run each command yourself. Ask the user only where a step tells you to.

1. Check the machine
2. Install workerd
3. Run a hello-world Worker
4. Run a Durable Object with storage
5. Run the user's project, if there is one
6. Tell the user what you did

---

## Check the machine

1. **Operating system.** Run `uname -sm`. On Windows, check the processor
   architecture. The `workerd` npm package supplies binaries for Linux
   x86-64, Linux ARM64, macOS, and Windows x86-64. On any other platform,
   stop. Tell the user that the npm package has no binary for it.
2. **An existing project.** Check the current directory for
   `wrangler.jsonc`, `wrangler.json`, and `wrangler.toml`.
   - `wrangler.jsonc` or `wrangler.json`: workerd reads this file. Do the
     hello-world steps in a scratch directory. Then go to
     [Run the user's project](#run-the-users-project).
   - Only `wrangler.toml`: workerd does not read this file. Tell the user.
     Ask before you convert it to `wrangler.jsonc`.
   - None: work in a new, empty directory.

   workerd will soon support `worker.config.json`, the file of the `cf`
   command, in place of `wrangler.json`. See
   [Upcoming changes](https://workerd.dev/docs/limitations/#upcoming-changes).
3. **Port 8787.** workerd listens on `localhost:8787`. Make sure that no
   other process, for example `wrangler dev`, listens on this port. If the
   port is in use, add `-s http=localhost:8788`, or another free port, to
   each `npx workerd serve` command below. Use the same port in each `curl`
   command.

---

## Install workerd

1. If the project directory has no `package.json`, run `npm init -y`.
2. Install workerd and esbuild in the project directory. workerd bundles
   `main` with esbuild, as Wrangler does.

   ```sh
   npm install -D workerd esbuild
   ```

   Instead, esbuild can be on `PATH`, or `WORKERD_ESBUILD` can name its
   executable.

Run `npx workerd serve -h` to list the flags of `serve` in the installed
version.

This setup does not use these other ways to get workerd:

- **Docker.** The image `cloudflare/workerd` runs `workerd serve /app` on
  port 8787. It has no esbuild, so the project must set
  `"no_bundle": true`. Read
  `https://workerd.dev/docs/cluster/index.md#run-in-docker`.
- **Releases.** The releases are at
  `https://github.com/cloudflare/workerd/releases`. The documentation does
  not describe the release files.

---

## Run a hello-world Worker

Create a `hello/` directory with these two files. The Worker returns a text
response from a `fetch` handler.

```js title="index.js"
export default {
  async fetch(request, env) {
    return new Response("Hello from workerd! url=" + request.url, {
      status: 200,
    });
  },
};
```

```json title="wrangler.json"
{ "name": "hello", "main": "index.js", "compatibility_date": "2026-01-01", "no_bundle": true }
```

Start workerd as a background process. It runs until you stop it.

```sh
npx workerd serve ./hello/wrangler.json
```

Send a request to it:

```sh
curl http://localhost:8787/
```

The response is `Hello from workerd! url=http://localhost:8787/`. Stop
workerd, because the next step uses the same port.

---

## Run a Durable Object with storage

A Durable Object is a single-threaded actor with its own SQLite database. On
one machine, workerd keeps the database across restarts, in
`.wrangler/state/workerd` beside the configuration.

Create a `counter/` directory with these two files. The Worker gives each
`name` its own counter. It reaches the class through `ctx.exports.Counter`,
so it needs no binding.

```js title="index.js"
export class Counter {
  constructor(state, env) {
    this.state = state;
  }
  async fetch(request) {
    let n = (await this.state.storage.get("n")) ?? 0;
    n++;
    await this.state.storage.put("n", n);
    return Response.json({ n, url: request.url });
  }
}
export default {
  async fetch(request, env, ctx) {
    const name = new URL(request.url).searchParams.get("name") ?? "default";
    const id = ctx.exports.Counter.idFromName(name);
    return ctx.exports.Counter.get(id).fetch(request);
  },
};
```

```jsonc title="wrangler.jsonc"
{
  "name": "counter",
  "main": "index.js",
  "compatibility_date": "2026-01-01",
  "no_bundle": true,
  "exports": {
    "Counter": { "type": "durable-object", "storage": "sqlite" }
  }
}
```

Start workerd in the background. Then send the same request two times:

```sh
npx workerd serve ./counter/wrangler.jsonc
curl 'http://localhost:8787/?name=alice'
curl 'http://localhost:8787/?name=alice'
```

The responses have `"n":1` and `"n":2`. Stop workerd and start it again.
Send the request once more. The response has `"n":3`, because the state
survived the restart.

The `exports` map in the configuration declares each Durable Object class.
`"storage": "sqlite"` gives the class a SQLite database. `ctx.exports`
needs a compatibility date of `2025-11-17` or later.

---

## Run the user's project

If the user has no Wrangler configuration, skip this section.

workerd is not a hardened sandbox. Do not run untrusted code outside a
virtual machine or another secure sandbox.

Before you start the project, compare its configuration with what workerd
accepts:

1. Run `npx workerd -h`. Compare the fields in its Config section with the
   top-level keys of the project's configuration.
2. A top-level key that the help does not list stops startup, for example
   `routes`, `services`, or `containers`. Tell the user which keys stop
   startup. Ask before you remove them.
3. If the project uses a field that the help lists under
   `--experimental`, add `--experimental` to the command.
4. If the project uses Containers, Workers AI, Vectorize, Hyperdrive,
   Browser Rendering, or Email Workers, tell the user. workerd does not
   implement these services.

Dynamic Workers, Python Workers, and service bindings between Workers need
a `config.capnp`. A Wrangler configuration cannot declare them.

Start the project. If the file is `wrangler.json`, use `./wrangler.json`.

```sh
npx workerd serve ./wrangler.jsonc
```

workerd does not read `.dev.vars` or `.env`. If the project keeps local
values in these files, tell the user. To rebuild and restart workerd on a
change, add `--watch`. Add `.wrangler/` to `.gitignore`.

workerd does not migrate stored state. After a configuration change, an
error that looks unrelated can come from old state in
`.wrangler/state/workerd`. Tell the user that a delete removes local data.
Then delete the directory to start from an empty state.

---

When you finish, tell the user:

```
┌─ workerd Agent Setup Complete ───────────────────────┐
│  ✓ workerd   <version from npm ls workerd>           │
│  ✓ hello     http://localhost:8787/                  │
│  ✓ counter   n survived a restart                    │
│  ✓ project   <path, or "none">                       │
└──────────────────────────────────────────────────────┘
```

---

## Next steps

Each service page, listed at `https://workerd.dev/docs/index.md#services`,
has an example and the differences from Cloudflare.

Do not start a cluster unless the user asks for one. To run several
instances as a cluster on one object store bucket, read
`https://workerd.dev/docs/index.md` from "Run a cluster" on. Then read
`https://workerd.dev/docs/cluster/index.md`.

For the supported APIs and services, read
`https://workerd.dev/docs/cloudflare-compat/index.md`. For the operational
limits, read `https://workerd.dev/docs/limitations/index.md`.

You can read these instructions again at
`https://workerd.dev/docs/agent-setup/prompt.md`.

Source: https://workerd.dev/docs/agent-setup/index.mdx
