Skip to content

Configure Workers for Platforms

Prerequisites:

Enable Workers for Platforms

To enable Workers for Platforms, you will need to purchase the Workers for Platforms Paid plan.

  1. Log in to the Cloudflare dashboard, and select your account.
  2. Complete the payment process for the Workers for Platforms Paid plan.

If you are an Enterprise customer, contact your Cloudflare account team to enable Workers for Platforms.

Learn about Workers for Platforms

Refer to How Workers for Platforms works to learn more about Workers for Platforms terminology and architecture.


This guide will instruct you on setting up Workers for Platforms. You will configure a dispatch namespace, a dynamic dispatch Worker and a user Worker to test a request end to end.

1. Create a user Worker

First, create a user Workers. User Workers are Workers that your end users (end developers) will be uploading.

User Workers can be created using C3. C3 (create-cloudflare-cli) is a command-line tool designed to help you setup and deploy Workers to Cloudflare as fast as possible.

Open a terminal window and run C3 to create your Worker project. This example creates a user Worker called customer-worker-1.

Terminal window
npm create cloudflare@latest customer-worker-1 -- --type=hello-world

When following the interactive prompts, answer the questions as below:

  • Select no to using TypeScript.
  • Select no to deploying your application.

2. Create a dispatch namespace

Create a dispatch namespace. A dispatch namespace is made up of a collection of user Workers.

This example creates a dispatch namespace called testing. To create a dispatch namespace, run:

Terminal window
npx wrangler dispatch-namespace create testing

3. Upload a user Worker to the dispatch namespace

Make sure you are in your user Worker's project directory:

Terminal window
cd customer-worker-1

To upload and deploy the user Worker to the dispatch namespace, running the following command:

Terminal window
npx wrangler deploy --dispatch-namespace testing

4. Create a dispatch Worker

Dispatch Workers are used to execute user Workers from the dispatch namespace. You will now create a dispatch Worker and add logic it needs to route to the user Worker created in step 2.

Navigate up a level from your user Worker's project directory:

Terminal window
cd ..

Create your dispatch Worker. In this example, the dispatch Worker is called my-dispatcher.

Terminal window
npm create cloudflare@latest -- my-dispatcher

For setup, select the following options:

  • For What would you like to start with?, choose Hello World example.
  • For Which template would you like to use?, choose Hello World Worker.
  • For Which language do you want to use?, choose JavaScript.
  • For Do you want to use git for version control?, choose Yes.
  • For Do you want to deploy your application?, choose No (we will be making some changes before deploying).

Change to your project's directory:

Terminal window
cd my-dispatcher

Open the wrangler.toml file in your project directory, and add the dispatch namespace binding:

[[dispatch_namespaces]]
binding = "DISPATCHER"
namespace = "testing"

Add the following to the index.js file:

export default {
async fetch(req, env) {
const worker = env.DISPATCHER.get("customer-worker-1");
return await worker.fetch(req);
},
};

Deploy your dispatch Worker:

Terminal window
npx wrangler deploy

5. Test a request

You will now send a request to the route your dynamic dispatch Worker is on. You should receive the response (Hello world) you created in your user Worker (customer-worker-1) that you call from your dynamic dispatch Worker (my-dispatcher).

Preview the response to your Workers for Platforms project at https://my-dispatcher.<YOUR_WORKER_SUBDOMAIN>.workers.dev/.

By completing this guide, you have successfully set up a dispatch namespace, dynamic dispatch Worker and user Worker to test a request end to end.