# Dynamic Worker Runner (/docs/experiments/dynamic-worker-runner)



Run untrusted JavaScript as an **ES module Worker** using the **Worker Loader** API. Loaded workers run with `globalOutbound: null` so they cannot make network requests.

## API Reference [#api-reference]

### POST /run [#post-run]

Execute a Worker module string and return its HTTP response status and body.

**`code`** `string` (required)

Full ES module that exports `default { fetch }`. Max length 10,000 characters.

#### Example Request [#example-request]

```bash
curl -X POST "https://your-worker.workers.dev/run" \
  -H "Content-Type: application/json" \
  -d '{"code":"export default { async fetch() { return Response.json({ hello: \"world\" }); } }"}'
```

#### Success Response [#success-response]

```json
{
  "status": 200,
  "body": { "hello": "world" }
}
```

#### Error Codes [#error-codes]

* `400` - Missing, empty, or too-long code (`INVALID_CODE`)
* `502` - Loader or isolate failure (`RUN_ERROR`)

## Use Cases [#use-cases]

* Learn Dynamic Workers / Worker Loader patterns safely
* Sandbox plugin or customer code without outbound network
* Prototype multi-tenant execution before Workers for Platforms
* Teach isolate isolation and `globalOutbound` restrictions

## Limitations [#limitations]

* Code must be a complete ES module with `default { fetch }`
* Max code length 10,000 characters
* No outbound network from the loaded worker
* Dynamic Workers / Worker Loader may require account entitlements

## Deployment [#deployment]

<Steps>
  <Step>
    ### Click the deploy button [#click-the-deploy-button]

    [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/shrinathsnayak/cloudflare-experiments/tree/main/apps/experiments/dynamic-worker-runner)
  </Step>

  <Step>
    ### Deploy [#deploy]

    Ensure your account supports Worker Loaders. `wrangler.json` binds `LOADER` via `worker_loaders`.
  </Step>

  <Step>
    ### Test your deployment [#test-your-deployment]

    ```bash
    curl -X POST "https://your-worker.workers.dev/run" \
      -H "Content-Type: application/json" \
      -d '{"code":"export default { async fetch() { return Response.json({ ok: true }); } }"}'
    ```
  </Step>
</Steps>

## Local Development [#local-development]

```bash
cd apps/experiments/dynamic-worker-runner
npm install
npm run dev
```

```bash
curl -X POST "http://localhost:8787/run" \
  -H "Content-Type: application/json" \
  -d '{"code":"export default { async fetch() { return Response.json({ hello: \"world\" }); } }"}'
```

## Configuration [#configuration]

`wrangler.json` declares:

* **Worker Loader binding** `LOADER`

## Cloudflare Features Used [#cloudflare-features-used]

* **[Workers](https://developers.cloudflare.com/workers/)** - Edge compute runtime
* **[Dynamic Workers / Worker Loaders](https://developers.cloudflare.com/workers/runtime-apis/bindings/worker-loader/)** - Load and run module code at request time
