Baseten Sandboxes are now available and free to use until November 16. Each organization can run up to 10 sandboxes at once. To raise this limit, contact support.
DEPLOYED, run commands and work with files
through its sandbox runtime API. The SDKs and
the CLI call it for you, and direct HTTP requests and MCP clients call it at
the sandbox’s URL.
The examples use a running sandbox named hello-world created from Base
Image, which includes Python. To create one, see
Create a sandbox. Before you start:
- Set up your SDK or the CLI. The SDK examples
read your API key from
BASETEN_API_KEY, and the CLI examples read the sandbox name fromSANDBOX_NAME. - For direct HTTP requests and MCP clients, request a
sandbox access token and set
SANDBOX_TOKEN.
Connect to a sandbox
Each sandbox serves its sandbox runtime API at its own URL. The CLI and SDKs look the URL up for you. Direct HTTP requests and MCP clients need it.Get the sandbox URL
The sandbox URL is theurl field that the create and get requests return.
For what each part of the URL means, see
Sandbox URLs.
To get the sandbox URL:
- TypeScript
- Python
- Go
- API
- CLI
- UI
Run the following code:
SANDBOX_URL to it for
the HTTP examples on this page.
Call the sandbox runtime API over HTTP
Send requests to the sandbox’s URL with a sandbox access token as a bearer token. To call the sandbox runtime API:-
Run the following command to send an authenticated request to the
sandbox:
-
Run the following command to download the OpenAPI spec for the API in that
sandbox, including its file and process operations:
401. For more
information, see the Sandbox runtime API reference.
Open a terminal
The CLI opens an interactive shell in the sandbox. The SDKs don’t open terminals; use them to run commands instead. To open an interactive terminal in the sandbox: Run the following command:Connect an MCP client
Each sandbox serves an MCP server at its URL followed by/mcp. An MCP client
connected to it gets tools for running commands and for reading, writing, and
editing files. This server is separate from the
Baseten MCP, which manages resources across your workspace.
To connect an MCP client to the sandbox:
Configure your MCP client with the following endpoint and authorization
header, replacing <SANDBOX_URL> with the sandbox’s URL and
<SANDBOX_TOKEN> with a sandbox access token:
Run a command
Run a command in the sandbox and wait for it to exit. The result includes the command’s standard output, standard error, and exit code. To check the sandbox’s Python version:- TypeScript
- Python
- Go
- API
- CLI
Run the following code:
DEPLOYED means the sandbox is ready, not that a task succeeded. Confirm a
task by its command’s exit code and output.
To add environment variables to one command:
These variables apply on top of the sandbox’s own
environment variables. Replace
worker.py with the path to your script in the sandbox.
- TypeScript
- Python
- Go
- API
- CLI
Update the previous example as follows:
Run a background process
Start a long-running command, such as a training script or a server, and return right away. Give the process a name so you can wait for it, read its output, or stop it later. A sandbox goes on standby when it stops receiving requests, which pauses its processes. Turn on keep-alive to keep the sandbox active while the process runs. To run a background process and wait for it:- TypeScript
- Python
- Go
- CLI
Replace To list the sandbox’s processes, call
worker.py with the path to your script in the sandbox, then run
the following code:sandbox.process.list(). To stop a
process, call sandbox.process.stop({ identifier: "worker" }), or
sandbox.process.kill({ identifier: "worker" }) to force it.baseten sandbox process and the
Sandbox runtime API reference.
Read and write files
Write a file into the sandbox and read it back. Files in a sandbox last until the sandbox is deleted or expires; see Local data. To write and read a file:- TypeScript
- Python
- Go
- API
- CLI
Run the following code:
Call an HTTP service in a sandbox
Start an HTTP service in the sandbox, then send requests to it through the sandbox runtime API’s/port/<PORT>/ route with a sandbox access token. Use
this route for your own code. To open an application in a browser or share it,
create a preview instead.
Serve your application on a port other than 8080, where the sandbox API
server listens, and 80, which the sandbox reserves for system services. The
examples use port 8000. The service must be listening before you send the
request.
To call an HTTP service in the sandbox:
- TypeScript
- Python
- Go
- API
- CLI
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.
Next steps
Preview a sandbox application
Open a running web application in a browser or share it.
Inspect sandbox logs
Read command output and the sandbox’s runtime logs.