Skip to content

Commit 1a77c52

Browse files
committed
Clarify preview url usage
1 parent 9d62ab8 commit 1a77c52

File tree

12 files changed

+52
-105
lines changed

12 files changed

+52
-105
lines changed

src/content/docs/sandbox/concepts/index.mdx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ sidebar:
77

88
These pages explain how the Sandbox SDK works, why it's designed the way it is, and the concepts you need to understand to use it effectively.
99

10-
## Available concepts
11-
1210
- [Architecture](/sandbox/concepts/architecture/) - How the SDK is structured and why
1311
- [Sandbox lifecycle](/sandbox/concepts/sandboxes/) - Understanding sandbox states and behavior
1412
- [Container runtime](/sandbox/concepts/containers/) - How code executes in isolated containers

src/content/docs/sandbox/concepts/preview-urls.mdx

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Preview URLs work in local development without configuration. For production, yo
1212
Preview URLs provide public HTTPS access to services running inside sandboxes. When you expose a port, you get a unique URL that proxies requests to your service.
1313

1414
```typescript
15-
await sandbox.startProcess('python -m http.server 8000');
15+
await sandbox.startProcess("python -m http.server 8000");
1616
const exposed = await sandbox.exposePort(8000);
1717

1818
console.log(exposed.exposedAt);
@@ -29,7 +29,7 @@ console.log(exposed.exposedAt);
2929

3030
**Local development**: `http://localhost:8787/...`
3131

32-
URLs remain stable for a given sandbox ID and port. You can share, bookmark, or use them in webhooks.
32+
Preview URLs remain stable while a port is exposed and can be shared during that time. However, if you unexpose and re-expose a port, a new random token is generated and the URL changes. For persistent URLs, keep ports exposed for the duration you need them accessible.
3333

3434
## Request Routing
3535

@@ -39,14 +39,14 @@ You must call `proxyToSandbox()` first in your Worker's fetch handler to route p
3939
import { proxyToSandbox, getSandbox } from "@cloudflare/sandbox";
4040

4141
export default {
42-
async fetch(request, env) {
43-
// Handle preview URL routing first
44-
const proxyResponse = await proxyToSandbox(request, env);
45-
if (proxyResponse) return proxyResponse;
46-
47-
// Your application routes
48-
// ...
49-
}
42+
async fetch(request, env) {
43+
// Handle preview URL routing first
44+
const proxyResponse = await proxyToSandbox(request, env);
45+
if (proxyResponse) return proxyResponse;
46+
47+
// Your application routes
48+
// ...
49+
},
5050
};
5151
```
5252

@@ -57,11 +57,11 @@ Requests flow: Browser → Your Worker → Durable Object (sandbox) → Your Ser
5757
Expose multiple services simultaneously:
5858

5959
```typescript
60-
await sandbox.startProcess('node api.js'); // Port 3000
61-
await sandbox.startProcess('node admin.js'); // Port 3001
60+
await sandbox.startProcess("node api.js"); // Port 3000
61+
await sandbox.startProcess("node admin.js"); // Port 3001
6262

63-
const api = await sandbox.exposePort(3000, { name: 'api' });
64-
const admin = await sandbox.exposePort(3001, { name: 'admin' });
63+
const api = await sandbox.exposePort(3000, { name: "api" });
64+
const admin = await sandbox.exposePort(3001, { name: "admin" });
6565

6666
// Each gets its own URL:
6767
// https://3000-abc123.example.com
@@ -71,7 +71,6 @@ const admin = await sandbox.exposePort(3001, { name: 'admin' });
7171
## What Works
7272

7373
- HTTP/HTTPS requests
74-
- WebSocket (WSS) via HTTP upgrade
7574
- Server-Sent Events
7675
- All HTTP methods (GET, POST, PUT, DELETE, etc.)
7776
- Request and response headers
@@ -80,15 +79,25 @@ const admin = await sandbox.exposePort(3001, { name: 'admin' });
8079

8180
- Raw TCP/UDP connections
8281
- Custom protocols (must wrap in HTTP)
83-
- Ports 80/443 (use 1024+)
82+
- WebSocket connections
83+
- Ports outside range 1024-65535
84+
- Port 3000 (used internally by the SDK)
8485

8586
## Security
8687

