Quick Start
Complete service authorization, create a template, generate an API Key, and make your first call via the console—launching your first sandbox within minutes.
Sandbox task path
Prerequisites
- You have activated Alibaba Cloud Bailian.
- Your current account has Sandbox operation permissions.
Step 1: Complete Service Authorization
When using Sandbox for the first time, complete the Service Linked Role (SLR) authorization so that Bailian can create and manage the sandbox runtime environment.
Navigate to Bailian Console > Sandbox. The "Alibaba Cloud Service Authorization" dialog appears the first time you create a template:
| Field | Value |
|---|---|
| Role name | AliyunServiceRoleForSFMSandbox |
| Role permission policy | AliyunServiceRolePolicyForSFMSandbox |
| Permission description | Allows Bailian to access sandbox compute and network resources to create, run, and release sandbox instances |
Select the Data Access Authorization Agreement, then click Confirm Authorization to complete this one-time authorization.
Step 2: Create a Template
A template defines the sandbox runtime environment. Go to Sandbox > My Templates, click Create Template, and configure the following fields:
| Field | Description |
|---|---|
| Image | Choose one of three: code-interpreter-v1 (code execution), browser (browser operations), all-in-one (combined capabilities) |
| Template name | Required, customizable |
| Resource specification | Required. Spec 1 (1 Core / 2 GB, recommended) or Spec 2 (4 Core / 8 GB) |
For file mounts, network allow/deny lists, environment variables, or lifecycle control, expand Advanced Configuration. Click Create when done. After creation, the template appears in the list; record its templateCode for later calls.
NoteIn lifecycle control, choose either idle timeout or maximum lifetime, up to 7 days.
Step 3: Get an API Key
Calls to the sandbox are authenticated with a Bailian API Key. Click API-KEY at the bottom left of the Sandbox page to create or copy an API Key (format sk-...).
Step 4: Make Your First Call
Sandbox is compatible with the E2B SDK. After installing the SDK, create an instance and execute code using the Bailian sandbox endpoint and API Key.
Install the SDK:
pip install "e2b==2.31.0"
# For Code Interpreter capabilities such as run_code
pip install "e2b-code-interpreter==2.8.1"
Before creating an instance, replace the example inputs below:
{workspace_id}: Follow Sandbox API prerequisites to view the actual workspace ID in the console's top-right menu, or see Get the workspace ID. The API key and template must belong to this workspace.ALIYUN_UID: Sign in to the Alibaba Cloud official website and view Account ID in the top-right profile menu. Replaceyour aliyunUidwith that UID.BAILIAN_API_KEY: Replace it with the actual API key obtained in step 3.template: Replaceyour template codewith thetemplateCoderecorded in step 2. You can select thecode-interpreter-v1image for this example.
Noteapi_key is a required parameter of the E2B SDK. It only needs to match the format e2b_ followed by hexadecimal characters; e2b_${ALIYUN_UID} is recommended. Bailian's real authentication uses Authorization: Bearer <Bailian API Key> and does not use api_key.
Create an instance and execute:
from e2b import Sandbox
ALIYUN_UID = "your aliyunUid" # Alibaba Cloud UID
BAILIAN_API_KEY = "sk-..." # Bailian API Key
sbx = Sandbox.create(
api_url="https://{workspace_id}.cn-beijing.maas.aliyuncs.com/api/v1/agentstudio/sandbox",
api_key=f"e2b_{ALIYUN_UID}", # Placeholder; only needs to match the E2B SDK format
headers={"Authorization": f"Bearer {BAILIAN_API_KEY}"}, # Bailian API Key for authentication
template="your template code",
)
# Run a command
result = sbx.commands.run("python --version")
print(result.stdout)
# Write and read a file
sbx.files.write("/home/user/workspace/hello.md", "hello bailian")
print(sbx.files.read("/home/user/workspace/hello.md"))
Noteresult.stdout reports the Python version inside the sandbox. Reading the file back prints hello bailian, letting you check command execution and file writing and reading. The Python version depends on the selected template's actual output.
After trying the example, follow Manage and Use Instances for instance lifecycle and resource release, accounting for the template's idle timeout or maximum lifetime when handling instances you no longer need. See Sandbox Billing for billing rules.