Skip to main content
Create a sandbox for an agent’s task, wait for it to deploy, and delete it when the work finishes. Use a Baseten SDK to manage sandboxes from your application. Each SDK creates and refreshes sandbox access tokens for you. Use the Baseten CLI or the dashboard to inspect and troubleshoot sandboxes by hand. You need permission to create resources in your team. For file, process, and network API definitions, see the Sandbox runtime API reference.

Set up

Install the CLI or an SDK:
To install the SDK:Run the following command:
Set BASETEN_API_KEY to a Baseten API key for SDK authentication.
Run the following command to set the sandbox name used by the CLI examples:
Sandbox commands are in pre-release. Arguments, flags, and output may change.

Create a sandbox

Creating a sandbox starts an isolated environment from an image. The new sandbox gets a name, an image, memory (which also sets its CPU allocation), and a region. Name, image, memory, and region can’t change after creation.
An organization can run up to 10 sandboxes at once. To raise this quota, contact Baseten support with your organization ID.
To create a sandbox:Run the following code:
The call waits until the sandbox is ready. To reuse a live sandbox with the same name, set createIfNotExists: true.

Get a sandbox

A sandbox’s record shows its configuration, URL, and current status.
To check a sandbox’s status:Run the following code:
To work with the sandbox, not just its record, use client.get({ name: "hello-world" }).
If the status is FAILED, inspect the sandbox’s logs before retrying. For the deployment and deletion statuses, see Lifecycle and expiration.

Interact with a running sandbox

You can interact with a sandbox in two ways:
  • Use the Management API, through the CLI or an SDK, to:
    • Create sandboxes.
    • List sandboxes.
    • Update sandboxes.
    • Delete sandboxes.
  • Use the sandbox’s own API, served by a server inside each sandbox at the sandbox’s url field, to:
    • Run commands.
    • Read, write, and edit files.
The sandbox’s server URL is the url field the create or get call returns. Get the URL before connecting:
To get the sandbox’s server URL:Run the following code:
Use the URL exactly as the API returns it. Direct HTTP calls and MCP clients need it, and the CLI and SDKs look it up for you: Anatomy of a sandbox server URL. In https://sbx-hello-world-abc123.us-pdx-1.b10.co, hello-world is the sandbox name, abc123 identifies the sandbox's team, and us-pdx-1 is the region. Anatomy of a sandbox server URL. In https://sbx-hello-world-abc123.us-pdx-1.b10.co, hello-world is the sandbox name, abc123 identifies the sandbox's team, and us-pdx-1 is the region. To connect to a sandbox: Connect with an interactive terminal, direct HTTP calls, an SDK, or an MCP client:
To write and read a file through the SDK:Run the following code:
For operations the SDK doesn’t wrap, sandbox.raw_api is the generated client, and client.sandbox_from_url(url=...) builds a sandbox from a URL you already have.
Applications that need tighter control than the MCP server’s tools define their own functions instead. The sandbox’s MCP server is separate from the Baseten MCP, which manages deployments and models across your workspace.

Run commands in a sandbox

Run a single command in a deployed sandbox. Its output streams to your terminal, and its exit code becomes the command’s exit code.
To check the sandbox’s Python version:Run the following code:
To add environment variables for one command:Replace worker.py with the path to your script in the sandbox, then update the example as follows:

Access an HTTP service

Start an HTTP service in a sandbox, then send requests through the sandbox API server’s /port/<PORT>/ route. The service must be listening before you send the request. Use a running hello-world sandbox created from an image that includes Python, such as Base Image. Set up your SDK or CLI. For the SDK examples, set BASETEN_API_KEY. For every tab, request a sandbox access token and set SANDBOX_TOKEN in your local environment. Direct HTTP requests need this token even when an SDK starts the service.
To call an HTTP service:Run the following code to start the server, wait until it listens on port 8000, send a request, and stop the server:
A successful request returns HTTP 200 with Python’s directory listing. Requests without the sandbox access token return HTTP 401.

Set environment variables

Environment variables pass configuration, such as an output format or log level, to the processes in a sandbox, so one image can serve different tasks. Set them at creation. Names begin with a letter or underscore and contain only letters, digits, and underscores. Don’t put your Baseten API key in a sandbox’s environment.
To create a sandbox with environment variables:Run the following code:
Set secret: false for a value you want to read back later.
Values are secret by default. Secret values are masked as **** when you read them back, so use them for credentials and tokens. For configuration you’ll want to see later, such as LOG_LEVEL or OUTPUT_FORMAT, mark the value as not secret so it stays readable. A describe shows the difference:
Secret masking protects values in ordinary configuration responses. Code running inside the sandbox can read the actual values, including an agent that can run commands. Only provide credentials that you’re willing to make available to that code. To reveal secret values in the configuration, run the following command:
Don’t include this output in logs or agent prompts. Don’t pass --plain-env or secret: false for credentials; those values remain readable in ordinary configuration responses.

Replace environment variables

Include every variable you want to keep when you update envs, including existing secrets and their actual values. SDK and API updates replace the entire environment variable set. The CLI’s --env and --plain-env update flags do the same. Don’t copy masked **** values from a configuration response. Omitting envs leaves the existing set unchanged. Set DB_PASSWORD in your local shell, then run the following command to replace the sandbox’s environment variables with one secret and one plain-text variable:
The update removes any sandbox variables you omit from the command.

List sandboxes

To list sandboxes:Run the following code:
Filter by status with statuses, such as statuses: ["DEPLOYED"].

Manage a sandbox’s labels

Labels help you find and group sandboxes, such as marking every sandbox an agent creates with purpose=agent. This example changes the purpose label from hello-world to hello-universe:
To change a sandbox’s labels:Run the following code:
A supplied set of labels replaces all previous labels, so include every one you want to keep. Omitted fields leave their values unchanged. You can also update environment variables with envs. The name, image, memory, and region can’t change after creation. To use a different image, create a new sandbox.

Delete a sandbox

Deletion stops the sandbox’s processes and permanently removes its files, so save the files you need first.
To delete a sandbox:Run the following code:
Deletion continues after the call returns. Stop sending work to the sandbox, then describe the sandbox until its status is TERMINATED. The sandbox reports TERMINATED for a few minutes, and then it’s no longer found. Don’t reuse the name until deletion completes.

Work in a team

If you belong to multiple teams, specify a team ID when you create an SDK client:
To select a team for SDK requests:Replace <TEAM_ID> with your team’s ID, then configure the client as follows:

Handle a failed command

Use the error message to choose the next action:

Next steps

Sandbox lifecycle and expiration

Track deployment status and set an expiration policy.

Manage sandbox images

Choose a built-in image, or create and version your own.