↩ BLOG
/
Guides

JavaScript Guide: Run Claude Code in an E2B Sandbox

Tereza Tizkova
Growth & E2B
Share
JS/TS
PYTHON

Claude Code is Anthropic’s CLI that turns prompts into concrete edits, files, and shell commands. Running it inside an E2B Sandbox gives you an isolated environment where the agent can safely create files, install tools, and execute without interactive prompts.

What we’ll build

  • In this guide, we will show step-by-step how to run Claude Code in E2B’s isolated cloud sandbox.
  • We’ll pass a simple prompt (“create a hello-world index.html”), cover the two timeouts you should care about, and keep secrets in .env.
  • We will build a one-shot command that runs a prompt using Claude Code agent and returns the output.

See full example in the E2B Cookbook.

This is a JavaScript guide using E2B JS/TS SDK, for the Python SDK version, see the Python version.

Components we’ll use

  • E2B JS SDK – creates and controls the sandbox from Node.
  • Prebuilt sandbox template: anthropic-claude-code which already includes the @anthropic-ai/claude-code CLI, no extra setup needed.
  • Anthropic API – Claude Code uses your Anthropic key inside the sandbox.

Prerequisites

Outline

  1. Create the project
  2. Add the files (package.json, src/index.ts)
  3. Put E2B_API_KEY in .env
  4. Install and run

1) Create the project

We make a new folder for this example, then a src folder for code. Create and commit .gitignore before .env so secrets never enter git history.

mkdir anthropic-claude-code-in-sandbox-js
cd anthropic-claude-code-in-sandbox-js
mkdir -p src

Target structure:

anthropic-claude-code-in-sandbox-js/
├─ src/
│  └─ index.ts
├─ package.json
├─ .gitignore
└─ .env

2) Files to add

Now we create the following files.

File: package.json

We run TypeScript directly with tsx (no build step). "type": "module" gives us ESM imports.

{
  "name": "anthropic-claude-code-in-sandbox",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "tsx src/index.ts"
  },
  "dependencies": {
    "dotenv": "^16.5.0",
    "e2b": "^1.4.0"
  },
  "devDependencies": {
    "tsx": "^4.20.3"
  }
}

File: .gitignore

.env
node_modules/

File: .env

The SDK reads E2B_API_KEY from the environment. We’ll load this file at runtime.

E2B_API_KEY="YOUR_E2B_API_KEY"

File: src/index.ts

Then, we create the index.ts file that does four things:

  1. Loads .env so E2B_API_KEY is available to the SDK.
  2. Creates the sandbox from the prebuilt template.
  3. Injects the Anthropic key.
  4. Runs a non-interactive prompt, prints the output, and cleans up.
import dotenv from 'dotenv'
import { Sandbox } from 'e2b'

dotenv.config()

const templateName = 'anthropic-claude-code'

const sbx = await Sandbox.create(templateName, {
  // Add this to match the README’s example timeout; remove it to match the file exactly
  // timeoutMs: 60 * 5 * 1000,
  envs: {
    ANTHROPIC_API_KEY: '<your api key>',
  },
})

console.log('Sandbox created', sbx.sandboxId)

// Run a prompt with Claude Code (non-interactive)
const result = await sbx.commands.run(
  "echo 'Create a hello world index.html' | claude -p --dangerously-skip-permissions",
  { timeoutMs: 0 } // allow long-running commands
)

console.log(result.stdout)

await sbx.kill()

3) Install & run

Finally, we istall dependencies and start the script with Node + tsx.

npm install
npm run start

Expected: console output from Claude Code. Any files it creates (e.g., index.html) live inside the sandbox filesystem

See full example in the E2B Cookbook.

Follow @e2b on X to see live updates.
/
We're hiring! Check open positions on the Careers page.
/
Hundreds of millions of sandboxes launched.
/
We're always Excited 2 Build.
/