Skip to content

Workers

A Worker is a request handler that uses the Cloudflare Workers API. workerd runs each Worker in a V8 isolate, as Cloudflare does. In a cluster, every instance runs the same Worker, so a load balancer can send a request to any instance. Read the handlers documentation for the API.

Example

The hello example returns a text response from a fetch handler.

index.jsjs
export default {
  async fetch(request, env) {
    return new Response("Hello from workerd! url=" + request.url, {
      status: 200,
    });
  },
};
wrangler.jsonjson
{ "name": "hello", "main": "index.js", "compatibility_date": "2026-01-01", "no_bundle": true }
npx workerd serve ./hello/wrangler.json
curl http://localhost:8787/

workerd listens on localhost:8787, as wrangler dev does. To listen on another address, pass -s http=ADDRESS, for example -s http=*:8080.

The examples set "no_bundle": true, so they do not need esbuild. Without it, workerd bundles main with esbuild, as Wrangler does. The workerd npm package does not include esbuild. Install it with npm install -D esbuild.

workerd will soon support worker.config.json, the file of the cf command, in place of wrangler.json. See Upcoming changes.

Differences from Cloudflare

  • A wrangler.json project serves plain HTTP. To serve HTTPS, put a proxy in front of workerd, or use an https socket in a config.capnp. workerd does not manage a custom domain.
  • workerd refuses a Wrangler field that it does not support, such as ai for Workers AI. It also refuses fields that apply only to a deploy to Cloudflare, such as observability, routes, account_id, and workers_dev. Remove these fields before you serve the project.

See also Cloudflare compatibility.

Python Workers

workerd runs Python Workers on Pyodide, as Cloudflare does. A wrangler.json cannot name a .py file as main, so the Python example uses a config.capnp, the native workerd configuration format. The Worker returns the SHA-256 and the word count of the request body.

config.capnptext
using Workerd = import "/workerd/workerd.capnp";

const config :Workerd.Config = (
  services = [(name = "main", worker = .worker)],
  sockets = [
    (name = "http", address = "localhost:8787", http = (), service = "main"),
  ],
);

const worker :Workerd.Worker = (
  modules = [(name = "entry.py", pythonModule = embed "src/entry.py")],
  compatibilityDate = "2026-01-01",
  compatibilityFlags = ["python_workers"],
);
src/entry.pypython
# POST text to get its SHA-256 and its word count.
import hashlib

from workers import Response, WorkerEntrypoint


class Default(WorkerEntrypoint):
    async def fetch(self, request):
        text = await request.text()
        return Response.json({
            "sha256": hashlib.sha256(text.encode()).hexdigest(),
            "words": len(text.split()),
        })
npx workerd serve ./python/config.capnp
curl --data 'hello from python' http://localhost:8787/
  • A Python Worker needs the python_workers compatibility flag. The compatibility date selects the Pyodide and Python versions, as on Cloudflare.
  • workerd downloads the Pyodide bundle, about 17 MB, when it starts. To keep a copy on disk, pass --pyodide-bundle-disk-cache-dir DIR.
  • From compatibility date 2026-04-21, the workers SDK is not built in. To use the built-in SDK with a later date, add the disable_python_external_sdk flag.
  • workerd does not read pyproject.toml or scan python_modules/. A config.capnp names each module, so pywrangler sync adds no package. The example uses the standard library and the built-in workers SDK, because its compatibility date is before 2026-04-21.
  • A Python Worker works in a cluster. To join one, pass --experimental and --cluster-addr. The config.capnp must also have a writable disk service named do-storage.

Type to search…

↑↓ navigate↵ selectEsc close