
Deno Sandbox provides ephemeral Linux microVMs that boot in milliseconds and are fully controllable via an SDK (JavaScript, TypeScript, Python) or REST API. Each sandbox runs in its own Firecracker microVM with complete isolation, making it ideal for executing untrusted code, building AI agent infrastructure, or creating secure development environments. Deno Deploy, which powers the Sandbox infrastructure, is built on Vultr and runs across Vultr Bare Metal, Cloud Compute, and Cloud GPU infrastructure globally.
This guide outlines how to use the @deno/sandbox TypeScript SDK to create isolated microVMs, execute shell commands, run JavaScript code, manage files, and expose HTTP services. It also demonstrates a practical use case by building a code execution agent that integrates with Vultr Serverless Inference.
Before you begin, you need to:
Deno provides a unified installation method that works across all Linux distributions, including Ubuntu, Debian, RHEL, Fedora, and Rocky. The installation uses a shell script that downloads and configures the Deno runtime on your system.
Download and execute the Deno installation script.
The script downloads the latest Deno release and installs it to $HOME/.deno/bin. The installer prompts you to automatically configure your PATH or enable shell completions. Accept these prompts.
Add the Deno installation directory to your Bash configuration.
Add the Deno binary path to your system PATH.
Apply the configuration changes to your current shell session.
Verify the installation.
The output displays the installed Deno, TypeScript, and V8 versions.
The @deno/sandbox SDK authenticates using an organization token from Deno Deploy. Generate this token from the dashboard.
Navigate to console.deno.com and sign in.
Select your organization or create a new one.
Open Settings from the top navigation bar.
Scroll down to the Organization Tokens section.
Click + Create a new token.
Enter a description for the token in the Description field.
Select an expiration period from the Expires In dropdown. Choose a duration based on your security requirements.
Click Create token.
Copy the generated token and store it securely. The token is only displayed once.
Export the token as an environment variable in your terminal.
Replace YOUR_ACCESS_TOKEN with the token you copied.
Create a new project directory and configure Deno to use the sandbox SDK.
Create a project directory.
Navigate into the project directory.
Add the @deno/sandbox package to your project.
This creates a deno.json file with the dependency configured.
A sandbox is an ephemeral Linux microVM that exists for the duration of your script. The await using syntax ensures the sandbox is automatically destroyed when the block exits.
Create a new file named main.ts.
Add the following code to create a sandbox and run a shell command.
Save and close the file.
This example demonstrates core sandbox functionality:
await using for automatic cleanup.uname -a inside the sandbox to retrieve system information.Run the script. The --allow-net and --allow-env flags grant network access for the Deploy API and environment variable access for authentication.
The output displays the sandbox ID and Linux kernel information from inside the microVM.
The sandbox provides a full Linux environment with common utilities pre-installed. Execute commands using the sh template literal or the spawn method for more control.
Create a file named shell_demo.ts.
Add the following code to demonstrate various shell operations.
Save and close the file.
This example demonstrates different shell command capabilities:
ls -la.Run the script.
The sandbox can execute JavaScript and TypeScript code directly using the deno.eval method. This is useful for running dynamically generated code or for building code-interpreter features.
Create a file named eval_demo.ts.
Add the following code to evaluate JavaScript expressions.
Save and close the file.
This code demonstrates three JavaScript evaluation scenarios:
map(), and returns a JSON string.Run the script.
The script executes successfully and displays the results. You may see "error: unexpected end of file" messages in the output. These are stderr messages from the sandbox environment and do not affect the execution or results.
The sandbox can run web servers and expose them to the public internet via secure HTTPS URLs. This enables live previews and webhook endpoints.
Create a file named http_demo.ts.
Add the following code to start a web server inside the sandbox and expose it publicly.
This example demonstrates HTTP service exposure:
Save and close the file.
Run the script.
The output displays a public URL. Open this URL in a web browser to access the server running inside the sandbox.
Customize the sandbox by specifying memory allocation, region, network restrictions, and environment variables.
Create a file named config_demo.ts.
Add the following code to demonstrate configuration options.
This configuration demonstrates several sandbox customization options:
Save and close the file.
Run the script.
This section demonstrates how to build a code execution agent that uses Vultr Serverless Inference to generate code and the Deno Sandbox to execute it safely.
Before you begin, provision a Vultr Serverless Inference subscription and retrieve the inference API key. The inference API key is different from your main Vultr account API key and is specific to each inference subscription.
Retrieve the list of available models from your Vultr Serverless Inference subscription. Replace YOUR_INFERENCE_API_KEY with the API key from your inference subscription.
Note one of the model IDs from the response to use in your code.
Create a file named ai_agent.ts.
Add the following code. Update MODEL_NAME with a model ID from the previous step.
Save and close the file.
This AI agent integrates Vultr Serverless Inference with Deno Sandbox through three main components:
generateCode function:
executeInSandbox function:
sandbox.deno.eval() and returns the captured output.Main execution:
Export your Vultr Serverless Inference API key as an environment variable. Replace YOUR_INFERENCE_API_KEY with your actual inference subscription API key.
This allows the code to read your API key from the environment variable without hardcoding it in the file.
Run the agent with a custom prompt.
The agent sends the prompt to Vultr Serverless Inference, receives generated code, and executes it safely in an isolated sandbox. You may see "error: unexpected end of file" messages in the output. These are stderr messages from the sandbox environment and do not affect the execution or results.
You have configured the Deno Sandbox SDK to create ephemeral Linux microVMs, execute shell commands, evaluate JavaScript code, manage files, and expose HTTP services. The sandbox provides a secure, isolated environment for running untrusted code, making it suitable for AI agents, code interpreters, and development platforms. For more information, refer to the official Deno Sandbox documentation.
0 Comments
Be the first to comment and share your perspective with the community.