Getting Started
The Daytona SDK provides official Python and TypeScript interfaces for interacting with Daytona, enabling you to programmatically manage development environments and execute code.
Follow the step by step guide to create and run your first Daytona Sandbox for an AI Agent.
For steps on additional configuration, including setting environmnent variables as well as accessing experimental features on our staging deployment, visit Configuration.
Install the Daytona SDK
Daytona provides official Python and TypeScript SDKs for interacting with the Daytona platform. Install the SDK using your preferred method:
pip install daytona-sdk
# Using npmnpm install @daytonaio/sdk
# Using yarn
yarn add @daytonaio/sdk
# Using pnpm
pnpm add @daytonaio/sdk
Run Code Inside a Sandbox
Run the following code to create a Daytona Sandbox and execute commands:
from daytona_sdk import Daytona, DaytonaConfig, CreateSandboxParams
# Initialize the Daytona clientdaytona = Daytona(DaytonaConfig(api_key="YOUR_API_KEY"))
# Create the Sandbox instancesandbox = daytona.create(CreateSandboxParams(language="python"))
# Run code securely inside the Sandboxresponse = sandbox.process.code_run('print("Sum of 3 and 4 is " + str(3 + 4))')if response.exit_code != 0: print(f"Error running code: {response.exit_code} {response.result}")else: print(response.result)
# Clean up the Sandboxdaytona.remove(sandbox)
import { Daytona } from '@daytonaio/sdk'
async function main() { // Initialize the Daytona client const daytona = new Daytona({ apiKey: 'YOUR_API_KEY', })
let sandbox; try { // Create the Sandbox instance sandbox = await daytona.create({ language: "python", }); // Run code securely inside the Sandbox const response = await sandbox.process.codeRun( 'print("Sum of 3 and 4 is " + str(3 + 4))' ); if (response.exitCode !== 0) { console.error("Error running code:", response.exitCode, response.result); } else { console.log(response.result); } } catch (error) { console.error("Sandbox flow error:", error); } finally { // Clean up the Sandbox if (sandbox) { await daytona.remove(sandbox); } }}
main().catch(console.error)
python main.py
npx tsx ./index.ts
Preview Your App
The following snippet uploads a file containing a simple Flask app to a Daytona Sandbox. The web server runs on port 3000
and is accessible through the provided preview URL:
from daytona_sdk import Daytona, DaytonaConfig, SessionExecuteRequest
daytona = Daytona(DaytonaConfig(api_key="YOUR_API_KEY"))
sandbox = daytona.create()
app_code = b'''from flask import Flask
app = Flask(__name__)
@app.route('/')def hello(): return """ <!DOCTYPE html> <html> <head> <title>Hello World</title> <link rel="icon" href="https://www.daytona.io/favicon.ico"> </head> <body style="display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #0a0a0a; font-family: Arial, sans-serif;"> <div style="text-align: center; padding: 2rem; border-radius: 10px; background-color: white; box-shadow: 0 4px 8px rgba(0,0,0,0.1);"> <img src="https://raw.githubusercontent.com/daytonaio/daytona/main/assets/images/Daytona-logotype-black.png" alt="Daytona Logo" style="width: 180px; margin: 10px 0px;"> <p>This web app is running in a Daytona sandbox!</p> </div> </body> </html> """
if __name__ == '__main__': app.run(host='0.0.0.0', port=3000)'''
# Save the Flask app to a file
sandbox.fs.upload_file(sandbox.get_user_root_dir() + "/app.py", app_code)
# Create a new session and execute a command
exec_session_id = "python-app-session"sandbox.process.create_session(exec_session_id)
sandbox.process.execute_session_command(exec_session_id, SessionExecuteRequest( command="python " + sandbox.get_user_root_dir() + "/app.py", var_async=True))
# Get the preview link for the Flask app
preview_link = sandbox.get_preview_link(3000)print(f"Flask app is available at: {preview_link}")
import { Daytona } from '@daytonaio/sdk';
const daytona = new Daytona(({ apiKey: "YOUR_API_KEY"}));
async function main() { const sandbox = await daytona.create();
const appCode = `from flask import Flask
app = Flask(__name__)
@app.route('/')def hello(): return """ <!DOCTYPE html> <html> <head> <title>Hello World</title> <link rel="icon" href="https://www.daytona.io/favicon.ico"> </head> <body style="display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #0a0a0a; font-family: Arial, sans-serif;"> <div style="text-align: center; padding: 2rem; border-radius: 10px; background-color: white; box-shadow: 0 4px 8px rgba(0,0,0,0.1);"> <img src="https://raw.githubusercontent.com/daytonaio/daytona/main/assets/images/Daytona-logotype-black.png" alt="Daytona Logo" style="width: 180px; margin: 10px 0px;"> <p>This web app is running in a Daytona sandbox!</p> </div> </body> </html> """
if __name__ == '__main__': app.run(host='0.0.0.0', port=3000) `;
const fileContent = new File([appCode], "app.py")
let rootDir = await sandbox.getUserRootDir();
// Save the Flask app to a file await sandbox.fs.uploadFile(rootDir + "/app.py", fileContent);
// Create a new session and execute a command const execSessionId = "python-app-session"; await sandbox.process.createSession(execSessionId);
await sandbox.process.executeSessionCommand(execSessionId, ({ command: `python ${rootDir}/app.py`, async: true, }));
// Get the preview link for the Flask app const previewLink = sandbox.getPreviewLink(3000); console.log(`Flask app is available at: ${previewLink}`);}
main().catch(error => console.error("Error:", error));
Connect to an LLM
The following snippet connects to an LLM using the Anthropic API and asks Claude a question by running Python code directly:
from daytona_sdk import Daytona, DaytonaConfig
daytona = Daytona(DaytonaConfig(api_key="YOUR_DAYTONA_API_KEY"))
sandbox = daytona.create()
app_code = '''import requests
def get_claude_response(api_key, prompt): url = "<https://api.anthropic.com/v1/messages>" headers = { "x-api-key": api_key, "anthropic-version": "2023-06-01", "Content-Type": "application/json" } data = { "model": "claude-2", "max_tokens": 256, "messages": [{"role": "user", "content": prompt}] } response = requests.post(url, json=data, headers=headers) if response.status_code == 200: content = response.json().get("content", []) return "".join([item["text"] for item in content if item["type"] == "text"]) else: return f"Error {response.status_code}: {response.text}"
if __name__ == "__main__": api_key = "YOUR_ANTHROPIC_API_KEY" prompt = "Hello Claude, how are you today?" result = get_claude_response(api_key, prompt) print("Claude's response:", result)'''
# Run Python code inside the Sandbox and get the output
response = sandbox.process.code_run(app_code)print(response.result)
import { Daytona } from '@daytonaio/sdk';
const daytona = new Daytona({ apiKey: "YOUR_DAYTONA_API_KEY"});
async function main() { const sandbox = await daytona.create();
const appCode = `import requests
def get_claude_response(api_key, prompt): url = "https://api.anthropic.com/v1/messages" headers = { "x-api-key": api_key, "anthropic-version": "2023-06-01", "Content-Type": "application/json" } data = { "model": "claude-2", "max_tokens": 256, "messages": [{"role": "user", "content": prompt}] }
response = requests.post(url, json=data, headers=headers) if response.status_code == 200: content = response.json().get("content", []) return "".join([item["text"] for item in content if item["type"] == "text"]) else: return f"Error {response.status_code}: {response.text}"
if __name__ == "__main__": api_key = "YOUR_ANTHROPIC_API_KEY" prompt = "Hello Claude, how are you today?" result = get_claude_response(api_key, prompt) print("Claude's response:", result)`;
// Run Python code inside the Sandbox and get the output const response = await sandbox.process.codeRun(appCode); console.log(response.result);}
main().catch(console.error);
Additional Examples
Use the Daytona SDK Python examples or TypeScript/JavaScript examples to create a Sandbox and run your code.
Learn more by checkout out the Daytona SDK repository on GitHub.
Setting up the Daytona CLI
If you want to use images from your local device or simply prefer managing your Sandboxes using the command line interface, install the Daytona CLI by running:
brew install daytonaio/cli/daytona
powershell -Command "irm https://get.daytona.io/windows | iex"