8788
:::caution
88-
Preview URLs are publicly accessible. Anyone with the URL can access your service.
89+
Preview URLs are publicly accessible by default, but require a valid access token that is generated when you expose a port.
8990
:::
9091

91-
Add authentication in your service:
92+
**Built-in security**:
93+
94+
- **Token-based access** - Each exposed port gets a unique token in the URL (for example, `https://8080-sandbox-abc123token.example.com`)
95+
- **HTTPS in production** - All traffic is encrypted with automatic TLS
96+
- **Unpredictable URLs** - Tokens are randomly generated and difficult to guess
97+
98+
**Add application-level authentication**:
99+
100+
For additional security, implement authentication within your application:
92101

93102
```python
94103
from flask import Flask, request, abort
@@ -97,16 +106,14 @@ app = Flask(__name__)
97106

98107
@app.route('/data')
99108
def get_data():
100-
token = request.headers.get('Authorization')
101-
if token != 'Bearer secret-token':
109+
# Check for your own authentication token
110+
auth_token = request.headers.get('Authorization')
111+
if auth_token != 'Bearer your-secret-token':
102112
abort(401)
103113
return {'data': 'protected'}
104114
```
105115

106-
**Security features**:
107-
- All traffic is HTTPS (automatic TLS)
108-
- URLs use random sandbox IDs (hard to guess)
109-
- You control authentication in your service
116+
This adds a second layer of security on top of the URL token.
110117

111118
## Troubleshooting
112119

@@ -123,10 +130,10 @@ const ports = await sandbox.getExposedPorts();
123130

124131
// 3. Is service binding to 0.0.0.0 (not 127.0.0.1)?
125132
// Good:
126-
app.run(host='0.0.0.0', port=3000)
133+
app.run((host = "0.0.0.0"), (port = 3000));
127134

128135
// Bad (localhost only):
129-
app.run(host='127.0.0.1', port=3000)
136+
app.run((host = "127.0.0.1"), (port = 3000));
130137
```
131138

132139
### Production Errors
@@ -135,7 +142,7 @@ For custom domain issues, see [Production Deployment troubleshooting](/sandbox/g
135142

136143
### Local Development
137144

138-
:::caution[Local development only]
145+
:::caution[Local development limitation]
139146
When using `wrangler dev`, you must expose ports in your Dockerfile:
140147

141148
```dockerfile

src/content/docs/sandbox/configuration/index.mdx

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,91 +14,31 @@ Configure your Sandbox SDK deployment with Wrangler, customize container images,
1414
<LinkTitleCard
1515
title="Wrangler configuration"
1616
href="/sandbox/configuration/wrangler/"
17-
icon="document"
17+
icon="puzzle"
1818
>
19-
Configure Durable Objects bindings, container images, and Worker settings in wrangler.jsonc.
19+
Configure Durable Objects bindings, container images, and Worker settings in
20+
wrangler.jsonc.
2021
</LinkTitleCard>
2122

2223
<LinkTitleCard
2324
title="Dockerfile reference"
2425
href="/sandbox/configuration/dockerfile/"
25-
icon="wrench"
26+
icon="seti:docker"
2627
>
27-
Customize the sandbox container image with your own packages, tools, and configurations.
28+
Customize the sandbox container image with your own packages, tools, and
29+
configurations.
2830
</LinkTitleCard>
2931

3032
<LinkTitleCard
3133
title="Environment variables"
3234
href="/sandbox/configuration/environment-variables/"
33-
icon="gear"
35+
icon="seti:powershell"
3436
>
3537
Pass configuration and secrets to your sandboxes using environment variables.
3638
</LinkTitleCard>
3739

3840
</CardGrid>
3941

