A starter template ↗ for deploying a Worker that loads and runs Dynamic Workers.
This template demonstrates how to use the Worker Loader API to execute code at runtime. The host Worker exposes an /api/run endpoint that accepts code from the frontend, loads it into a sandboxed Dynamic Worker, and returns the result.
Use this pattern for AI agents that need to execute a snippet of code to complete an action.
Add a worker_loaders binding to your Wrangler file:
{
"worker_loaders": [
{
"binding": "LOADER"
}
]
}[[worker_loaders]]
binding = "LOADER"In this example:
env.LOADER.load()creates a one-off dynamic isolateglobalOutbound: nullblocks all outbound network access from the Dynamic Worker
export default {
async fetch(request, env) {
const { code } = await request.json();
const worker = env.LOADER.load({
compatibilityDate: "2026-05-01",
mainModule: "worker.js",
modules: {
"worker.js": code,
},
// Block all outbound network access
globalOutbound: null,
});
const result = await worker.getEntrypoint().fetch(request);
return result;
},
};export default {
async fetch(request, env): Promise<Response> {
const { code } = await request.json();
const worker = env.LOADER.load({
compatibilityDate: "2026-05-01",
mainModule: "worker.js",
modules: {
"worker.js": code,
},
// Block all outbound network access
globalOutbound: null,
});
const result = await worker.getEntrypoint().fetch(request);
return result;
},
} satisfies ExportedHandler;npm install
npm run dev