This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
npm run build- Full production build (clean, esbuild, generate types for both CJS and ESM)npm run build:esbuild- Run esbuild bundling onlynpm run clean- Remove dist directorynpm run dev:cli- Development mode with file watching for CLI (watches src/bin)
npm run typecheck- Run TypeScript type checking without emitting filesnpm run format- Format code using Prettier
npm run dev:cli- Start development mode with file watching specifically for CLI changes- The CLI binary is built to
dist/bin/codesandbox.mjsand made executable - CLI can be run with
./dist/bin/codesandbox.mjsor justcsbif installed globally
npm run build-openapi- Generate API clients from production OpenAPI specnpm run build-openapi:staging- Generate API clients from staging OpenAPI spec- Generated clients are placed in
src/api-clients/directory
SDK Entry Points:
src/index.ts- Main SDK export withCodeSandboxclasssrc/browser/index.ts- Browser-specific SDK buildsrc/node/index.ts- Node.js-specific SDK build
Key Classes:
CodeSandbox- Main SDK class providingsandboxesandhostspropertiesSandboxes(src/Sandboxes.ts) - Manages sandbox operations (create, list, get, fork)Sandbox(src/Sandbox.ts) - Individual sandbox instance with connection and session managementSandboxClient(src/SandboxClient/index.ts) - High-level client for sandbox interactionsAPI(src/API.ts) - Low-level API wrapper for all CodeSandbox REST endpoints
Communication Layer:
src/pitcher-protocol/- WebSocket protocol definitions and message types for real-time sandbox communicationsrc/AgentClient/- WebSocket client implementation for connecting to sandbox agents
CLI:
src/bin/main.tsx- CLI entry point using Ink (React for CLI)src/bin/commands/- Individual CLI commands (build, sandbox, previewHosts, hostTokens)src/bin/ui/- Ink-based UI components for the interactive CLI
Multi-Format Output:
- ESM build:
dist/esm/(primary format, "type": "module") - CommonJS build:
dist/cjs/(compatibility) - Browser build: Separate bundles with browser polyfills
- CLI build: Standalone executable with shebang
Key Build Features:
- esbuild for bundling with custom plugins for module replacement
- Separate TypeScript compilation for type definitions
- Browser polyfills for Node.js modules (os, path, crypto, etc.)
- External dependencies preserved except for CLI build which bundles React/Ink
Configuration:
- Main tsconfig.json excludes
src/binandsrc/api-clients - Separate build configs for CJS (
tsconfig.build-cjs.json) and ESM (tsconfig.build-esm.json) - Strict TypeScript configuration with comprehensive type checking
Generated Types:
src/api-clients/contains auto-generated API clients and types from OpenAPI specs- Multiple API client modules for different sandbox services (fs, git, shell, etc.)
API Authentication:
- Requires CodeSandbox API token (CSB_API_KEY environment variable)
- Token creation: https://codesandbox.io/t/api
- Automatic User-Agent header injection with SDK version
Environment Detection:
- Automatic base URL inference based on API key format
- Support for different CodeSandbox environments (production, staging)
The codebase includes a test-template/ directory with a Vite + React + TypeScript template for SDK testing.
SDK Documentation: https://codesandbox.io/docs/sdk
Contributing to Documentation:
- Documentation source is located at https://github.com/codesandbox/docs
- SDK-specific docs are in
packages/projects-docs/pages/sdk/folder - Update documentation there when making changes to SDK functionality
Error Handling:
handleResponseutility for consistent API error handling- Retry logic with exponential backoff for critical operations
- OpenTelemetry tracing integration for observability
Session Management:
- Support for custom sessions with git credentials and environment variables
- Browser session creation for web-based sandbox interactions
- Automatic session cleanup and disposal
Resource Management:
- VM tier scaling and hibernation timeout management
- Cluster and bootup type tracking
- Agent version management and update detection