|
| 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> |
0 commit comments