Skip to content
AnswerWell geo Documentation

Research · 7 min read · Answerwell

Sandbox Preview URLs, Tunnels, Ports and Routing

How to expose sandbox web servers and APIs with preview URLs, tunnels, background processes, and Worker routing.

This page summarises Cloudflare Sandbox SDK documentation on reaching a web server or API that runs inside an isolated container sandbox. The Durable Object named Sandbox manages the container lifecycle, preview URLs, and routing. You typically start a background process with startProcess(), then publish it with either sandbox.tunnels (Cloudflare Tunnel inside the container) or exposePort() plus proxyToSandbox() (Worker-fronted preview URLs).

Preview URLs and tunnels: which public URL to use

Section titled “Preview URLs and tunnels: which public URL to use”

Cloudflare documents three main ways to open a sandbox service from outside the container: quick or named tunnels, preview URLs from exposePort(), and Worker path or hostname routing when the server stays private. Pick the path that matches stability, DNS control, and whether your Worker must sit in front of the request.

Approach Typical URL Custom domain / DNS Stable across container restart Worker in request path
Quick tunnel sandbox.tunnels.get(port) Random *.trycloudflare.com Not required No (new hostname each restart) No (edge → container via tunnel)
Named tunnel sandbox.tunnels.get(port, { name }) https://<name>.<your-zone> Zone + API token Yes No
Preview URL exposePort() + proxyToSandbox() https://{port}-{sandbox-id}-{token}.<hostname> Wildcard DNS for production Yes if you set a custom token Yes

For most public-URL use cases — development, .workers.dev deployments, and production traffic — sandbox.tunnels is the recommended option. Use named tunnels for stable hostnames on a zone you control, or quick tunnels for zero-config *.trycloudflare.com URLs. exposePort() is appropriate when you want the Worker itself to front the request (for example, to inject authentication or rewrite responses, or call sandbox-only RPC methods on the same hostname).

— Cloudflare, Ports API

We recently released support for Cloudflare Tunnel in the Sandbox SDK. This provides a robust API for exposing services running in your sandbox to the public internet. It fixes issues many were facing with local development and deployment to workers.dev domains. To migrate from exposePort() to tunnels, refer to the tunnels API documentation and the expose services guide.

— Cloudflare, Deprecating Sandbox SDK features

For a fast demo or .workers.dev deployment without DNS setup, start the service and call sandbox.tunnels.get(port). For production hostnames you control, prefer a named tunnel. Use preview URLs when you need the Worker to authenticate, rewrite, or validate tokens before traffic hits the container—patterns described in Expose services and Preview URLs concept.

sandbox.tunnels: quick and named public URLs

Section titled “sandbox.tunnels: quick and named public URLs”

The sandbox.tunnels namespace runs cloudflared inside the container and exposes a port on the public internet.

sandbox.tunnels.get(port): share a dev server on trycloudflare.com

Section titled “sandbox.tunnels.get(port): share a dev server on trycloudflare.com”

By default, sandbox.tunnels.get(port) creates a quick tunnel ↗︎ on a zero-config *.trycloudflare.com URL — no Cloudflare account, DNS record, or custom domain required. This is perfect for quick development and for .workers.dev deployments.

— Cloudflare, Share sandbox previews through Cloudflare Tunnel

The service must listen on 0.0.0.0:<port> inside the container (not only 127.0.0.1). After startProcess(), repeated get() calls for the same port return the same tunnel record until the container restarts.

Named tunnels sandbox.tunnels.get(port, { name }): stable hostname on your zone

Section titled “Named tunnels sandbox.tunnels.get(port, { name }): stable hostname on your zone”

Unlike quick tunnels, which generate a new random URL each time, a named tunnel produces a persistent URL that survives container restarts. This makes named tunnels suitable for production use cases where you want control over the tunnel and it’s origin.

— Cloudflare, Share sandbox previews through Cloudflare Tunnel

name must be a single DNS label (lowercase, 1–63 characters, no dots). The SDK creates a Cloudflare Tunnel, a proxied CNAME, and respawns cloudflared after container restarts while keeping the hostname. Calling sandbox.destroy() tears down tunnels and DNS records so dangling resources do not remain—see Tunnels API lifecycle.

Quick vs named limits (common conclusion): Quick tunnels trade URL stability for zero configuration; they are not positioned as production targets, and SSE over trycloudflare.com is buffered (WebSockets work). Named tunnels need account API access and consume tunnel/DNS quotas but give stable URLs suitable for webhooks and OAuth callbacks. If WARP or Zero Trust egress blocks tunnel handshakes, tunnels.get() may hang until timeout—documented under limitations in Tunnels API.

exposePort() and proxyToSandbox(): Worker-fronted preview URLs

Section titled “exposePort() and proxyToSandbox(): Worker-fronted preview URLs”

Preview URLs put a token in the hostname and route through your Worker.

exposePort() preview URL format and stable tokens

Section titled “exposePort() preview URL format and stable tokens”

