Agents - Share sandbox previews through Cloudflare Tunnel
Key Points
- Zero-config *.trycloudflare.com previews
- Persistent named tunnels with CNAME support
- Tunnels and DNS torn down on sandbox.destroy()
Summary
Cloudflare Sandbox now exposes containerized services through Cloudflare Tunnel (using cloudflared) so you can share public preview URLs without configuring exposePort() or a custom domain. By default sandbox.tunnels.get(port) returns a zero‑config quick tunnel on a *.trycloudflare.com URL for fast dev previews; use a named tunnel (sandbox.tunnels.get(port, { name })) to bind a persistent hostname on your zone backed by a Cloudflare Tunnel and CNAME.
Key Points
- Zero‑config quick tunnels: sandbox.tunnels.get(port) → random-words.trycloudflare.com; no Cloudflare account or DNS changes required.
- Named tunnels: sandbox.tunnels.get(port, { name }) creates a persistent hostname (name.your-zone) with a Cloudflare Tunnel + CNAME; survives container restarts.
- Cleanup: calling sandbox.destroy() tears down the Cloudflare Tunnel and removes the associated DNS record to avoid dangling resources.
- Use cases: quick developer previews, sharing running services, and .workers.dev deployments.
Quick examples
import { getSandbox } from "@cloudflare/sandbox";
const sandbox = getSandbox(env.Sandbox, "my-sandbox");
await sandbox.startProcess("python -m http.server 8080");
const tunnel = await sandbox.tunnels.get(8080);
console.log(tunnel.url);
// → https://random-words-here.trycloudflare.com
// Named tunnel (persistent hostname)
const named = await sandbox.tunnels.get(8080, { name: "my-app-preview" });
console.log(named.url);
// → https://my-app-preview.example.com
Upgrade
Install the latest SDK to get tunnels:
npm i @cloudflare/sandbox@latest
Notes
- Quick tunnels are ideal for ephemeral previews; use named tunnels for stable preview domains.
- sandbox.destroy() removes both the tunnel and DNS record automatically.