SDK v2 migration guide
This guide helps you migrate from E2B SDK v1 to v2, covering all breaking changes and new patterns.
Table of contents
SDK v2 introduces several important changes:
- New creation pattern in Python Synchronous SDK
- Secure by default
- Updated file operations in Python SDK
- Updated list method
Breaking changes
1. Sandbox creation in synchronous Python SDK
In v2, the synchronous Python SDK uses a class method create() instead of the constructor Sandbox().
from e2b_code_interpreter import Sandbox
sandbox = Sandbox.create()
sandbox = Sandbox.create(template="base")In the v1, you would instantiate directly:
from e2b_code_interpreter import Sandbox
sandbox = Sandbox()
sandbox = Sandbox(template="base")2. Secure communication by default
Sandboxes are now secure by default. This means you can’t access the sandbox controller directly through its URL without an authentication header. The SDK automatically handles the authentication header for you.
For custom templates created before envd v0.2.0, you need to rebuild them to enable secure communication.
Otherwise, you will receive error messages when creating sandboxes. You can check the template envd version using the e2b template list command or view the templates list in the dashboard.
You can temporarily disable secure communication by setting secure to false during sandbox creation, but this is not recommended for production environments.
import { Sandbox } from '@e2b/code-interpreter'
const sandbox = await Sandbox.create({ secure: false }) // Explicitly disableCheck more details in the secured access documentation.
3. File writing in Python SDK
The file writing API in Python has been made more consistent.
In v2, use sandbox.files.write() for single files and sandbox.files.write_files() for multiple files:
from e2b_code_interpreter import Sandbox
sandbox = Sandbox.create()
# Write single file
info = sandbox.files.write("/tmp/file.txt", "content")
# Write multiple files
infos = sandbox.files.write_files([
{"path": "/tmp/file1.txt", "data": "content1"},
{"path": "/tmp/file2.txt", "data": "content2"}
])In v1, the same write() method was overloaded for both single and multiple files:
from e2b_code_interpreter import Sandbox
sandbox = Sandbox.create()
# Write single file
info = sandbox.write(path="/tmp/file.txt", data="content")
# Write multiple files
infos = sandbox.write([
{"path": "/tmp/file1.txt", "data": "content1"},
{"path": "/tmp/file2.txt", "data": "content2"}
])4. Listing sandboxes
The method for listing sandboxes has been updated to use pagination.
import { Sandbox, SandboxInfo } from '@e2b/code-interpreter'
// Get paginator
const paginator = Sandbox.list()
// Iterate through all sandboxes
for (const sandbox of await paginator.nextItems()) {
console.log(sandbox.sandboxId)
}
// Iterate through all sandboxes
const allSandboxes: SandboxInfo[] = []
while (paginator.hasNext) {
const items = await paginator.nextItems()
allSandboxes.push(...items)
}
// With query
const queryPaginator = Sandbox.list({query: {metadata: {key: "value"}}})