Skip to main content
Run Claude Code in a sandbox to execute AI-generated code safely.

Install Claude Code

Set up Node.js with Claude Code and the tools it needs: curl for fetching, git for repos, and ripgrep for fast code search.
// 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 and publish

Build the template. Claude Code itself is lightweight, so minimal resources work fine.
// build.ts
import { Template, defaultBuildLogger } from 'e2b'
import { template as claudeCodeTemplate } from './template'

await Template.build(claudeCodeTemplate, 'claude-code', {
  cpuCount: 1,
  memoryMB: 1024,
  onBuildLogs: defaultBuildLogger(),
})

Run AI prompts

Create a sandbox with your Anthropic API key and pipe prompts to Claude. It can write files, run commands, and build whatever you describe. Get your API key from the Anthropic Console. The -p flag runs Claude in non-interactive pipe mode. The --dangerously-skip-permissions flag allows Claude to execute file and command operations without confirmation prompts—this is safe since the sandbox is isolated. Setting timeout=0 disables the command timeout, letting Claude run as long as needed.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('claude-code', {
  timeoutMs: 60_000,
  envs: {
    ANTHROPIC_API_KEY: '<your api key>',
  },
})

const result = await sandbox.commands.run(
  `echo 'Create a hello world index.html' | claude -p --dangerously-skip-permissions`,
  { timeoutMs: 0 }
)
console.log(result.stdout)