This is a minimal Workers for Platforms example that demonstrates the core concepts of dynamic dispatch. The platform allows users to create and upload custom Workers through a simple web interface, then access them via friendly URLs.
Workers for Platforms gives your customers the ability to build services and customizations (powered by Workers) while you retain full control over how their code is executed and billed. The dynamic dispatch namespaces feature makes this possible.
By creating a dispatch namespace and using the dispatch_namespaces binding in a regular fetch handler, you have a "dispatch Worker":
export default {
async fetch(request, env) {
// "dispatcher" is a binding defined in wrangler.jsonc
// "my-user-worker" is a script previously uploaded to the dispatch namespace
const worker = env.dispatcher.get("my-user-worker");
return await worker.fetch(request);
}
}This is the perfect way for a platform to create boilerplate functions, handle routing to "user Workers", and sanitize responses. You can manage thousands of Workers with a single Cloudflare Workers account!
Users can upload Workers scripts through a simple web form. The platform uploads the script to a dispatch namespace and stores a name → Worker ID mapping in Workers KV. Users can then access their Workers via URLs like /user-workers/my-worker.
This minimal example focuses on the core Workers for Platforms concepts:
- Dynamic dispatch using the
dispatcherbinding - Worker upload via the Cloudflare API
- Simple name-based routing using KV storage
- Simple Worker Creation: Web form for uploading Worker code
- Dynamic Dispatch: Route requests to user Workers by name
- KV Storage: Store friendly name mappings
- No Dependencies: Pure Workers runtime with minimal external dependencies
Your Cloudflare account needs access to Workers for Platforms.
-
Install the package and dependencies:
npm install -
Create an API token with Workers Scripts (Edit) permission:
Visit https://dash.cloudflare.com/?to=/:account/api-tokens and create a new token with the "Workers Scripts (Edit)" permission.
-
Copy the
.env.testfile to.envand set theCLOUDFLARE_API_TOKENandCLOUDFLARE_ACCOUNT_IDsecrets:cp .env.test .env
Then edit the
.envfile with your actual values:CLOUDFLARE_ACCOUNT_ID = "your_actual_account_id" CLOUDFLARE_API_TOKEN = "your_actual_api_token"
The
.envfile is already in.gitignoreand will not be committed to git.Then run the following commands to add these secrets to your Worker in production:
npx wrangler secret put CLOUDFLARE_API_TOKENnpx wrangler secret put CLOUDFLARE_ACCOUNT_ID -
Create a KV namespace for Worker mappings:
npx wrangler kv:namespace create "WORKER_MAPPINGS"Copy the namespace ID and preview ID into
wrangler.jsoncunder thekv_namespacesbinding. -
Create a dispatch namespace:
npx wrangler dispatch-namespace create workers-for-platforms-example-project -
Run the Worker in dev mode:
npm run devOr deploy to production:
npm run deploy
Once the Worker is live, visit localhost:8787 in a browser. You can create a new Worker via the "/upload" link. Access your Workers at /user-workers/{name}!
Then access it at: http://localhost:8787/user-workers/my-worker