Skip to content

Cloudflare.WorkersForPlatforms reference

Source: packages/cloudflare/src/WorkersForPlatforms/DispatchNamespace.ts

A Workers for Platforms dispatch namespace — a container for customer (“user”) Workers that a platform Worker dispatches to at runtime via a dynamic-dispatch binding.

The namespace has no mutable properties: its name is its identity, so changing the name triggers a replacement. Deleting a namespace also deletes every script uploaded into it.

Note: Workers for Platforms is a paid add-on. On accounts without the subscription, namespace creation fails with an entitlement error.

DispatchNamespace: Creating a Dispatch Namespace

Section titled “DispatchNamespace: Creating a Dispatch Namespace”

Namespace with a generated name

const namespace = yield* Cloudflare.WorkersForPlatforms.DispatchNamespace("Customers", {});

Namespace with an explicit name

const namespace = yield* Cloudflare.WorkersForPlatforms.DispatchNamespace("Customers", {
name: "my-platform-customers",
});

A Cloudflare.Worker deploys into the namespace as a “user worker” (rather than as a routable account-level script) when its namespace prop is set. Reference the namespace by its name output so it deploys first.

const namespace = yield* Cloudflare.WorkersForPlatforms.DispatchNamespace("Customers", {});
const customerA = yield* Cloudflare.Worker("CustomerA", {
namespace: namespace.name,
script: `export default { fetch() { return new Response("hi"); } }`,
});

DispatchNamespace: Dispatching from a platform Worker

Section titled “DispatchNamespace: Dispatching from a platform Worker”

Effect-native binding via Get

Cloudflare.WorkersForPlatforms.Get(namespace) binds the namespace and returns an Effect-native client; get(name) resolves a user Worker by script name. Provide GetBinding on the Worker’s runtime layer.

const dispatch = yield* Cloudflare.WorkersForPlatforms.Get(namespace);
return {
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
const userWorker = yield* dispatch.get("CustomerA");
return yield* Effect.promise(() => userWorker.fetch(request));
}),
};

Async binding via env + InferEnv

Passing the namespace on a Worker’s env binds it as a native dispatch_namespace binding; Cloudflare.InferEnv types env.DISPATCH as the runtime DispatchNamespace, so the async handler calls .get(name) directly.

const platform = Cloudflare.Worker("Platform", {
main: "./handler.ts",
env: { DISPATCH: namespace },
});
type Env = Cloudflare.InferEnv<typeof platform>;
// handler.ts
export default {
async fetch(request: Request, env: Env) {
return env.DISPATCH.get("CustomerA").fetch(request);
},
};

DispatchNamespace: Looking up an existing namespace

Section titled “DispatchNamespace: Looking up an existing namespace”

DispatchNamespace.lookup references a namespace Alchemy doesn’t manage (created in the dashboard or by another stack), found by name. It is read during plan/deploy and never created, updated or deleted.

const namespace = Cloudflare.WorkersForPlatforms.DispatchNamespace.lookup({
name: "customers",
});
const platform = yield* Cloudflare.Worker("Platform", {
main: "./handler.ts",
env: { DISPATCH: namespace },
});
const customerA = yield* Cloudflare.Worker("CustomerA", {
namespace,
main: "./customer.ts",
});

Source: packages/cloudflare/src/WorkersForPlatforms/Get.ts

Bind a DispatchNamespace to a Worker and obtain the Effect-native dynamic-dispatch client (get).

Get is a single identifier that is simultaneously the binding’s Context tag, its type, and the callable — yield* Cloudflare.WorkersForPlatforms.Get(namespace). The value it returns is also an entry on an async Worker’s env.

Provide GetBinding on the Worker’s runtime layer to resolve the underlying dispatch_namespace binding at request time.

Bind the namespace during the Worker’s init phase, then look up and forward to a user Worker from a request handler.

const dispatch = yield* Cloudflare.WorkersForPlatforms.Get(namespace);
return {
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
const userWorker = yield* dispatch.get("customer-a");
return yield* Effect.promise(() => userWorker.fetch(request));
}),
};

Get: Routing user Workers’ requests through an outbound Worker

Section titled “Get: Routing user Workers’ requests through an outbound Worker”

Declare an outbound Worker and its values

Every fetch() a dispatched user Worker makes goes to the outbound Worker instead of the internet. The type parameter declares the values each dispatch passes it, and parameters lists their names. Every dispatch must pass every value (null for none): Cloudflare refuses one that leaves a value out. In alchemy dev the binding has no outbound Worker, because Cloudflare’s preview refuses one: dispatches work, and user Workers reach the internet directly.

const Egress = Cloudflare.Worker("Egress", { main: "./src/egress.ts" });
export const Dispatch = Cloudflare.WorkersForPlatforms.Get<{ tenant: string }>(Namespace, {
outbound: Egress,
parameters: ["tenant"],
});

Dispatch from an async Worker

InferEnv types get, so a missing or mistyped value is a compile error.

export const Dispatcher = Cloudflare.Worker("Dispatcher", {
main: "./src/dispatcher.ts",
env: { NS: Dispatch },
});
export type DispatcherEnv = Cloudflare.InferEnv<typeof Dispatcher>;
// src/dispatcher.ts
const userWorker = env.NS.get(script, {}, { outbound: { tenant: script } });

Dispatch from an Effect-native Worker

const dispatch = yield* Dispatch;
const userWorker = yield* dispatch.get(script, {}, { outbound: { tenant: script } });

Read the values in the outbound Worker

src/egress.ts
export default {
fetch(request: Request, env: { tenant: string }) {
console.log(env.tenant);
return fetch(request);
},
};

Read the values in an Effect-native outbound Worker

OutboundParameters yields the values of the current dispatch, so read it inside the handler.

src/egress.ts
export default class Egress extends Cloudflare.Worker<Egress>()(
"Egress",
{ main: import.meta.filename },
Effect.gen(function* () {
return {
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
const { tenant } = yield* Cloudflare.WorkersForPlatforms.OutboundParameters<{
tenant: string;
}>();
return HttpServerResponse.text(`${tenant} asked for ${request.url}`);
}),
};
}),
) {}