Sessions
Create isolated execution contexts within a sandbox. Each session maintains its own shell state, environment variables, and working directory. See Session management concept for details.
Create a new isolated execution session.
const session = await sandbox.createSession(options?: SessionOptions): Promise<ExecutionSession>
Parameters:
options
(optional):id
- Custom session ID (auto-generated if not provided)env
- Environment variables for this sessioncwd
- Working directory (default:"/workspace"
)
Returns: Promise<ExecutionSession>
with all sandbox methods bound to this session
// Multiple isolated environmentsconst prodSession = await sandbox.createSession({ id: "prod", env: { NODE_ENV: "production", API_URL: "https://api.example.com" }, cwd: "/workspace/prod",});
const testSession = await sandbox.createSession({ id: "test", env: { NODE_ENV: "test", API_URL: "http://localhost:3000" }, cwd: "/workspace/test",});
// Run in parallelconst [prodResult, testResult] = await Promise.all([ prodSession.exec("npm run build"), testSession.exec("npm run build"),]);
// Multiple isolated environmentsconst prodSession = await sandbox.createSession({ id: 'prod', env: { NODE_ENV: 'production', API_URL: 'https://api.example.com' }, cwd: '/workspace/prod'});
const testSession = await sandbox.createSession({ id: 'test', env: { NODE_ENV: 'test', API_URL: 'http://localhost:3000' }, cwd: '/workspace/test'});
// Run in parallelconst [prodResult, testResult] = await Promise.all([ prodSession.exec('npm run build'), testSession.exec('npm run build')]);
Retrieve an existing session by ID.
const session = await sandbox.getSession(sessionId: string): Promise<ExecutionSession>
Parameters:
sessionId
- ID of an existing session
Returns: Promise<ExecutionSession>
bound to the specified session
// First request - create sessionconst session = await sandbox.createSession({ id: "user-123" });await session.exec("git clone https://github.com/user/repo.git");await session.exec("cd repo && npm install");
// Second request - resume session (environment and cwd preserved)const session = await sandbox.getSession("user-123");const result = await session.exec("cd repo && npm run build");
// First request - create sessionconst session = await sandbox.createSession({ id: 'user-123' });await session.exec('git clone https://github.com/user/repo.git');await session.exec('cd repo && npm install');
// Second request - resume session (environment and cwd preserved)const session = await sandbox.getSession('user-123');const result = await session.exec('cd repo && npm run build');
Set environment variables in the sandbox.
await sandbox.setEnvVars(envVars: Record<string, string>): Promise<void>
Parameters:
envVars
- Key-value pairs of environment variables to set
const sandbox = getSandbox(env.Sandbox, "user-123");
// Set environment variables firstawait sandbox.setEnvVars({ API_KEY: env.OPENAI_API_KEY, DATABASE_URL: env.DATABASE_URL, NODE_ENV: "production",});
// Now commands can access these variablesawait sandbox.exec("python script.py");
const sandbox = getSandbox(env.Sandbox, 'user-123');
// Set environment variables firstawait sandbox.setEnvVars({ API_KEY: env.OPENAI_API_KEY, DATABASE_URL: env.DATABASE_URL, NODE_ENV: 'production'});
// Now commands can access these variablesawait sandbox.exec('python script.py');
Destroy the sandbox container and free up resources.
await sandbox.destroy(): Promise<void>
async function executeCode(code) { const sandbox = getSandbox(env.Sandbox, `temp-${Date.now()}`);
try { await sandbox.writeFile("/tmp/code.py", code); const result = await sandbox.exec("python /tmp/code.py"); return result.stdout; } finally { await sandbox.destroy(); }}
async function executeCode(code: string): Promise<string> { const sandbox = getSandbox(env.Sandbox, `temp-${Date.now()}`);
try { await sandbox.writeFile('/tmp/code.py', code); const result = await sandbox.exec('python /tmp/code.py'); return result.stdout; } finally { await sandbox.destroy(); }}
The ExecutionSession
object has all sandbox methods bound to the specific session:
Commands: exec()
, execStream()
Processes: startProcess()
, listProcesses()
, killProcess()
, killAllProcesses()
, getProcessLogs()
, streamProcessLogs()
Files: writeFile()
, readFile()
, mkdir()
, deleteFile()
, renameFile()
, moveFile()
, gitCheckout()
Environment: setEnvVars()
Code Interpreter: createCodeContext()
, runCode()
, listCodeContexts()
, deleteCodeContext()
- Session management concept - How sessions work
- Commands API - Execute commands
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-