Workers for Platforms enables you to deploy Workers on behalf of your customers. This API provides multi-tenant isolation through dispatch namespaces.
Overview
Access the Workers for Platforms API:
import Cloudflare from 'cloudflare';
const client = new Cloudflare({
apiToken: process.env.CLOUDFLARE_API_TOKEN,
});
// Access Workers for Platforms
const wfp = client.workersForPlatforms;
Dispatch namespaces
Dispatch namespaces provide isolation between customer Workers in a multi-tenant environment.
Create a dispatch namespace
Create a new namespace for isolating customer Workers.
const namespace = await client.workersForPlatforms.dispatch.namespaces.create({
account_id: '023e105f4ecef8ad9ca31a8372d0c353',
name: 'customer-namespace',
});
Your Cloudflare account ID
Name for the dispatch namespace
The unique identifier for the namespace
ISO 8601 timestamp when the namespace was created
List dispatch namespaces
Retrieve all dispatch namespaces in your account.
for await (const namespace of client.workersForPlatforms.dispatch.namespaces.list({
account_id: '023e105f4ecef8ad9ca31a8372d0c353',
})) {
console.log(namespace);
}
Your Cloudflare account ID
Get a dispatch namespace
Retrieve details about a specific dispatch namespace.
const namespace = await client.workersForPlatforms.dispatch.namespaces.get(
'customer-namespace',
{ account_id: '023e105f4ecef8ad9ca31a8372d0c353' }
);
The name of the dispatch namespace
Your Cloudflare account ID
Delete a dispatch namespace
Delete a dispatch namespace and all Workers within it.
await client.workersForPlatforms.dispatch.namespaces.delete(
'customer-namespace',
{ account_id: '023e105f4ecef8ad9ca31a8372d0c353' }
);
The name of the dispatch namespace to delete
Your Cloudflare account ID
Use cases
Use dispatch namespaces to isolate customer code:
// Create a namespace for each customer
const customerNamespace = await client.workersForPlatforms.dispatch.namespaces.create({
account_id: accountId,
name: `customer-${customerId}`,
});
// Deploy customer's Worker to their namespace
// This ensures isolation from other customers
Dynamic Worker deployment
Deploy Workers programmatically for your users:
// List all customer namespaces
const namespaces = await client.workersForPlatforms.dispatch.namespaces.list({
account_id: accountId,
});
// Deploy or update Workers in each namespace as needed
for await (const namespace of namespaces) {
console.log(`Namespace: ${namespace.name}`);
}
Best practices
- Namespace naming: Use a consistent naming convention for customer namespaces (e.g.,
customer-{id})
- Isolation: Each customer should have their own namespace to ensure security and isolation
- Cleanup: Delete namespaces when customers churn to free up resources
- Monitoring: Track namespace usage and performance per customer