@computesdk/codesandbox

中文文档
1.5.70发布于 2026/11/06MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1万

CodeSandbox provider for ComputeSDK - fast browser-compatible sandboxes with npm and Python support

原始使用文档
文档目录14 节
  1. Installation
  2. Setup
  3. Quick Start
  4. Configuration
  5. Environment Variables
  6. Configuration Options
  7. Features
  8. API Reference
  9. Command Execution
  10. Filesystem Operations
  11. Sandbox Management
  12. Best Practices
  13. Support
  14. License

@computesdk/codesandbox

CodeSandbox provider for ComputeSDK - Execute code in secure, isolated CodeSandbox environments with full filesystem and development environment support.

Installation

npm install @computesdk/codesandbox

Setup

  1. Get your CodeSandbox API key from codesandbox.io/t/api
  2. Set the environment variable:
export CSB_API_KEY=your_api_key_here

Quick Start

import { codesandbox } from '@computesdk/codesandbox';

const compute = codesandbox({
  apiKey: process.env.CSB_API_KEY,
  templateId: 'universal', // Optional: specify template
  timeout: 600000 // 10 minutes
});

const sandbox = await compute.instances.create();

const result = await sandbox.runCommand('node -e "console.log(\"Hello from CodeSandbox!\")"');
console.log(result.stdout);

await sandbox.destroy();

Configuration

Environment Variables

export CSB_API_KEY=your_api_key_here

Configuration Options

interface CodesandboxConfig {
  /** CodeSandbox API key - if not provided, will use CSB_API_KEY env var */
  apiKey?: string;
  /** Template to use for new sandboxes (defaults to universal template) */
  templateId?: string;
  /** Default runtime environment (e.g. 'node', 'python') */
  runtime?: string;
  /** Execution timeout in milliseconds */
  timeout?: number;
}

Features

  • ✅ Command Execution - Run shell commands in sandbox
  • ✅ Filesystem Operations - Full file system access via CodeSandbox API
  • ✅ Template Support - Create sandboxes from custom templates
  • ✅ Development Environment - Full development setup with package managers
  • ✅ Persistence - Files persist across hibernation/resume cycles
  • ✅ Snapshot/Resume - Fast sandbox restoration from snapshots

API Reference

Command Execution

// Run Node.js code via heredoc
const result = await sandbox.runCommand(`node - <<'JS'
const fs = require('fs');
const data = { timestamp: Date.now() };
console.log('Processing data:', JSON.stringify(data));
JS`);

// Run Python code via heredoc
const result = await sandbox.runCommand(`python - <<'PY'
import datetime
import json

data = {'timestamp': datetime.datetime.now().isoformat()}
print('Processing data:', json.dumps(data))
PY`);

// List files
const result = await sandbox.runCommand('ls -la');

// Install Node.js packages
const result = await sandbox.runCommand('npm install lodash');

// Install Python packages
const result = await sandbox.runCommand('pip install requests');

// Run development server
const result = await sandbox.runCommand('npm run dev');

Filesystem Operations

// Write file
await sandbox.filesystem.writeFile('/project/workspace/app.js', `
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.json({ message: 'Hello from CodeSandbox!' });
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});
`);

// Read file
const content = await sandbox.filesystem.readFile('/project/workspace/package.json');

// Create directory
await sandbox.filesystem.mkdir('/project/workspace/src');

// List directory contents
const files = await sandbox.filesystem.readdir('/project/workspace');

// Check if file exists
const exists = await sandbox.filesystem.exists('/project/workspace/app.js');

// Remove file or directory
await sandbox.filesystem.remove('/project/workspace/temp.txt');

Sandbox Management

// Get sandbox info
const info = await sandbox.getInfo();
console.log(info.id, info.provider, info.status);

// Resume existing sandbox
const existing = await compute.instances.getById('sandbox-id');

// Hibernate sandbox (saves state)
await compute.instances.destroy('sandbox-id'); // Actually hibernates

// Note: CodeSandbox doesn't support listing all sandboxes
// Each sandbox is managed individually

Best Practices

  1. Resource Management: Use hibernation instead of destroying sandboxes to preserve state
  2. Error Handling: Use try-catch blocks for robust error handling
  3. Timeouts: Set appropriate timeouts for long-running tasks
  4. File Organization: Organize files in /project/workspace/ directory
  5. Template Usage: Use appropriate templates for your project type
  6. API Key Security: Never commit API keys to version control
  7. Snapshot Management: Leverage CodeSandbox's snapshot/resume capabilities

Support

License

MIT

版本与发布记录

@computesdk/codesandbox 共发布 92 个版本,首次发布于 2025/08/21;过去 12 个月发布了 80 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.5.70latest2026/11/064 天前
1.5.692026/11/046 天前
1.5.682026/10/2812 天前
1.5.672026/10/2317 天前
1.5.662026/10/2318 天前
1.5.652026/10/2218 天前

关于 @computesdk/codesandbox 的常见问题

@computesdk/codesandbox 是什么?

CodeSandbox provider for ComputeSDK - fast browser-compatible sandboxes with npm and Python support

如何安装 @computesdk/codesandbox?

在项目目录运行 npm install @computesdk/codesandbox,或使用 pnpm add / yarn add / bun add。

@computesdk/codesandbox 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@computesdk/codesandbox 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@computesdk/codesandbox 可以商用吗?

@computesdk/codesandbox 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@computesdk/codesandbox 的下载量有多少?

根据 npm 官方下载统计,@computesdk/codesandbox 在 2026/10/11 – 2026/11/09 这 30 天内被下载 10,077 次,日均约 336 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@computesdk/codesandbox 还在维护吗?

最新版本 1.5.70 发布于 2026/11/06(4 天前);至今共发布 92 个版本,其中过去 12 个月发布了 80 个;首次发布于 2025/08/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。