Build sandboxed apps for secure code execution on Cloudflare Workers with the Sandbox SDK (lifecycle, commands, files, code interpreter, preview URLs). USE WHEN building AI code execution, code interpreters, CI/CD systems, interactive dev environments, or running untrusted code. Biases toward Cloudflare docs.
Build sandboxed apps for secure code execution on Cloudflare Workers with the Sandbox SDK (lifecycle, commands, files, code interpreter, preview URLs). USE WHEN building AI code execution, code interpreters, CI/CD systems, interactive dev environments, or running untrusted code. Biases toward Cloudflare docs.
cluster
cloudflare
version
1.0.0
Cloudflare Sandbox SDK
Build secure, isolated code execution environments on Cloudflare Workers.
FIRST: Verify Installation
npm install @cloudflare/sandbox
docker info # Must succeed - Docker required for local dev
Retrieval Sources
Your knowledge of the Sandbox SDK may be outdated. Prefer retrieval over pre-training for any Sandbox SDK task.
Base image (docker.io/cloudflare/sandbox:0.7.0) includes Python 3.11, Node.js 20, and common tools.
Add dependencies by extending the Dockerfile:
FROM docker.io/cloudflare/sandbox:0.7.0
# Python packages
RUN pip install requests beautifulsoup4
# Node packages (global)
RUN npm install -g typescript
# System packages
RUN apt-get update && apt-get install -y ffmpeg && rm -rf /var/lib/apt/lists/*
EXPOSE 8080 # Required for local dev port exposure
Keep images lean - affects cold start time.
Preview URLs (Port Exposure)
Expose HTTP services running in sandboxes:
const { url } = await sandbox.exposePort(8080);
// Returns preview URL for the service
Production requirement: Preview URLs need a custom domain with wildcard DNS (*.yourdomain.com). The .workers.dev domain does not support preview URL subdomains.