Dynamic Workers Starter

Last updated Oct 18, 2026 A starter template ↗︎ for deploying a Worker that loads and runs Dynamic Workers.

Deploy to Workers

What it does

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.

Configuration

Add a worker_loaders binding to your Wrangler file:

{
    "worker_loaders": [
        {
            "binding": "LOADER"
        }
    ]
}
[[worker_loaders]]
binding = "LOADER"

Loading and executing a Dynamic Worker

In this example:

  • env.LOADER.load() creates a one-off dynamic isolate
  • globalOutbound: null blocks 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;

Running locally

Miniflare is not a hardened sandbox

Do not use Miniflare to run untrusted or potentially malicious Worker code directly on your machine. Run the entire development process inside an appropriate secure sandbox, such as a virtual machine.

Miniflare starts a local workerd ↗︎ process. This warning applies to default local execution with wrangler dev, vite dev, vite preview, direct Miniflare use, and Miniflare-based testing tools. Setting remote: true on a binding changes which resource your code accesses, but your Worker code still executes locally. By contrast, legacy wrangler dev --remote executes your Worker code on Cloudflare.

Miniflare is not a security boundary for untrusted code. Although workerd attempts to restrict Worker code to configured resources, an implementation bug could allow code to escape those restrictions. The hosted Workers platform applies additional layers of defense in depth that Miniflare does not provide locally.

npm install
npm run dev

Was this helpful?

YesNo

On this page

Dynamic Workers Starter - 图2Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/dynamic-workers/examples/dynamic-workers-starter/#page","headline":"Dynamic Workers Starter","description":"A starter template for deploying a Worker that loads and runs Dynamic Workers.","url":"https://developers.cloudflare.com/dynamic-workers/examples/dynamic-workers-starter/","inLanguage":"en","image":"https://developers.cloudflare.com/dynamic-workers/examples/dynamic-workers-starter/og.png?v=9bfa1c960456467d","dateModified":"2026-10-18","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["JavaScript","TypeScript"]}


description: Write workflow logic in JavaScript and watch every step execute with live console.log streaming. title: Dynamic Workflows Playground image: https://developers.cloudflare.com/dynamic-workers/examples/dynamic-workflows-playground/og.png?v=a8d0d2021fa2c8a7


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/dynamic-workers/llms.txt
Use this file to discover all available pages before exploring further.