How to Migrate from E2B to CubeSandbox: Step-by-Step Guide
CubeSandbox acts as a drop-in replacement for the E2B SDK, requiring only environment variable changes to migrate existing integrations without modifying business logic.
The TencentCloud/CubeSandbox project provides a seamless migration path from E2B (E2B Sandboxes) with built-in compatibility shims across Python, Node.js, and Go SDKs. By updating a few environment variables, you can redirect existing code to the CubeSandbox control plane while preserving all existing functionality. This guide covers the complete migration process using the actual source implementation.
Step-by-Step Migration Process
Identify Current E2B Configuration
Locate your existing E2B environment variables in your shell exports or .env files. You typically need to find:
E2B_API_URLE2B_API_KEYE2B_TEMPLATE_ID(if using custom templates)
Configure CubeSandbox Environment Variables
Create or update your .env file to include CubeSandbox-specific variables. The SDKs automatically prefer CUBE_API_URL and CUBE_API_KEY, falling back to E2B equivalents if CubeSandbox variables are absent.
CUBE_API_URL=<your-cube-api-host>:3000
CUBE_API_KEY=<your-api-key>
CUBE_TEMPLATE_ID=<template-id>
Verify SDK Configuration
Confirm that the SDK correctly resolves the new environment variables before deploying. The configuration loaders read variables on first use.
Python:
import cubesandbox
cfg = cubesandbox.Config.from_env()
print(cfg.api_url, cfg.api_key)
Node.js:
const { Config } = require('@cubesandbox/sdk');
const cfg = Config.fromEnv();
console.log(cfg.apiUrl, cfg.apiKey);
Go:
cfg, _ := cubesandbox.NewConfigFromEnv()
fmt.Println(cfg.APIURL, cfg.APIKey)
Test the Migration
Run a quick sandbox creation to verify connectivity:
import os
from cubesandbox import Sandbox
sandbox = Sandbox.create(template=os.getenv("CUBE_TEMPLATE_ID") or "default")
sandbox.exec("echo 'Migration successful'")
Optional Advanced Configuration
If using custom network policies, CubeSandbox converts E2B-style per-host rules automatically. The compatibility layers in sdk/python/cubesandbox/_policy.py and sdk/node/src/policy.ts handle the transformation to CubeSandbox's native format. No code changes are required unless you want to use CubeSandbox-specific features like per-sandbox egress tokens.
SDK Compatibility Implementation
The migration works because CubeSandbox SDKs implement a priority-based configuration loader. In sdk/go/config.go, sdk/python/cubesandbox/_config.py, and sdk/node/src/config.ts, the code first checks for CUBE_API_URL and CUBE_API_KEY, then falls back to E2B_API_URL and E2B_API_KEY if the CubeSandbox variables are unset.
This backward compatibility ensures zero code changes for existing E2B integrations while enabling immediate access to CubeSandbox features like hardware-level isolation and credential vaults.
Complete Code Examples
Python Implementation
import os
from cubesandbox import Sandbox, Config
# Load configuration from env (CubeSandbox vars take precedence)
cfg = Config.from_env()
print("API URL:", cfg.api_url) # → CUBE_API_URL if set
print("API Key:", cfg.api_key)
# Create a sandbox (works with both E2B and CubeSandbox)
sandbox = Sandbox.create(template=os.getenv("CUBE_TEMPLATE_ID") or "default")
sandbox.exec("python - <<'PY'\nprint('Hello from CubeSandbox')\nPY")
Node.js/TypeScript Implementation
import { Config, Sandbox } from '@cubesandbox/sdk';
// Resolve env vars – CUBE_API_URL / CUBE_API_KEY win over E2B equivalents
const cfg = Config.fromEnv();
console.log('API URL:', cfg.apiUrl);
console.log('API Key:', cfg.apiKey);
// Launch a sandbox
(async () => {
const sandbox = await Sandbox.create({
template: process.env.CUBE_TEMPLATE_ID ?? 'default',
});
const result = await sandbox.exec('node -e "console.log(\'Hello from CubeSandbox\')"');
console.log(result.stdout);
})();
Go Implementation
package main
import (
"context"
"fmt"
"log"
"os"
cs "github.com/tencentcloud/CubeSandbox/sdk/go"
)
func main() {
cfg, err := cs.NewConfigFromEnv()
if err != nil {
log.Fatal(err)
}
fmt.Println("API URL:", cfg.APIURL) // CUBE_API_URL if set
fmt.Println("API Key:", cfg.APIKey)
// Create a sandbox
sandbox, err := cs.NewSandbox(cfg, cs.SandboxOptions{
TemplateID: os.Getenv("CUBE_TEMPLATE_ID"),
})
if err != nil {
log.Fatal(err)
}
out, err := sandbox.Exec(context.Background(), cs.ExecOptions{
Cmd: []string{"bash", "-c", "echo Hello from CubeSandbox"},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(string(out.Stdout))
}
Summary
- Environment variable swap: Replace
E2B_API_URL/E2B_API_KEYwithCUBE_API_URL/CUBE_API_KEYin your.envfiles. - Automatic fallback: SDKs in
sdk/go/config.go,sdk/python/cubesandbox/_config.py, andsdk/node/src/config.tsprioritize CubeSandbox variables but fall back to E2B equivalents for backward compatibility. - Zero code changes: Existing business logic remains untouched; only configuration requires updates.
- Network policy compatibility: E2B-style network rules convert automatically via
policy.tsandpolicy.pycompatibility layers. - Template migration: Optionally update
CUBE_TEMPLATE_IDto use CubeSandbox-specific templates instead ofE2B_TEMPLATE_ID.
Frequently Asked Questions
Do I need to modify my existing code to migrate from E2B to CubeSandbox?
No. CubeSandbox is designed as a drop-in replacement. The SDKs automatically detect CubeSandbox environment variables and fall back to E2B variables if needed, so your existing function calls remain unchanged.
Which environment variables does CubeSandbox use compared to E2B?
CubeSandbox uses CUBE_API_URL, CUBE_API_KEY, and CUBE_TEMPLATE_ID. These take precedence over the E2B equivalents (E2B_API_URL, E2B_API_KEY, E2B_TEMPLATE_ID) according to the implementation in sdk/python/cubesandbox/_config.py and sdk/node/src/config.ts.
Will my network policies work after migrating to CubeSandbox?
Yes. CubeSandbox includes compatibility layers in sdk/python/cubesandbox/_policy.py and sdk/node/src/policy.ts that automatically convert E2B per-host network rules to CubeSandbox's native format. No manual conversion is required unless you want to use CubeSandbox-specific features.
Can I run E2B and CubeSandbox simultaneously during migration?
Yes. You can configure both sets of environment variables. The CubeSandbox SDK prioritizes CUBE_* variables, allowing you to migrate gradually by switching individual services or keeping E2B as a fallback until you verify CubeSandbox connectivity.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →