For a complete guide on running Claude Code in E2B sandboxes — including working with repositories, streaming output, and connecting MCP tools — see the Agents in Sandbox: Claude Code guide.
Claude Code Agent available in a sandbox
// template.ts
import { Template } from 'e2b'
export const template = Template()
.fromNodeImage('24')
.aptInstall(['curl', 'git', 'ripgrep'])
// Claude Code will be available globally as "claude"
.npmInstall('@anthropic-ai/claude-code@latest', { g: true })
// build.ts
import { Template, defaultBuildLogger } from 'e2b'
import { template as claudeCodeTemplate } from './template'
Template.build(claudeCodeTemplate, 'claude-code', {
cpuCount: 1,
memoryMB: 1024,
onBuildLogs: defaultBuildLogger(),
})
// sandbox.ts
import { Sandbox } from 'e2b'
const sbx = await Sandbox.create('claude-code', {
envs: {
ANTHROPIC_API_KEY: '<your api key>',
},
})
console.log('Sandbox created', sbx.sandboxId)
// Print help for Claude Code
// const result = await sbx.commands.run('claude --help')
// console.log(result.stdout)
// Run a prompt with Claude Code
const result = await sbx.commands.run(
`claude --dangerously-skip-permissions -p 'Create a hello world index.html'`,
{ timeoutMs: 0 }
)
console.log(result.stdout)
sbx.kill()
Was this page helpful?