Create a sandbox
Create a new sandbox using the Blaxel SDK by specifying a name, image to use, optional deployment region, and the ports to expose. Note that ports 80 (system), and 443 & 8080 (sandbox API) are reserved by Blaxel.Set up authentication to Blaxel
Set up authentication to Blaxel
The Blaxel SDK authenticates with your workspace using credentials from these sources, in priority order:
- when running on Blaxel, authentication is handled automatically
- variables in your
.env
file (BL_WORKSPACE
andBL_API_KEY
, or see this page for other authentication options). - environment variables from your machine
- configuration file created locally when you log in through Blaxel CLI (or deploy on Blaxel)
Images
The list of public images can be found here. To create a sandbox with one of those images, enter
blaxel/prod-{NAME}:latest
(e.g. blaxel/prod-base:latest).Ports
The following ports are reserved by Blaxel’s system:- 443: This port hosts the main sandbox API and is exposed via HTTPS
- 80: Reserved for system operations
- 8080: Reserved for sandbox API functionality
ports
parameter. This allows you to access these ports from outside the sandbox, typically to create a preview URL on them.
Expiration date
Set time-to-live (TTL) on the sandbox to automatically delete it after a specific period using thettl
parameter, or at a specific date using the expiresAt
/expires_at
parameter.
The ttl
parameter accepts a string with the following time units: s
(seconds), m
(minutes), h
(hours), d
(days), and w
(weeks).
This differs from the automatic shut down (scale-to-zero) which happens to all sandboxes when inactive and where the memory and filesystem are snapshotted to be resumed instantly.
Retrieve an existing sandbox
To reconnect to an existing sandbox, simply provide its name:Complete code examples demonstrating all operations are available on Blaxel’s GitHub: in TypeScript and in Python.
Create if not exists
This helper function either retrieves an existing sandbox or creates a new one if it doesn’t exist. Blaxel first checks for an existing sandbox with the providedname
and either retrieves it or creates a new one using your specified configuration.
Connect to a sandbox with a terminal
You can explore the contents of a sandbox with a terminal-like interface by running:
MCP server for a sandbox
Every sandbox is also exposed via an MCP server that allows agents to operate a sandbox using tool calls. The MCP server operates through WebSockets at the sandbox’s base URL:Tools available in the MCP server…
Tools available in the MCP server…
- Process management:
processExecute
- Execute a command.processGet
- Get process information by identifier (PID or name).processGetLogs
- Get logs for a specific process.processKill
- Kill a specific process.processStop
- Stop a specific process.processesList
- List all running processes.
- Filesystem operations
fsDeleteFileOrDirectory
- Delete a file or directory.fsGetWorkingDirectory
- Get the current working directory.fsListDirectory
- List contents of a directory.fsReadFile
- Read contents of a file.fsWriteFile
- Create or update a file.
- Tools specialized for code generation AI:
codegenEditFile
- Propose and apply a targeted edit to a specified file, with instructions and partial contents. This tool uses MorphLLM for fast edits, and requires a Morph API key set as an environment variable when creating the sandbox.codegenCodebaseSearch
- Find semantic code snippets from the codebase based on a natural language query.codegenFileSearch
- Fast fuzzy filename search in the project.codegenGrepSearch
- Run fast, exact regex/text searches on files for locating patterns or strings.codegenListDir
- List contents of a directory in the project.codegenParallelApply
- Plan and apply similar changes to multiple locations/files simultaneously.codegenReadFileRange
- Read a specific range of lines in a file (max 250 lines at once).codegenReapply
- Retry the application of the last edit, in case it previously failed.
Sandbox statuses
Blaxel sandboxes start fromstandby
to active
in under 25 milliseconds, and scale back down top standby
after a few seconds of inactivity, maintaining their previous state after scaling down.
Here is the summary on the possible statuses for a sandbox:
standby
: The sandbox is created but is hibernating. Sandboxes transition from standby to active mode in approximately 20 ms.- Cost: You are not charged for CPU/memory while a sandbox is in standby mode. However, you are charged for the storage of the snapshot and/or the volumes.
active
: The sandbox is running and processing tasks. You are charged for active runtime. Sandboxes automatically return to standby mode after 1 second of inactivity.- Cost: You are for CPU/memory and storage while a sandbox is in active mode.
stopped
: The sandbox is shut down and requires manual restart to access its API. This is useful if you want to deactivate a sandbox’s preview URLs.- Cost: You are not charged for CPU/memory while a sandbox is in stopped mode. However, you are charged for the storage of the snapshot and/or the volumes.
Processes and commands
Execute and manage processes in sandboxes.
File system operations
Manage directories and files in sandboxes.
Tools for AI codegen
Manage codebases with tools specifically designed for code generation.
Log streaming
Access logs generated in a sandbox.
Ports
Expose and connect to ports of a sandbox.
Preview in real-time
Render code in real-time via a direct preview URL.
Sessions
Manage temporary sessions to connect to sandboxes from a frontend client.
Volumes (private preview)
Attach volumes to sandboxes to persist files.
Templates
Create custom sandbox templates.
Sandbox API
Access the your sandbox with an HTTP REST API.