> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mirage.strukto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# E2B Machine in TypeScript

> Run captured programs in an E2B cloud sandbox.

Runs programs in an E2B cloud sandbox. Settings and notes: [E2B](https://docs.mirage.strukto.ai/home/setup/machine/e2b.md).

## Install

```bash theme={null}
pnpm add e2b ssh2
```

## Usage

`captures` defaults to [`@external`](https://docs.mirage.strukto.ai/home/setup/machine/overview#captures):
every program Mirage does not have itself. Name `@python` to run
Python in the sandbox too.

```typescript theme={null}
import { MountMode, RAMVFS, Workspace } from '@struktoai/mirage-node'

const ws = new Workspace(
  { '/data': new RAMVFS() },
  {
    mode: MountMode.EXEC,
    machines: { box: { provider: 'e2b', sandboxId: 'sandbox-id', mountType: 'ssh', captures: ['@python', '@external'] } },
  },
)
await ws.shell("python3 -c 'print(1 + 1)'")
```

In a browser, import `@struktoai/mirage-core/machine/e2b/handle` once
to register the provider (`pnpm add @struktoai/mirage-core`), pass
`apiKey`, and use `mountType: 'own'` or leave it unset.

## Connecting the workspace

Pick a [`mount_type`](https://docs.mirage.strukto.ai/home/setup/machine/overview#connecting-the-workspace). `ssh`, `mirage` and `both` do not work in browsers. Under
`ssh` each file call is about 150 ms for a small file from a laptop; under
`mirage`, files on `ram` and `sql` mounts stay cached in the sandbox once
read, unless a policy judges the session's file calls, and E2B's `base`
template is enough: it has `fuse3` and `npm`, and Mirage installs
[`mirage-fuse`](https://docs.mirage.strukto.ai/home/setup/machine/overview#mirage) there the first time. With `own`,
base the template on [the machine image](https://docs.mirage.strukto.ai/home/setup/machine/overview.md#the-machine-image)
and run `mirage workspace create /tmp/sandbox.yaml` in the sandbox.

### A template for ssh

Build a template with `sshfs` and pass its sandbox ID as `sandbox_id`:

```typescript theme={null}
import { Sandbox, Template } from 'e2b'

await Template.build(Template().fromBaseImage().aptInstall('sshfs'), 'mirage-sshfs')
const sandbox = await Sandbox.create('mirage-sshfs', { timeoutMs: 3_600_000 })
console.log(sandbox.sandboxId)
```

## In a browser

Import the E2B machine from core (`pnpm add @struktoai/mirage-core`), which
also registers the `e2b` provider for `machines:`, and pass `apiKey`:

```ts theme={null}
import { E2BMachine } from '@struktoai/mirage-core/machine/e2b/handle'
```

Keep account keys out of shared frontend bundles; have your backend
authenticate the app. `mountType` `ssh`, `mirage` and `both` need Node, so a browser
uses `own` or none.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.