Local development
To test your Dispatch Worker, user Worker and Outbound Worker before deploying to production, you can use Wrangler for development and testing.
npm create cloudflare@latest -- customer-worker-1pnpm create cloudflare@latest customer-worker-1yarn create cloudflare customer-worker-1For setup, select the following options:
- For What would you like to start with?, choose
Hello World Starter. - For Which template would you like to use?, choose
Worker only. - 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).
Then, move into the newly created directory:
cd customer-worker-1Update the src/index.js file for customer-worker-1:
export default { async fetch(request) { // make a subrequest to the internet const response = await fetch("https://example.com"); return new Response( `user worker got "${await response.text()}" from fetch`, ); },};Update the Wrangler file for customer-worker-1 and add the dispatch namespace:
{ "dispatch_namespace": "my-namespace"}# ... other content above ...
dispatch_namespace = "my-namespace"npm create cloudflare@latest -- dispatch-workerpnpm create cloudflare@latest dispatch-workeryarn create cloudflare dispatch-workerFor setup, select the following options:
- For What would you like to start with?, choose
Hello World Starter. - For Which template would you like to use?, choose
Worker only. - 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).
Then, move into the newly created directory:
cd dispatch-workerUpdate the src/index.js file for dispatch-worker:
export default { async fetch(request, env) { // get the user Worker, specifying parameters that the Outbound Worker will see when it intercepts a user worker's subrequest const customerScript = env.DISPATCH_NAMESPACE.get( "customer-worker-1", {}, { outbound: { paramCustomerName: "customer-1", }, }, ); // invoke user Worker return await customerScript.fetch(request); },};Update the Wrangler file for dispatch-worker and add the dispatch namespace binding:
{ "dispatch_namespaces": [ { "binding": "DISPATCH_NAMESPACE", "namespace": "my-namespace", "outbound": { "service": "outbound-worker", "parameters": [ "paramCustomerName" ] } } ]}# ... other content above ...
[[dispatch_namespaces]]binding = "DISPATCH_NAMESPACE"namespace = "my-namespace"outbound = { service = "outbound-worker", parameters = ["paramCustomerName"] }npm create cloudflare@latest -- outbound-workerpnpm create cloudflare@latest outbound-workeryarn create cloudflare outbound-workerFor setup, select the following options:
- For What would you like to start with?, choose
Hello World Starter. - For Which template would you like to use?, choose
Worker only. - 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).
Then, move into the newly created directory:
cd outbound-workerUpdate the src/index.js file for outbound-worker:
export default { async fetch(request, env) { const { paramCustomerName } = env; // use the parameters passed by the dispatcher to know what this user this request is for // and return custom content back to the user worker return new Response( `intercepted a request for ${paramCustomerName} by the outbound`, ); },};In separate terminals, start a local dev session for each of your Workers.
For your dispatcher Worker:
cd dispatch-workernpx wrangler@dispatch-namespaces-dev dev --port 8600For your outbound Worker:
cd outbound-workernpx wrangler@dispatch-namespaces-dev dev --port 8601And for your user Worker:
cd customer-worker-1npx wrangler@dispatch-namespaces-dev dev --port 8602Send a request to your dispatcher Worker:
curl http://localhost:8600# -> user worker got "intercepted a request for customer-1 by the outbound" from fetchWas this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark