Execute code in isolated, ephemeral sandbox environments
The Sandbox API provides secure, isolated environments for executing shell commands.
Each sandbox is a temporary container that runs independently, perfect for running untrusted code or testing scripts.
Use the Disk API to persist files and transfer them to/from sandboxes. This enables workflows where you prepare files, process them in a sandbox, and save the results.
1
Create a disk and sandbox
Set up both a disk for persistent storage and a sandbox for execution.
Copy
# Create a disk for file storagedisk = client.disks.create()print(f"Disk ID: {disk.id}")# Create a sandbox for executionsandbox = client.sandboxes.create()print(f"Sandbox ID: {sandbox.sandbox_id}")
2
Upload a file to disk
Upload a file to your disk that you want to process in the sandbox.
Copy
from acontext import FileUpload# Upload a script to the diskartifact = client.disks.artifacts.upsert( disk.id, file=FileUpload( filename="data.txt", content=b"Hello from disk!\nProcess this in sandbox." ), file_path="/input/")print(f"Uploaded: {artifact.path}{artifact.filename}")
3
Download file to sandbox
Transfer the file from disk to the sandbox filesystem.
Copy
# Download artifact to sandboxsuccess = client.disks.artifacts.download_to_sandbox( disk_id=disk.id, file_path="/input/", filename="data.txt", sandbox_id=sandbox.sandbox_id, sandbox_path="/workspace/")print(f"Download success: {success}")# Verify the file exists in sandboxresult = client.sandboxes.exec_command( sandbox_id=sandbox.sandbox_id, command="cat /workspace/data.txt")print(f"File content: {result.stdout}")
The file is copied to the sandbox at the specified path. The original filename is preserved.
4
Process and create output
Run commands in the sandbox to process files and generate output.
Copy
# Process the file and create outputclient.sandboxes.exec_command( sandbox_id=sandbox.sandbox_id, command="wc -l /workspace/data.txt > /workspace/result.txt")# Verify the outputresult = client.sandboxes.exec_command( sandbox_id=sandbox.sandbox_id, command="cat /workspace/result.txt")print(f"Result: {result.stdout}")
5
Upload result to disk
Save the processed output from the sandbox back to persistent disk storage.
Copy
# Upload the result from sandbox to diskuploaded = client.disks.artifacts.upload_from_sandbox( disk_id=disk.id, sandbox_id=sandbox.sandbox_id, sandbox_path="/workspace/", sandbox_filename="result.txt", file_path="/output/")print(f"Saved: {uploaded.path}{uploaded.filename}")# Verify by reading from diskartifact_info = client.disks.artifacts.get( disk.id, file_path="/output/", filename="result.txt", with_content=True)print(f"Persisted content: {artifact_info.content.raw}")
Your processed file is now safely stored on the disk and will persist after the sandbox is terminated.
6
Clean up
Terminate the sandbox when processing is complete.
Copy
# Kill the sandbox (disk persists)client.sandboxes.kill(sandbox.sandbox_id)print("Sandbox terminated, files preserved on disk")
The disk and its files remain available after the sandbox is killed. You can download them later or transfer to a new sandbox.