Download data from sandbox
You can download data from the sandbox using the files.read()
method.
import fs from 'fs'
import { Sandbox } from '@e2b/code-interpreter'
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.
import fs from 'fs'
import { Sandbox } from '@e2b/code-interpreter'
// 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', {
useSignature: true,
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()