Skip to content

Commit dc4c637

Browse files
committed
Add Sandbox Agent SDK documentation
Add docs page for running Sandbox Agent inside E2B sandboxes, covering setup, headless usage with step-by-step guide, and full file preview.
1 parent bbd9bf4 commit dc4c637

2 files changed

Lines changed: 160 additions & 0 deletions

File tree

docs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"docs/agents/amp",
5757
"docs/agents/claude-code",
5858
"docs/agents/codex",
59+
"docs/agents/sandbox-agent-sdk",
5960
{
6061
"group": "OpenClaw",
6162
"icon": "https://mintlify.s3.us-west-1.amazonaws.com/e2b-openclaw-icon/images/icons/openclaw.svg",

docs/agents/sandbox-agent-sdk.mdx

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
---
2+
title: "Sandbox Agent SDK"
3+
description: "Run Sandbox Agent inside an E2B sandbox and control it from a local Node.js script."
4+
---
5+
6+
This guide shows how to run [Sandbox Agent](https://github.com/rivet-dev/sandbox-agent) inside an E2B sandbox and control it with the [Sandbox Agent SDK](https://sandboxagent.dev/docs/sdk-overview).
7+
8+
Sandbox Agent lets you run coding agents in sandboxes and control them from your own backend over HTTP.
9+
10+
- **Universal Agent API** - A single interface to manage Claude Code, Codex, OpenCode, Cursor, Amp, and Pi with full feature coverage across every supported agent.
11+
- **Universal Session Schema** - A standardized schema that normalizes all agent event formats into a consistent structure for storage and replay.
12+
- **Server or SDK Mode** - Run as a standalone HTTP server or integrate natively using the TypeScript SDK.
13+
14+
## Workflow overview
15+
16+
- A local Node.js script starts an E2B sandbox and boots the Sandbox Agent server inside it.
17+
- The script opens a session, sends a prompt to an agent (Claude in this example), and prints the response.
18+
- When the script exits, the sandbox is destroyed.
19+
20+
## Prerequisites
21+
22+
- `E2B_API_KEY` — get your key from the [E2B Dashboard](https://e2b.dev/dashboard?tab=keys)
23+
- At least one provider key: `OPENAI_API_KEY`, `CODEX_API_KEY`, or `ANTHROPIC_API_KEY`
24+
25+
## Setup
26+
27+
Install the [Sandbox Agent SDK](https://www.npmjs.com/package/sandbox-agent) and its dependencies. [dotenv](https://www.npmjs.com/package/dotenv) loads environment variables from your `.env` file, and [tsx](https://www.npmjs.com/package/tsx) lets you run TypeScript directly without a build step.
28+
29+
```bash
30+
npm install sandbox-agent dotenv tsx e2b
31+
```
32+
33+
Create a `.env` file with your API keys:
34+
35+
```text .env
36+
E2B_API_KEY=e2b_***
37+
ANTHROPIC_API_KEY=your_anthropic_api_key
38+
# OPENAI_API_KEY=your_openai_api_key
39+
```
40+
41+
## Run headless
42+
43+
Create an `index.ts` file and follow the steps below to build a script that boots an agent inside an E2B sandbox, sends a prompt, and streams the response.
44+
45+
<Steps>
46+
<Step title="Forward provider credentials">
47+
Load your environment variables and collect the provider API keys that need to be forwarded into the sandbox so the agent can authenticate with the LLM provider.
48+
49+
```ts
50+
import "dotenv/config";
51+
52+
const envs: Record<string, string> = {};
53+
if (process.env.ANTHROPIC_API_KEY) envs.ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
54+
if (process.env.OPENAI_API_KEY) envs.OPENAI_API_KEY = process.env.OPENAI_API_KEY;
55+
```
56+
</Step>
57+
<Step title="Start Sandbox Agent">
58+
Initialize the SDK with the E2B provider. This creates a sandbox and boots the Sandbox Agent server inside it. The [inspector UI](https://sandboxagent.dev/docs/sdk-overview) lets you view sessions, events, and agent state in real time.
59+
60+
```ts
61+
import { SandboxAgent } from 'sandbox-agent';
62+
import { e2b } from 'sandbox-agent/e2b';
63+
64+
const sdk = await SandboxAgent.start({
65+
sandbox: e2b({
66+
create: { envs },
67+
})
68+
});
69+
70+
console.log(`Inspector URL: ${sdk.inspectorUrl}`);
71+
```
72+
</Step>
73+
<Step title="Send a prompt and stream events">
74+
Create a session targeting a specific agent (Claude in this example), send a prompt, and stream the resulting events. Read more about [Sessions and Events](https://sandboxagent.dev/docs/agent-sessions).
75+
76+
```ts
77+
const session = await sdk.createSession({ agent: 'claude' });
78+
const off = session.onEvent((event) => {
79+
console.log(`[event] from ${event.sender}`, event.payload);
80+
});
81+
await session.prompt([
82+
{ type: 'text', text: 'Summarize this repository' }
83+
]);
84+
off();
85+
```
86+
87+
To use a different agent, update the `agent` value in `createSession` (for example, `codex`) and ensure the corresponding provider credentials are available in the environment.
88+
</Step>
89+
<Step title="Destroy the sandbox">
90+
Once you're done, destroy the sandbox to free resources.
91+
92+
```ts
93+
await sdk.destroySandbox();
94+
```
95+
</Step>
96+
<Step title="Run the script">
97+
Run the completed `index.ts` file:
98+
99+
```bash
100+
node --import tsx index.ts
101+
```
102+
</Step>
103+
</Steps>
104+
105+
### Full file preview
106+
107+
<CodeGroup>
108+
```ts index.ts expandable
109+
import "dotenv/config";
110+
import { SandboxAgent } from "sandbox-agent";
111+
import { e2b } from "sandbox-agent/e2b";
112+
113+
// Forward provider credentials into the sandbox
114+
const envs: Record<string, string> = {};
115+
if (process.env.ANTHROPIC_API_KEY) envs.ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
116+
if (process.env.OPENAI_API_KEY) envs.OPENAI_API_KEY = process.env.OPENAI_API_KEY;
117+
118+
// Start Sandbox Agent with the E2B provider
119+
const sdk = await SandboxAgent.start({
120+
sandbox: e2b({
121+
create: { envs },
122+
}),
123+
});
124+
125+
console.log(`Inspector URL: ${sdk.inspectorUrl}`);
126+
127+
// Create a session targeting Claude and stream events
128+
const session = await sdk.createSession({ agent: "claude" });
129+
const off = session.onEvent((event) => {
130+
console.log(`[event] from ${event.sender}`, event.payload);
131+
});
132+
133+
await session.prompt([
134+
{ type: "text", text: "Summarize this repository" },
135+
]);
136+
off();
137+
138+
// Clean up
139+
await sdk.destroySandbox();
140+
```
141+
</CodeGroup>
142+
143+
## Full example
144+
145+
See the complete project in the [e2b-cookbook repo](https://github.com/rivet-dev/e2b-cookbook/tree/main/examples/sandbox-agent-sdk).
146+
147+
## Related guides
148+
149+
<CardGroup cols={3}>
150+
<Card title="Sandbox persistence" icon="clock" href="/docs/sandbox/persistence">
151+
Auto-pause, resume, and manage sandbox lifecycle
152+
</Card>
153+
<Card title="Git integration" icon="code-branch" href="/docs/sandbox/git-integration">
154+
Clone repos, manage branches, and push changes
155+
</Card>
156+
<Card title="SSH access" icon="terminal" href="/docs/sandbox/ssh-access">
157+
Connect to the sandbox via SSH for interactive sessions
158+
</Card>
159+
</CardGroup>

0 commit comments

Comments
 (0)