Set up
Install the CLI or an SDK:- TypeScript
- Python
- Go
- CLI
To install the SDK:Run the following command:Set
BASETEN_API_KEY to a Baseten API key
for SDK authentication.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.
- TypeScript
- Python
- Go
- CLI
- UI
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.- TypeScript
- Python
- Go
- CLI
- UI
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" }).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
urlfield, to:- Run commands.
- Read, write, and edit files.
url field the create or get call returns.
Get the URL before connecting:
- Python
- CLI
- Dashboard
To get the sandbox’s server URL:Run the following code:
- Python
- HTTP
- CLI
- MCP server
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.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.- TypeScript
- Python
- Go
- CLI
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.
- TypeScript
- Python
- Go
- CLI and cURL
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: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.- TypeScript
- Python
- Go
- CLI
- UI
To create a sandbox with environment variables:Run the following code:Set
secret: false for a value you want to read back later.**** 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:
--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 updateenvs, 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:
List sandboxes
- TypeScript
- Python
- Go
- CLI
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 withpurpose=agent. This example changes the purpose label
from hello-world to hello-universe:
- TypeScript
- Python
- Go
- CLI
- UI
To change a sandbox’s labels:Run the following code:
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.- TypeScript
- Python
- Go
- CLI
- UI
To delete a sandbox:Run the following code:
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:- TypeScript
- Python
- Go
- CLI
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.