Skip to main content
You can use the files.write() method to upload data to the sandbox.

Upload single file

import fs from 'fs'
import { Sandbox } from 'novita-sandbox/code-interpreter'

const sandbox = await Sandbox.create()

// Read file from local filesystem
const localFilePath = '../local-test-file'
const content = fs.readFileSync(localFilePath, 'utf8')

// Upload file to sandbox
const filePathInSandbox = '/tmp/test-file'
const result = await sandbox.files.write(filePathInSandbox, content)
console.log(result)

await sandbox.kill()

Upload with pre-signed URL

Pre-signed upload URLs allow users to upload files securely from unauthorized environments, such as web browsers. You can use the secure: true option to create a sandbox with a pre-signed upload URL. Only authorized users can upload files throught the URL. You can optionally set an expiration time for the URL to limit its validity.
import { Sandbox } from 'novita-sandbox/code-interpreter'

const sandbox = await Sandbox.create({ secure: true })

const filePathInSandbox = '/tmp/test-file'

// Generate pre signature upload URL
const publicUploadUrl = await sandbox.uploadUrl(filePathInSandbox, {
  useSignatureExpiration: 120, // optional
})

// Simulate browser behavior
const form = new FormData()
form.append('file', new Blob(['file content']), 'test-file')

const uploadRes = await fetch(publicUploadUrl, {
  method: 'POST',
  body: form,
})
if (!uploadRes.ok) {
  throw new Error(`Upload failed: ${uploadRes.status} ${await uploadRes.text()}`)
}

// SDK Read Back Verification
const result = await sandbox.files.read(filePathInSandbox)
console.log(result)

await sandbox.kill()

Upload directory / multiple files

import fs from 'fs'
import path from 'path'

import { Sandbox } from 'novita-sandbox/code-interpreter'

const sandbox = await Sandbox.create()

const readDirectoryFiles = (directoryPath) => {
  const files = fs.readdirSync(directoryPath);

  const filesArray = files
    .filter(file => {
      const fullPath = path.join(directoryPath, file);
      return fs.statSync(fullPath).isFile();
    })
    .map(file => {
      const filePath = path.join(directoryPath, file);
    
      return {
        path: filePath,
        data: fs.readFileSync(filePath, 'utf8')
      };
    });

  return filesArray;
};

const localDirectoryPath = '../local-test-dir'
const files = readDirectoryFiles(localDirectoryPath);
console.log(files); 

files.forEach(file => {
  file.path = file.path.replace('../local-test-dir', '/tmp')
})

const result = await sandbox.files.write(files)
console.log(result)

await sandbox.kill()
Last modified on June 4, 2026