Skip to content
65 changes: 27 additions & 38 deletions src/content/docs/sandbox/api/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,57 @@ The Sandbox SDK provides a comprehensive API for executing code, managing files,

<CardGrid>

<LinkTitleCard
title="Lifecycle"
href="/sandbox/api/lifecycle/"
icon="rocket"
>
Create and manage sandbox containers. Get sandbox instances, configure options, and clean up resources.
<LinkTitleCard title="Lifecycle" href="/sandbox/api/lifecycle/" icon="rocket">
Create and manage sandbox containers. Get sandbox instances, configure
options, and clean up resources.
</LinkTitleCard>

<LinkTitleCard
title="Commands"
href="/sandbox/api/commands/"
icon="seti:shell"
>
Execute commands and stream output. Run scripts, manage background processes, and capture execution results.
<LinkTitleCard title="Commands" href="/sandbox/api/commands/" icon="seti:shell">
Execute commands and stream output. Run scripts, manage background processes,
and capture execution results.
</LinkTitleCard>

<LinkTitleCard
title="Files"
href="/sandbox/api/files/"
icon="document"
>
Read, write, and manage files in the sandbox filesystem. Includes directory operations and file metadata.
<LinkTitleCard title="Files" href="/sandbox/api/files/" icon="document">
Read, write, and manage files in the sandbox filesystem. Includes directory
operations and file metadata.
</LinkTitleCard>

<LinkTitleCard
title="File Watching"
href="/sandbox/api/file-watching/"
icon="seti:eye"
>
Monitor real-time filesystem changes using native inotify. Build development tools, hot-reload systems, and responsive file processing.
Monitor real-time filesystem changes using native inotify. Build development
tools, hot-reload systems, and responsive file processing.
</LinkTitleCard>

<LinkTitleCard
title="Code Interpreter"
href="/sandbox/api/interpreter/"
icon="laptop"
>
Execute Python and JavaScript code with rich outputs including charts, tables, and formatted data.
Execute Python and JavaScript code with rich outputs including charts, tables,
and formatted data.
</LinkTitleCard>

<LinkTitleCard
title="Ports"
href="/sandbox/api/ports/"
icon="download"
>
Expose services running in the sandbox via preview URLs. Access web servers and APIs from the internet.
<LinkTitleCard title="Ports" href="/sandbox/api/ports/" icon="download">
Expose services running in the sandbox via preview URLs. Access web servers
and APIs from the internet.
</LinkTitleCard>

<LinkTitleCard
title="Storage"
href="/sandbox/api/storage/"
icon="database"
>
Mount S3-compatible buckets (R2, S3, GCS) as local filesystems for persistent data storage across sandbox lifecycles.
<LinkTitleCard title="Storage" href="/sandbox/api/storage/" icon="database">
Mount S3-compatible buckets (R2, S3, GCS) as local filesystems for persistent
data storage across sandbox lifecycles.
</LinkTitleCard>

<LinkTitleCard
title="Sessions"
href="/sandbox/api/sessions/"
icon="seti:plan"
>
Create isolated execution contexts within a sandbox. Each session maintains its own shell state, environment variables, and working directory.
<LinkTitleCard title="Sessions" href="/sandbox/api/sessions/" icon="seti:plan">
Create isolated execution contexts within a sandbox. Each session maintains
its own shell state, environment variables, and working directory.
</LinkTitleCard>

<LinkTitleCard title="Terminal" href="/sandbox/api/terminal/" icon="terminal">
Connect browser-based terminal UIs to sandbox shells via WebSocket, with the
xterm.js SandboxAddon for automatic reconnection and resize handling.
</LinkTitleCard>

