Skip to content

Sandbox bridge

Last reviewed: 4 days ago

The sandbox bridge is a reference-implementation Cloudflare Worker that exposes the Sandbox SDK as an HTTP API. Any HTTP client — Python script, Node.js service, CI pipeline — can create and control sandboxes without writing a Worker.

Why use the bridge

The Sandbox SDK is designed for use within Cloudflare Workers. If your application runs outside of the Workers ecosystem, it cannot interact with sandboxes directly.

The bridge exposes the Sandbox SDK as a standard HTTP API so you can create and control sandboxes from any language or platform.

Key Sandbox SDK methods map to individual HTTP endpoints. The bridge adds authentication, input validation, workspace path containment, and an optional warm pool for instant container boot.

Deploy

Deploy the bridge Worker to your Cloudflare account:

Deploy to Cloudflare

The button deploys the Worker and generates a SANDBOX_API_KEY secret for authentication. When deployment finishes, note your Worker URL and API key — every example on this page uses them.

Manual deployment

If you prefer to deploy step by step, scaffold the project and deploy manually.

Prerequisites:

Steps:

  1. Scaffold the bridge project:

    Terminal window
    npm create cloudflare sandbox-bridge --template=cloudflare/sandbox-sdk/bridge/worker
    cd sandbox-bridge
  2. Authenticate with Cloudflare:

    Terminal window
    npx wrangler login
  3. Set the API key secret. Choose any strong token value — clients must send this as a Bearer token:

    Terminal window
    openssl rand -hex 32 | tee /dev/stderr | npx wrangler secret put SANDBOX_API_KEY

    The key is printed to your terminal and piped to Wrangler. Save it — you will need it to authenticate API requests.

  4. Deploy the Worker:

    Terminal window
    npx wrangler deploy
  5. Verify the deployment:

    Terminal window
    curl https://cloudflare-sandbox-bridge.<your-subdomain>.workers.dev/health

    You should see {"ok":true}.

Container image

The bridge Dockerfile extends the cloudflare/sandbox base image and pre-installs common agent tooling:

  • Languages: Python 3.13, Node.js, Bun
  • Tools: git, ripgrep, curl, wget, jq, tar, sed, gawk, procps

Customize the Dockerfile to add languages, system packages, or tools your workloads need.

Usage

All examples assume the following environment variables are set:

Terminal window
export SANDBOX_API_URL=https://cloudflare-sandbox-bridge.<your-subdomain>.workers.dev
export SANDBOX_API_KEY=<your-token>

Create a sandbox and run a command

Terminal window
# Create a sandbox
SANDBOX_ID=$(curl -s -X POST "$SANDBOX_API_URL/v1/sandbox" \
-H "Authorization: Bearer $SANDBOX_API_KEY" | jq -r '.id')
echo "Sandbox ID: $SANDBOX_ID"
# Run a command
curl -s -X POST "$SANDBOX_API_URL/v1/sandbox/$SANDBOX_ID/exec" \
-H "Authorization: Bearer $SANDBOX_API_KEY" \
-H "Content-Type: application/json" \
-d '{"argv": ["sh", "-lc", "echo hello from the sandbox"], "timeout_ms": 10000}'
# Destroy the sandbox when done
curl -s -X DELETE "$SANDBOX_API_URL/v1/sandbox/$SANDBOX_ID" \
-H "Authorization: Bearer $SANDBOX_API_KEY"

Write and read files

Terminal window
# Write a file
curl -s -X PUT "$SANDBOX_API_URL/v1/sandbox/$SANDBOX_ID/file/workspace/hello.py" \
-H "Authorization: Bearer $SANDBOX_API_KEY" \
--data-binary 'print("hello world")'
# Read a file
curl -s "$SANDBOX_API_URL/v1/sandbox/$SANDBOX_ID/file/workspace/hello.py" \
-H "Authorization: Bearer $SANDBOX_API_KEY"

Keep the bridge updated

The bulk of the bridge logic is in the @cloudflare/sandbox package. To pull in the latest improvements:

  1. Update the SDK dependency:

    Terminal window
    npm update @cloudflare/sandbox
  2. Redeploy:

    Terminal window
    npx wrangler deploy

Check the sandbox-sdk releases for changes to the Dockerfile or bridge configuration that may require manual updates.

Source code and examples

The bridge source code and examples are available on GitHub: