User Script Dispatcher
Workers for Platforms style dispatch via DispatchNamespace or KV handlers
Workers for Platforms style dispatch - register customers and run tenant response handlers via a DispatchNamespace binding, with a KV fallback for local demos. Stored scripts return a fixed JSON response (safe demo - no dynamic JS eval).
API Reference
POST /scripts
Store a JSON response handler.
name string (required)
Tenant/script name (a-zA-Z0-9_-).
response object (required)
JSON object returned on KV dispatch. Max serialized size: 10,000 bytes.
Example Request
curl -X POST "https://your-worker.workers.dev/scripts" \
-H "Content-Type: application/json" \
-d '{"name":"tenant-a","response":{"hello":"world"}}'GET /scripts
name string (required)
Returns { name, updatedAt, hasResponse }.
Example Request
curl "https://your-worker.workers.dev/scripts?name=tenant-a"POST /dispatch/:name
- If
DISPATCHERis bound: forwards the request toenv.DISPATCHER.get(name).fetch(request). - If
DISPATCHER.getthrows:{ error, code: "DISPATCH_ERROR" }(502). - Without
DISPATCHER(or when using the KV path): returns the stored KVresponseobject.
Example Request
curl -X POST "https://your-worker.workers.dev/dispatch/tenant-a"POST /register
Record a customer name in KV for listing.
name string (required)
Example Request
curl -X POST "https://your-worker.workers.dev/register" \
-H "Content-Type: application/json" \
-d '{"name":"tenant-a"}'GET /customers
Returns { customers: string[] }.
Example Request
curl "https://your-worker.workers.dev/customers"Error Codes
400- Invalid name, response, body, or oversized script (INVALID_NAME,INVALID_RESPONSE,INVALID_BODY,SCRIPT_TOO_LARGE)404- Script not found (NOT_FOUND)502- Dispatch namespace failure (DISPATCH_ERROR)
Use Cases
- Learn Workers for Platforms dispatch namespace patterns
- Prototype multi-tenant routing with a KV fallback
- Register and list customers before wiring real scripts
- Teach safe demos that avoid eval while showing dispatch shape
Limitations
- KV path returns a stored JSON object - no dynamic JS execution
- Dispatch namespace (
demo-customers) may require Workers for Platforms - Script payload capped at 10,000 bytes
- No authentication on register/dispatch endpoints
Deployment
Configure KV and dispatch namespace
Create a KV namespace bound as SCRIPTS. For production dispatch, create a dispatch namespace matching demo-customers in wrangler.json. Local/KV fallback works without the paid feature.
Test your deployment
curl -X POST "https://your-worker.workers.dev/scripts" \
-H "Content-Type: application/json" \
-d '{"name":"tenant-a","response":{"hello":"world"}}'
curl -X POST "https://your-worker.workers.dev/dispatch/tenant-a"Local Development
cd apps/experiments/user-script-dispatcher
npm install
npm run devcurl -X POST "http://localhost:8787/scripts" \
-H "Content-Type: application/json" \
-d '{"name":"tenant-a","response":{"hello":"world"}}'
curl -X POST "http://localhost:8787/dispatch/tenant-a"Configuration
wrangler.json declares:
- KV namespace
SCRIPTS - Dispatch namespace
DISPATCHER→demo-customers
Cloudflare Features Used
- Workers - Edge compute runtime
- Workers KV - Script metadata and KV fallback responses
- Workers for Platforms - Optional
dispatch_namespacesbinding