</CardGrid>
23 changes: 15 additions & 8 deletions src/content/docs/sandbox/api/sessions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const session = await sandbox.createSession(options?: SessionOptions): Promise<E
```

**Parameters**:

- `options` (optional):
- `id` - Custom session ID (auto-generated if not provided)
- `env` - Environment variables for this session: `Record<string, string | undefined>`
Expand All @@ -32,7 +33,7 @@ const session = await sandbox.createSession(options?: SessionOptions): Promise<E
**Returns**: `Promise<ExecutionSession>` with all sandbox methods bound to this session

<TypeScriptExample>
```
```ts
// Multiple isolated environments
const prodSession = await sandbox.createSession({
id: 'prod',
Expand All @@ -42,10 +43,10 @@ const prodSession = await sandbox.createSession({

const testSession = await sandbox.createSession({
id: 'test',
env: {
NODE_ENV: 'test',
env: {
NODE_ENV: 'test',
API_URL: 'http://localhost:3000',
DEBUG_MODE: undefined // Skipped, not set in this session
DEBUG_MODE: undefined // Skipped, not set in this session
},
cwd: '/workspace/test'
});
Expand All @@ -67,12 +68,13 @@ const session = await sandbox.getSession(sessionId: string): Promise<ExecutionSe
```

**Parameters**:

- `sessionId` - ID of an existing session

**Returns**: `Promise<ExecutionSession>` bound to the specified session

<TypeScriptExample>
```
```ts
// First request - create session
const session = await sandbox.createSession({ id: 'user-123' });
await session.exec('git clone https://github.com/user/repo.git');
Expand All @@ -95,15 +97,17 @@ const result = await sandbox.deleteSession(sessionId: string): Promise<SessionDe
```

**Parameters**:

- `sessionId` - ID of the session to delete (cannot be `"default"`)

**Returns**: `Promise<SessionDeleteResult>` containing:

- `success` - Whether deletion succeeded
- `sessionId` - ID of the deleted session
- `timestamp` - Deletion timestamp

<TypeScriptExample>
```
```ts
// Create a temporary session for a specific task
const tempSession = await sandbox.createSession({ id: 'temp-task' });

Expand All @@ -113,6 +117,7 @@ try {
// Clean up the session when done
await sandbox.deleteSession('temp-task');
}

```
</TypeScriptExample>

Expand All @@ -131,6 +136,7 @@ await sandbox.setEnvVars(envVars: Record<string, string | undefined>): Promise<v
```

**Parameters**:

- `envVars` - Key-value pairs of environment variables to set or unset
- `string` values: Set the environment variable
- `undefined` or `null` values: Unset the environment variable
Expand All @@ -140,15 +146,15 @@ Call `setEnvVars()` **before** any other sandbox operations to ensure environmen
:::

<TypeScriptExample>
```
```ts
const sandbox = getSandbox(env.Sandbox, 'user-123');

// Set environment variables first
await sandbox.setEnvVars({
API_KEY: env.OPENAI_API_KEY,
DATABASE_URL: env.DATABASE_URL,
NODE_ENV: 'production',
OLD_TOKEN: undefined // Unsets OLD_TOKEN if previously set
OLD_TOKEN: undefined // Unsets OLD_TOKEN if previously set
});

// Now commands can access these variables
Expand All @@ -168,6 +174,7 @@ The `ExecutionSession` object has all sandbox methods bound to the specific sess
| **Processes** | [`startProcess()`](/sandbox/api/commands/#startprocess), [`listProcesses()`](/sandbox/api/commands/#listprocesses), [`killProcess()`](/sandbox/api/commands/#killprocess), [`killAllProcesses()`](/sandbox/api/commands/#killallprocesses), [`getProcessLogs()`](/sandbox/api/commands/#getprocesslogs), [`streamProcessLogs()`](/sandbox/api/commands/#streamprocesslogs) |
| **Files** | [`writeFile()`](/sandbox/api/files/#writefile), [`readFile()`](/sandbox/api/files/#readfile), [`mkdir()`](/sandbox/api/files/#mkdir), [`deleteFile()`](/sandbox/api/files/#deletefile), [`renameFile()`](/sandbox/api/files/#renamefile), [`moveFile()`](/sandbox/api/files/#movefile), [`gitCheckout()`](/sandbox/api/files/#gitcheckout) |
| **Environment** | [`setEnvVars()`](/sandbox/api/sessions/#setenvvars) |
| **Terminal** | [`terminal()`](/sandbox/api/terminal/#terminal) |
| **Code Interpreter** | [`createCodeContext()`](/sandbox/api/interpreter/#createcodecontext), [`runCode()`](/sandbox/api/interpreter/#runcode), [`listCodeContexts()`](/sandbox/api/interpreter/#listcodecontexts), [`deleteCodeContext()`](/sandbox/api/interpreter/#deletecodecontext) |

## Related resources
Expand Down
Loading
Loading