Production: https://{port}-{sandbox-id}-{token}.yourdomain.com

— Cloudflare, Preview URLs concept

Pass hostname from the incoming request (your Worker’s domain). .workers.dev does not support wildcard subdomains, so production preview URLs on a public hostname need a custom domain with wildcard DNS. Optional token (1–16 characters, lowercase letters, digits, hyphens, underscores) keeps the same URL across container restarts; auto-generated tokens change when you unexpose and re-expose.

Port 3000 is reserved for the internal Bun server and cannot be exposed—use another port such as 8080 (Expose services).

proxyToSandbox(): route preview hostname traffic in the Worker

Section titled “proxyToSandbox(): route preview hostname traffic in the Worker”

Route incoming HTTP and WebSocket requests to the correct sandbox container. Call this at the top of your Worker’s fetch handler, before any application logic, so that it intercepts and forwards preview URL requests automatically.

— Cloudflare, Ports API

Request flow: browser → your Worker → Durable Object (sandbox) → service in the container. For custom auth, you can call validatePortToken() before or alongside routing—see Ports API.

Uppercase sandbox IDs and preview URL routing

Section titled “Uppercase sandbox IDs and preview URL routing”

Hostnames are case-insensitive, so preview URLs lower-case the sandbox id in the hostname. An uppercase sandbox id can route to a different Durable Object than the one you created.

The solution: Use normalizeId: true to lowercase IDs when creating sandboxes

— Cloudflare, Preview URLs concept

Enable normalizeId: true in getSandbox() when ids may contain uppercase letters, or use lowercase ids from the start (Sandbox options).

startProcess(): run the server before you expose a port

Section titled “startProcess(): run the server before you expose a port”

Foreground exec() waits for completion; background startProcess() returns immediately while the server keeps running (Container runtime).

waitForPort() and waitForLog(): avoid exposing before the port listens

Section titled “waitForPort() and waitForLog(): avoid exposing before the port listens”

Wait for readiness - Don’t expose ports immediately after starting processes

— Cloudflare, Expose services

Prefer process.waitForPort() (HTTP or TCP) or waitForLog() over fixed setTimeout delays when readiness matters (Commands API, Run background processes). Use listProcesses(), getExposedPorts(), unexposePort(), and killProcess() for cleanup; killProcess() terminates the process group to avoid orphans (Commands API).

For local development with wrangler dev, declare listened ports with EXPOSE in the sandbox Dockerfile; without it you may see connection refused for the container port (Expose services).

Internal hostnames: agent browser preview without a public URL

Section titled “Internal hostnames: agent browser preview without a public URL”

Agents that build sites can preview a dev server without exposing it to the public internet using internal hostnames in Browser Run.

Internal hostnames give the browser sites that your agent serves instead of the Internet, such as static assets in an R2 bucket, a folder in the agent’s workspace, a dev server in its sandbox, or another Worker. Only this browser can reach these hostnames.

— Cloudflare, Browser tool

Use browser.assignHost() to point a name such as preview.internal at a sandbox port; requests stay on Cloudflare and run the matching host’s fetch handler in your Durable Object. This complements public tunnels or preview URLs when only the agent’s browser should see the app.

Worker-only previews: paths and per-preview hostnames (SDK 1.x direction)

Section titled “Worker-only previews: paths and per-preview hostnames (SDK 1.x direction)”

Newer patterns keep the server private and forward through the Worker:

Sandbox architecture describes the Sandbox Durable Object managing containers, preview URLs, and state. Move preview URLs and Move tunnels explain how 0.12 token storage and cloudflared behaviour map to 1.0 Worker routing and getTcpPort() (Durable Object Container API).

Common mistakes when a preview URL does not load

Section titled “Common mistakes when a preview URL does not load”
  • Service bound to 127.0.0.1 only instead of 0.0.0.0 (Preview URLs concept).
  • Exposing before the process listens; missing EXPOSE in Dockerfile under wrangler dev.
  • Uppercase sandbox id without normalizeId: true.
  • Attempting exposePort(3000) (reserved port).
  • Second-level wildcard TLS on a Worker subdomain without an appropriate certificate (Configure preview URLs on a custom domain).
  1. Start a known process in the sandbox (for example python -m http.server 8080) and confirm it appears in listProcesses() as documented in Run background processes.

  2. Obtain a public URL with sandbox.tunnels.get(8080) or exposePort(8080, { hostname }) plus proxyToSandbox() at the start of the Worker fetch handler, following Tunnels API or Expose services.

  3. Open the returned URL in a browser, or run curl against it. For preview URLs with tokens, request the correct hostname and confirm a wrong token returns 404 as in Move preview URLs.

  4. If you use a named tunnel or custom-domain preview URLs, confirm the hostname and DNS/CNAME records in the Cloudflare dashboard match Tunnels API or Configure preview URLs on a custom domain.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page does not match the official docs or your setup, email yimasynthetic@gmail.com with the page URL and the exact sentence you think is wrong or unclear.