Skip to content

Commit 8cce7d3

Browse files
authored
Add sandbox snapshots documentation (#115)
1 parent 20b0cdf commit 8cce7d3

File tree

7 files changed

+188
-4
lines changed

7 files changed

+188
-4
lines changed

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node 24.11.0

docs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"docs/sandbox/lifecycle-events-api",
100100
"docs/sandbox/lifecycle-events-webhooks",
101101
"docs/sandbox/persistence",
102+
"docs/sandbox/snapshots",
102103
"docs/sandbox/git-integration",
103104
"docs/sandbox/metrics",
104105
"docs/sandbox/metadata",

docs/sandbox/lifecycle-events-api.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Monitor sandbox lifecycle events"
33
sidebarTitle: Lifecycle events API
44
---
55

6-
The lifecycle API provides RESTful endpoints to request the latest sandbox lifecycle events. This allows you to track when sandboxes are created, paused, resumed, updated, or killed, along with metadata.
6+
The lifecycle API provides RESTful endpoints to request the latest sandbox lifecycle events. This allows you to track when sandboxes are created, paused, resumed, updated, snapshotted, or killed, along with metadata.
77
All requests require authentication using your team [API key](/docs/api-key#where-to-find-api-key).
88

99
Query Parameters:

docs/sandbox/lifecycle-events-webhooks.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,5 @@ The following event types can be subscribed to via webhooks, they are used as th
346346
- `sandbox.lifecycle.updated` - Sandbox configuration updates
347347
- `sandbox.lifecycle.paused` - Sandbox pausing
348348
- `sandbox.lifecycle.resumed` - Sandbox resuming
349+
- `sandbox.lifecycle.checkpointed` - Sandbox [snapshot](/docs/sandbox/snapshots) created
349350

docs/sandbox/persistence.mdx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,28 @@ This includes not only state of the sandbox's filesystem but also the sandbox's
1717

1818
Understanding how sandboxes transition between different states is crucial for managing their lifecycle effectively. Here's a diagram showing the possible state transitions:
1919

20-
<Frame>
21-
<img src="/images/diagram.png" />
22-
</Frame>
20+
```mermaid actions={false}
21+
flowchart TD
22+
start(( )) -->|Sandbox.create| Running
23+
24+
Running["<b>Running</b><br/>• Active execution<br/>• Consumes resources"]
25+
Paused["<b>Paused</b><br/>• Preserves memory and files<br/>• Cannot execute code"]
26+
Snapshotting["<b>Snapshotting</b><br/>• Creates persistent snapshot<br/>• Briefly pauses execution"]
27+
Killed["<b>Killed</b><br/>• Resources released<br/>• Cannot be resumed"]
28+
29+
Running -->|pause| Paused
30+
Running -->|createSnapshot| Snapshotting
31+
Paused -->|connect| Running
32+
Snapshotting -->|snapshot complete| Running
33+
Running -->|kill| Killed
34+
Paused -->|kill| Killed
35+
```
2336

2437
### State descriptions
2538

2639
- **Running**: The sandbox is actively running and can execute code. This is the initial state after creation.
2740
- **Paused**: The sandbox execution is suspended but its state is preserved.
41+
- **Snapshotting**: The sandbox is briefly paused while a persistent snapshot is being created. It automatically returns to Running. See [Snapshots](/docs/sandbox/snapshots).
2842
- **Killed**: The sandbox is terminated and all resources are released. This is a terminal state.
2943

3044
### Changing sandbox's state

docs/sandbox/snapshots.mdx

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
---
2+
title: "Sandbox snapshots"
3+
sidebarTitle: Snapshots
4+
---
5+
6+
Snapshots let you create a persistent point-in-time capture of a running sandbox, including both its filesystem and memory state.
7+
You can then use a snapshot to spawn new sandboxes that start from the exact same state.
8+
9+
The original sandbox continues running after the snapshot is created, and a single snapshot can be used to create many new sandboxes.
10+
11+
## Snapshots vs. Pause/Resume
12+
13+
| | Pause/Resume | Snapshots |
14+
|---|---|---|
15+
| Effect on original sandbox | Pauses (stops) the sandbox | Sandbox briefly pauses, then continues running |
16+
| Relationship | One-to-one — resume restores the same sandbox | One-to-many — snapshot can spawn many new sandboxes |
17+
| Use case | Suspend and resume a single sandbox | Create a reusable checkpoint |
18+
19+
For pause/resume functionality, see [Persistence](/docs/sandbox/persistence).
20+
21+
## Snapshot flow
22+
23+
```mermaid actions={false}
24+
graph LR
25+
A[Running Sandbox] -->|createSnapshot| B[Snapshotting]
26+
B --> C[Snapshot Created]
27+
B --> A
28+
C -->|Sandbox.create| D[New Sandbox 1]
29+
C -->|Sandbox.create| E[New Sandbox 2]
30+
C -->|Sandbox.create| F[New Sandbox N]
31+
```
32+
33+
The sandbox is briefly paused during the snapshot process but automatically returns to running state. The sandbox ID stays the same after the snapshot completes.
34+
35+
<Warning>
36+
During the snapshot, the sandbox is temporarily paused and resumed. This causes all active connections (e.g. WebSocket, PTY, command streams) to be dropped. Make sure your client handles reconnection properly.
37+
</Warning>
38+
39+
## Create a snapshot
40+
41+
You can create a snapshot from a running sandbox instance.
42+
43+
<CodeGroup>
44+
```js JavaScript & TypeScript
45+
import { Sandbox } from 'e2b'
46+
47+
const sandbox = await Sandbox.create()
48+
49+
// Create a snapshot from a running sandbox
50+
const snapshot = await sandbox.createSnapshot()
51+
console.log('Snapshot ID:', snapshot.snapshotId)
52+
```
53+
```python Python
54+
from e2b import Sandbox
55+
56+
sandbox = Sandbox.create()
57+
58+
# Create a snapshot from a running sandbox
59+
snapshot = sandbox.create_snapshot()
60+
print('Snapshot ID:', snapshot.snapshot_id)
61+
```
62+
</CodeGroup>
63+
64+
You can also create a snapshot by sandbox ID using the static method.
65+
66+
<CodeGroup>
67+
```js JavaScript & TypeScript
68+
import { Sandbox } from 'e2b'
69+
70+
// Create a snapshot by sandbox ID
71+
const snapshot = await Sandbox.createSnapshot(sandboxId)
72+
console.log('Snapshot ID:', snapshot.snapshotId)
73+
```
74+
```python Python
75+
from e2b import Sandbox
76+
77+
# Create a snapshot by sandbox ID
78+
snapshot = Sandbox.create_snapshot(sandbox_id)
79+
print('Snapshot ID:', snapshot.snapshot_id)
80+
```
81+
</CodeGroup>
82+
83+
## Create a sandbox from a snapshot
84+
85+
The snapshot ID can be used directly with `Sandbox.create()` to spawn a new sandbox from the snapshot. The new sandbox starts with the exact filesystem and memory state captured in the snapshot.
86+
87+
<CodeGroup>
88+
```js JavaScript & TypeScript highlight={5}
89+
import { Sandbox } from 'e2b'
90+
91+
const snapshot = await sandbox.createSnapshot()
92+
93+
// Create a new sandbox from the snapshot
94+
const newSandbox = await Sandbox.create(snapshot.snapshotId)
95+
```
96+
```python Python highlight={5}
97+
from e2b import Sandbox
98+
99+
snapshot = sandbox.create_snapshot()
100+
101+
# Create a new sandbox from the snapshot
102+
new_sandbox = Sandbox.create(snapshot.snapshot_id)
103+
```
104+
</CodeGroup>
105+
106+
## List snapshots
107+
108+
You can list all snapshots. The method returns a paginator for iterating through results.
109+
110+
<CodeGroup>
111+
```js JavaScript & TypeScript
112+
import { Sandbox } from 'e2b'
113+
114+
const paginator = Sandbox.listSnapshots()
115+
116+
const snapshots = []
117+
while (paginator.hasNext) {
118+
const items = await paginator.nextItems()
119+
snapshots.push(...items)
120+
}
121+
```
122+
```python Python
123+
from e2b import Sandbox
124+
125+
paginator = Sandbox.list_snapshots()
126+
127+
snapshots = []
128+
while paginator.has_next:
129+
items = paginator.next_items()
130+
snapshots.extend(items)
131+
```
132+
</CodeGroup>
133+
134+
### Filter by sandbox
135+
136+
You can filter snapshots created from a specific sandbox.
137+
138+
<CodeGroup>
139+
```js JavaScript & TypeScript
140+
import { Sandbox } from 'e2b'
141+
142+
const paginator = Sandbox.listSnapshots({ sandboxId: 'your-sandbox-id' })
143+
const snapshots = await paginator.nextItems()
144+
```
145+
```python Python
146+
from e2b import Sandbox
147+
148+
paginator = Sandbox.list_snapshots(sandbox_id="your-sandbox-id")
149+
snapshots = paginator.next_items()
150+
```
151+
</CodeGroup>
152+
153+
## Delete a snapshot
154+
155+
<CodeGroup>
156+
```js JavaScript & TypeScript
157+
import { Sandbox } from 'e2b'
158+
159+
// Returns true if deleted, false if the snapshot was not found
160+
const deleted = await Sandbox.deleteSnapshot(snapshot.snapshotId)
161+
```
162+
```python Python
163+
from e2b import Sandbox
164+
165+
Sandbox.delete_snapshot(snapshot.snapshot_id)
166+
```
167+
</CodeGroup>

images/diagram.png

-64.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)