@daytona/sdk
v0.198.0
Published
Daytona TypeScript SDK for sandbox management and code execution
Readme
Daytona TypeScript SDK
The official TypeScript SDK for Daytona, a secure and elastic infrastructure for running AI-generated code. Daytona provides full composable computers — sandboxes — that you can manage programmatically using the Daytona SDK.
The SDK provides an interface for sandbox management, file system operations, Git operations, language server protocol support, process and code execution, and computer use. For more information, see the documentation.
Installation
Install the package using npm:
npm install @daytona/sdkor using yarn:
yarn add @daytona/sdkGet API key
Generate an API key from the Daytona Dashboard ↗ to authenticate SDK requests and access Daytona services. For more information, see the API keys documentation.
Configuration
Configure the SDK using environment variables or by passing a configuration object:
DAYTONA_API_KEY: Your Daytona API keyDAYTONA_API_URL: The Daytona API URLDAYTONA_TARGET: Your target region environment (e.g.us,eu)
import { Daytona } from '@daytona/sdk'
// Initialize with environment variables
const daytona = new Daytona();
// Initialize with configuration object
const daytona = new Daytona({
apiKey: 'YOUR_API_KEY',
apiUrl: 'YOUR_API_URL',
target: 'us',
});Create a sandbox
Create a sandbox to run your code securely in an isolated environment.
import { Daytona } from '@daytona/sdk'
const daytona = new Daytona({apiKey: "YOUR_API_KEY"});
const sandbox = await daytona.create({
language: 'typescript'
});
const response = await sandbox.process.codeRun('console.log("Hello World!")');
console.log(response.result);Examples and guides
Daytona provides examples and guides for common sandbox operations, best practices, and a wide range of topics, from basic usage to advanced topics, showcasing various types of integrations between Daytona and other tools.
Create a sandbox with custom resources
Create a sandbox with custom resources (CPU, memory, disk).
import { Daytona, Image } from '@daytona/sdk';
const daytona = new Daytona();
const sandbox = await daytona.create({
image: Image.debianSlim('3.12'),
resources: { cpu: 2, memory: 4, disk: 8 }
});Create an ephemeral sandbox
Create an ephemeral sandbox that is automatically deleted when stopped.
import { Daytona } from '@daytona/sdk';
const daytona = new Daytona();
const sandbox = await daytona.create({
ephemeral: true,
autoStopInterval: 5
});Create a sandbox from a snapshot
Create a sandbox from a snapshot.
import { Daytona } from '@daytona/sdk';
const daytona = new Daytona();
const sandbox = await daytona.create({
snapshot: 'my-snapshot-name',
language: 'typescript'
});Execute commands
Execute commands in the sandbox.
// Execute a shell command
const response = await sandbox.process.executeCommand('echo "Hello, World!"')
console.log(response.result)
// Run TypeScript code
const response = await sandbox.process.codeRun(`
const x = 10
const y = 20
console.log(\`Sum: \${x + y}\`)
`)
console.log(response.result)File operations
Upload, download, and search files in the sandbox.
// Upload a file
await sandbox.fs.uploadFile(Buffer.from('Hello, World!'), 'path/to/file.txt')
// Download a file
const content = await sandbox.fs.downloadFile('path/to/file.txt')
// Search for files
const matches = await sandbox.fs.findFiles(root_dir, 'search_pattern')Git operations
Clone, list branches, and add files to the sandbox.
// Clone a repository
await sandbox.git.clone('https://github.com/example/repo', 'path/to/clone')
// List branches
const branches = await sandbox.git.branches('path/to/repo')
// Add files
await sandbox.git.add('path/to/repo', ['file1.txt', 'file2.txt'])Language server protocol
Create and start a language server to get code completions, document symbols, and more.
// Create and start a language server
const lsp = await sandbox.createLspServer('typescript', 'path/to/project')
await lsp.start()
// Notify the lsp for the file
await lsp.didOpen('path/to/file.ts')
// Get document symbols
const symbols = await lsp.documentSymbols('path/to/file.ts')
// Get completions
const completions = await lsp.completions('path/to/file.ts', {
line: 10,
character: 15,
})