Skip to main content
This page covers the deprecated code sandbox SDK. Use the together-sandbox SDK for new integrations.
The legacy code sandbox SDK gives you a fully configurable development environment with fast start-up times, robust snapshotting, and a suite of mature dev tools. It can spin up a sandbox by cloning a template in under three seconds. Inside this VM, you can run any code, install any dependencies, and run servers. Under the hood, the SDK uses the microVM infrastructure of CodeSandbox to spin up sandboxes. It supports:
  • Memory snapshot and restore (checkpointing) at any point in time.
  • Resuming or cloning VMs from a snapshot in three seconds.
  • VM filesystem persistence, with git version control.
  • Environment customization using Docker and Docker Compose (Dev Containers).

Access

The legacy code sandbox is available on Together’s custom plans. A self-serve option is available by creating an account with CodeSandbox.
CodeSandbox is a Together company, and its products are migrating to the Together platform. The together-sandbox SDK is the successor to this offering. Within CodeSandbox.io, this offering is called the CodeSandbox SDK.

Get started

Install the SDK:
Then create an API token at codesandbox.io/t/api by selecting Create API Token. Use this token to authenticate with the SDK:
TypeScript

Sandbox lifecycle

By default a sandbox is created from a template. A template is a memory and filesystem snapshot of a sandbox, so a new sandbox is a direct continuation of the template. If the template was running a dev server, that dev server is running when the sandbox is created. When you create, resume, or restart a sandbox, you can read its bootupType to see how it started:
  • FORK: The sandbox was created from a template. This happens when you call create successfully.
  • RUNNING: The sandbox was already running when you called resume.
  • RESUME: The sandbox was resumed from hibernation.
  • CLEAN: The sandbox was created or resumed from scratch because it was not running and had no snapshot. This can happen if the sandbox was shut down or restarted, the snapshot expired, or something went wrong.

Manage CLEAN bootups

When a sandbox boots from scratch, the platform:
  1. Starts the Firecracker VM.
  2. Creates a default user, called pitcher-host.
  3. Builds the Docker image specified in .devcontainer/devcontainer.json, if one is configured.
  4. Starts the Docker container.
  5. Mounts the /project/sandbox directory as a volume inside the Docker container.
You can connect to the sandbox during this process and track its progress:
JavaScript

Use templates

Code sandbox has default templates that you can use to create sandboxes. These templates are available in the Template Library, and the “Universal” template is used by default. To create your own template, use the CodeSandbox CLI.

Create a template

Create a new folder in your project and add the files you want available inside your sandbox. For example, set up a Vite project:
Configure the template with tasks so that it installs dependencies and starts the dev server. Create a my-template/.codesandbox/tasks.json file with the following content:
The setupTasks run after the sandbox has started, before any other tasks. Deploy the template to the clusters:
The template is built with the Micro VM tier unless you pass --vmTier to the build command.
This creates sandboxes for each of the clusters, writes files, restarts, waits for port 5173 to be available, and then hibernates. This generates the snapshot that lets you quickly create sandboxes already running a dev server from the template. When all clusters are updated successfully, you get back a template tag to use when you create sandboxes:
JavaScript

Connect to sandboxes in the browser

In addition to running your sandbox on the server, you can connect it to the browser. This requires some collaboration with the server:
JavaScript
Then in the browser:
JavaScript
The browser session manages the connection automatically and reconnects if the connection is lost. This is controlled by an option called onFocusChange, and by default it reconnects when the page is visible:
JavaScript
If you tell the browser session when it is in focus, it reconnects automatically when hibernated, unless you explicitly disconnect the session. While the connectToSandbox promise is resolving, you can listen to initialization events to show a loading state:
JavaScript

Disconnect the sandbox

Disconnecting the session ends the session and automatically hibernates the sandbox after a timeout. You can also hibernate the sandbox explicitly from the server:
JavaScript

Pricing

The self-serve option for running the legacy code sandbox is priced according to the CodeSandbox SDK plans, which have two main pricing components:
  • VM credits: The unit of measurement for VM runtime. One credit equates to a specific amount of resources used per hour, depending on the specs of the VM you are using. VM credits follow a pay-as-you-go approach and are priced at $0.01486 per credit.
  • VM concurrency: The maximum number of VMs you can run simultaneously with the SDK. Each CodeSandbox plan has a different VM concurrency limit.
Minutes are the smallest unit of measurement for VM credits. If a VM runs for 3 minutes and 25 seconds, you are billed the equivalent of 4 minutes of VM runtime.

VM credit prices by VM size

The table below summarizes how many VM credits each VM size uses per hour of runtime. The Nano VM size is the default recommendation, as it provides enough resources for most simple workflows. Pico is mostly suitable for very simple code execution jobs.

Concurrent VMs

Pick the plan that matches how many concurrent VMs you require:
  • Build (free) plan: 10 concurrent VMs.
  • Scale plan: 250 concurrent VMs.
  • Enterprise plan: custom concurrent VMs.
If you expect a high volume of VM runtime, the Enterprise plan also provides discounts on VM credits. For enterprise pricing, contact sales.

Estimate your bill

To estimate your bill, you must consider:
  • The base price of your CodeSandbox plan.
  • The number of included VM credits on that plan.
  • How many VM credits you expect to require.
As an example, say you plan to run 80 concurrent VMs on average, each running 3 hours per day, every day, on the Nano VM size:
  • You need a Scale plan.
  • You use a total of 72,000 VM credits per month (80 VMs x 3 hours/day x 30 days x 10 credits/hour).
  • Your Scale plan includes 1,100 free VM credits each month, so you purchase 70,900 VM credits.
Based on this, your expected bill for that month is:
  • Base price of the Scale plan: $170.
  • Total price of VM credits: $1,053.57 (70,900 VM credits x $0.01486 per credit).
  • Total bill: $1,223.57.

Further reading

Learn more about sandbox configuration and features in the CodeSandbox SDK documentation.