Documentation Index
Fetch the complete documentation index at: https://mintlify.com/cloudflare/sandbox-sdk/llms.txt
Use this file to discover all available pages before exploring further.
Creates an isolated execution context for running code. Each context maintains its own state, variables, and execution history.
Method Signature
async createCodeContext(
options?: CreateContextOptions
): Promise<CodeContext>
Parameters
Configuration options for the context
language
'python' | 'javascript' | 'typescript'
default:"'python'"
Programming language for the context
cwd
string
default:"'/workspace'"
Working directory for the context
envVars
Record<string, string | undefined>
Environment variables for the context. Undefined values are skipped.
Request timeout in milliseconds
Returns
The created code execution context
Unique identifier for the context
Programming language of the context
Current working directory
When the context was created
When the context was last used
Example
import { getSandbox } from '@cloudflare/sandbox';
const sandbox = getSandbox(env.SANDBOX, 'my-sandbox');
// Create a Python context
const pythonContext = await sandbox.createCodeContext({
language: 'python',
cwd: '/workspace/analysis',
envVars: {
API_KEY: 'secret-key'
}
});
console.log(pythonContext.id); // "ctx_abc123..."
// Create a JavaScript context
const jsContext = await sandbox.createCodeContext({
language: 'javascript'
});
Notes
- Contexts are isolated from each other - variables and state are not shared
- Multiple contexts can exist simultaneously
- Each context has its own execution history
- Contexts persist until explicitly deleted or the sandbox is destroyed
- Use the same context for related code executions to maintain state
See Also