40-
## Quick reference
41-
42-
### Essential wrangler.jsonc settings
43-
44-
```jsonc
45-
{
46-
"name": "my-worker",
47-
"main": "src/index.ts",
48-
"compatibility_date": "2024-09-02",
49-
"compatibility_flags": ["nodejs_compat"],
50-
"durable_objects": {
51-
"bindings": [
52-
{
53-
"name": "Sandbox",
54-
"class_name": "Sandbox",
55-
"script_name": "@cloudflare/sandbox"
56-
}
57-
]
58-
},
59-
"containers": [
60-
{
61-
"binding": "CONTAINER",
62-
"image": "ghcr.io/cloudflare/sandbox-runtime:latest"
63-
}
64-
]
65-
}
66-
```
67-
68-
### Common Dockerfile customizations
69-
70-
```dockerfile
71-
FROM ghcr.io/cloudflare/sandbox-runtime:latest
72-
73-
# Install additional Python packages
74-
RUN pip install scikit-learn tensorflow pandas
75-
76-
# Install Node.js packages globally
77-
RUN npm install -g typescript ts-node
78-
79-
# Install system packages
80-
RUN apt-get update && apt-get install -y postgresql-client
81-
82-
# Add custom scripts
83-
COPY ./scripts /usr/local/bin/
84-
```
85-
86-
### Environment variables
87-
88-
```typescript
89-
// Pass to sandbox at creation
90-
const sandbox = getSandbox(env.Sandbox, 'my-sandbox');
91-
92-
// Configure environment for commands
93-
await sandbox.exec('node app.js', {
94-
env: {
95-
NODE_ENV: 'production',
96-
API_KEY: env.API_KEY,
97-
DATABASE_URL: env.DATABASE_URL
98-
}
99-
});
100-
```
101-
10242
## Related resources
10343

10444
- [Get Started guide](/sandbox/get-started/) - Initial setup walkthrough

src/content/docs/sandbox/guides/background-processes.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Run background processes
33
pcx_content_type: how-to
44
sidebar:
55
order: 3
6+
description: Start and manage long-running services and applications.
67
---
78

89
import { TypeScriptExample } from "~/components";

src/content/docs/sandbox/guides/code-execution.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Use code interpreter
33
pcx_content_type: how-to
44
sidebar:
55
order: 5
6+
description: Execute Python and JavaScript code with rich outputs.
67
---
78

89
import { TypeScriptExample } from "~/components";

src/content/docs/sandbox/guides/execute-commands.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Execute commands
33
pcx_content_type: how-to
44
sidebar:
55
order: 1
6+
description: Run commands with streaming output, error handling, and shell access.
67
---
78

89
import { TypeScriptExample } from "~/components";

src/content/docs/sandbox/guides/expose-services.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Expose services
33
pcx_content_type: how-to
44
sidebar:
55
order: 4
6+
description: Create preview URLs and expose ports for web services.
67
---
78

89
import { TypeScriptExample } from "~/components";

src/content/docs/sandbox/guides/git-workflows.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Work with Git
33
pcx_content_type: how-to
44
sidebar:
55
order: 6
6+
description: Clone repositories, manage branches, and automate Git operations.
67
---
78

89
import { TypeScriptExample } from "~/components";

src/content/docs/sandbox/guides/index.mdx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,11 @@ sidebar:
55
order: 4
66
---
77

8-
These guides show you how to solve specific problems and implement features with the Sandbox SDK. Each guide focuses on a particular task and provides practical, production-ready solutions.
8+
import { ResourcesBySelector } from "~/components";
99

10-
## Available guides
10+
These guides show you how to solve specific problems and implement features with the Sandbox SDK. Each guide focuses on a particular task and provides practical, production-ready solutions.
1111

12-
- [Execute commands](/sandbox/guides/execute-commands/) - Run commands with streaming output, error handling, and shell access
13-
- [Manage files](/sandbox/guides/manage-files/) - Read, write, organize, and synchronize files in the sandbox
14-
- [Run background processes](/sandbox/guides/background-processes/) - Start and manage long-running services and applications
15-
- [Expose services](/sandbox/guides/expose-services/) - Create preview URLs and expose ports for web services
16-
- [Use code interpreter](/sandbox/guides/code-execution/) - Execute Python and JavaScript code with rich outputs
17-
- [Work with Git](/sandbox/guides/git-workflows/) - Clone repositories, manage branches, and automate Git operations
18-
- [Stream output](/sandbox/guides/streaming-output/) - Handle real-time output from commands and processes
12+
<ResourcesBySelector directory="sandbox/guides/" types={["how-to"]} />
1913

2014
## Related resources
2115

src/content/docs/sandbox/guides/manage-files.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Manage files
33
pcx_content_type: how-to
44
sidebar:
55
order: 2
6+
description: Read, write, organize, and synchronize files in the sandbox.
67
---
78

89
import { TypeScriptExample } from "~/components";

0 commit comments

Comments
 (0)