Skip to main content

Documentation Index

Fetch the complete documentation index at: https://e2b-squash-sandbox-pages.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

Each E2B Sandbox has its own isolated filesystem. The Hobby tier sandboxes come with 10 GB of the free disk space and Pro tier sandboxes come with 20 GB. With E2B SDK you can read and write files, get file and directory metadata, watch directories for changes, upload data to the sandbox, and download data from the sandbox.

Read and write files

Reading files

You can read files from the sandbox filesystem using the files.read() method.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create()
const fileContent = await sandbox.files.read('/path/to/file')

Writing single files

You can write single files to the sandbox filesystem using the files.write() method.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create()

await sandbox.files.write('/path/to/file', 'file content')

Writing multiple files

You can also write multiple files to the sandbox.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create()

await sandbox.files.write([
    { path: '/path/to/a', data: 'file content' },
    { path: '/another/path/to/b', data: 'file content' }
])

File and directory metadata

You can get information about a file or directory using the files.getInfo() / files.get_info() methods. Information such as file name, type, and path is returned.

File metadata

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create()

// Create a new file
await sandbox.files.write('test_file.txt', 'Hello, world!')

// Get information about the file
const info = await sandbox.files.getInfo('test_file.txt')

console.log(info)
// {
//   name: 'test_file.txt',
//   type: 'file',
//   path: '/home/user/test_file.txt',
//   size: 13,
//   mode: 0o644,
//   permissions: '-rw-r--r--',
//   owner: 'user',
//   group: 'user',
//   modifiedTime: '2025-05-26T12:00:00.000Z',
//   symlinkTarget: null
// }

Directory metadata

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create()

// Create a new directory
await sandbox.files.makeDir('test_dir')

// Get information about the directory
const info = await sandbox.files.getInfo('test_dir')

console.log(info)
// {
//   name: 'test_dir',
//   type: 'dir',
//   path: '/home/user/test_dir',
//   size: 0,
//   mode: 0o755,
//   permissions: 'drwxr-xr-x',
//   owner: 'user',
//   group: 'user',
//   modifiedTime: '2025-05-26T12:00:00.000Z',
//   symlinkTarget: null
// }

Watch directory for changes

You can watch a directory for changes using the files.watchDir() method in JavaScript and files.watch_dir() method in Python.
Since events are tracked asynchronously, their delivery may be delayed. It’s recommended not to collect or close watcher immediately after making a change.
import { Sandbox, FilesystemEventType } from 'e2b'

const sandbox = await Sandbox.create()
const dirname = '/home/user'

// Start watching directory for changes
const handle = await sandbox.files.watchDir(dirname, async (event) => {
  console.log(event)
  if (event.type === FilesystemEventType.WRITE) {
    console.log(`wrote to file ${event.name}`)
  }
})

// Trigger file write event
await sandbox.files.write(`${dirname}/my-file`, 'hello')

Recursive watching

You can enable recursive watching using the parameter recursive.
When rapidly creating new folders (e.g., deeply nested path of folders), events other than CREATE might not be emitted. To avoid this behavior, create the required folder structure in advance.
import { Sandbox, FilesystemEventType } from 'e2b'

const sandbox = await Sandbox.create()
const dirname = '/home/user'

// Start watching directory for changes
const handle = await sandbox.files.watchDir(dirname, async (event) => {
  console.log(event)
  if (event.type === FilesystemEventType.WRITE) {
    console.log(`wrote to file ${event.name}`)
  }
}, {
  recursive: true
})

// Trigger file write event
await sandbox.files.write(`${dirname}/my-folder/my-file`, 'hello')

Upload data

You can upload data to the sandbox using the files.write() method.

Upload single file

import fs from 'fs'
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create()

// Read file from local filesystem
const content = fs.readFileSync('/local/path')
// Upload file to sandbox
await sandbox.files.write('/path/in/sandbox', content)

Upload with pre-signed URL

Sometimes, you may want to let users from unauthorized environments, like a browser, upload files to the sandbox. For this use case, you can use pre-signed URLs to let users upload files securely. All you need to do is create a sandbox with the secure: true option. An upload URL will then be generated with a signature that allows only authorized users to upload files. You can optionally set an expiration time for the URL so that it will be valid only for a limited time.
import { Sandbox } from 'e2b'

// Start a secured sandbox (all operations must be authorized by default)
const sandbox = await Sandbox.create(template, { secure: true })

// Create a pre-signed URL for file upload with a 10 second expiration
const publicUploadUrl = await sandbox.uploadUrl(
  'demo.txt', {
    useSignatureExpiration: 10_000, // optional
  },
)

// Upload a file with a pre-signed URL (this can be used in any environment, such as a browser)
const form = new FormData()
form.append('file', 'file content')

await fetch(publicUploadUrl, { method: 'POST', body: form })

// File is now available in the sandbox and you can read it
const content = sandbox.files.read('/path/in/sandbox')

Upload directory / multiple files

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create()

// Read all files in the directory and store their paths and contents in an array
const readDirectoryFiles = (directoryPath) => {
  // Read all files in the local directory
  const files = fs.readdirSync(directoryPath);

  // Map files to objects with path and data
  const filesArray = files
    .filter(file => {
      const fullPath = path.join(directoryPath, file);
      // Skip if it's a directory
      return fs.statSync(fullPath).isFile();
    })
    .map(file => {
      const filePath = path.join(directoryPath, file);

      // Read the content of each file
      return {
        path: filePath,
        data: fs.readFileSync(filePath, 'utf8')
      };
    });

  return filesArray;
};

// Usage example
const files = readDirectoryFiles('/local/dir');
console.log(files);
// [
//   { path: '/local/dir/file1.txt', data: 'File 1 contents...' },
//   { path: '/local/dir/file2.txt', data: 'File 2 contents...' },
//   ...
// ]

await sandbox.files.write(files)

Download data

You can download data from the sandbox using the files.read() method.

Download single file

import fs from 'fs'
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create()

// Read file from sandbox
const content = await sandbox.files.read('/path/in/sandbox')
// Write file to local filesystem
fs.writeFileSync('/local/path', content)

Download with pre-signed URL

Sometimes, you may want to let users from unauthorized environments, like a browser, download files from the sandbox. For this use case, you can use pre-signed URLs to let users download files securely. All you need to do is create a sandbox with the secure: true option. A download URL will then be generated with a signature that allows only authorized users to access files. You can optionally set an expiration time for the URL so that it will be valid only for a limited time.
import fs from 'fs'
import { Sandbox } from 'e2b'

// Start a secured sandbox (all operations must be authorized by default)
const sandbox = await Sandbox.create(template, { secure: true })

// Create a pre-signed URL for file download with a 10 second expiration
const publicUrl = await sandbox.downloadUrl(
  'demo.txt', {
    useSignatureExpiration: 10_000, // optional
  },
)

// Download a file with a pre-signed URL (this can be used in any environment, such as a browser)
const res = await fetch(publicUrl)
const content = await res.text()