This guide walks you through creating your first FC Agent Sandbox with the E2B SDK and running a command in it.
Prerequisites
-
You have an Alibaba Cloud account and the required permissions to use Function Compute.
-
You have created an API key in the Function Compute console.
-
You have confirmed that FC Agent Sandbox is available in the target region and that your local network can access the service endpoint in that region.
-
For the Python example, Python 3.10 or later is installed.
-
For the TypeScript example, Node.js 20.18.1 or later is installed.
For SDK version requirements, supported regions, and endpoint rules, see Usage Constraints.
1. Configure environment variables
Pass the API key, endpoint, and domain through environment variables. Do not hard-code them in your code. For API key creation and management, see Create an API Key.
export E2B_API_KEY="<YOUR-API-KEY>"
export E2B_API_URL="https://api.<region>.e2b.fc.aliyuncs.com"
export E2B_DOMAIN="<region>.e2b.fc.aliyuncs.com"
Replace <region> with the region where FC Agent Sandbox is deployed.
2. Install the E2B SDK
Python
First, confirm that Python is version 3.10 or later, and then create and activate a virtual environment and install the E2B SDK:
python3 --versionpython3 -m venv .venv
source .venv/bin/activate
pip install e2b==2.31.0 e2b-code-interpreter==2.8.1
TypeScript
First, confirm that Node.js is version 20.18.1 or later, and then initialize the project and install the E2B SDK and its development dependencies:
node --versionnpm init -y
npm install e2b@^2.31.0 @e2b/code-interpreter@^2.6.1
npm install -D tsx typescript @types/node
3. Create and run a sandbox
Run either the Python or TypeScript example.
Python
Create quickstart.py:
import os
from e2b_code_interpreter import Sandbox
def require_env(name: str) -> str:
value = os.environ.get(name, "").strip()
if not value:
raise RuntimeError(f"Missing environment variable: {name}")
return value
sandbox = None
try:
sandbox = Sandbox.create(
template="code-interpreter-v1",
api_key=require_env("E2B_API_KEY"),
api_url=require_env("E2B_API_URL"),
domain=require_env("E2B_DOMAIN"),
)
result = sandbox.commands.run("python3 -c \"print('hello from sandbox')\"")
print(result.stdout.strip())
finally:
if sandbox is not None:
sandbox.kill()
Run:
python3 quickstart.py
TypeScript
Create quickstart.ts:
import { Sandbox } from "@e2b/code-interpreter";
async function main() {
const sandbox = await Sandbox.create("code-interpreter-v1", {
apiKey: process.env.E2B_API_KEY,
apiUrl: process.env.E2B_API_URL,
domain: process.env.E2B_DOMAIN,
timeoutMs: 300_000,
});
try {
const result = await sandbox.commands.run("python3 -c \"print('hello from sandbox')\"");
console.log(result.stdout.trim());
} finally {
await sandbox.kill();
}
}
main();
Run:
npx tsx quickstart.ts
If you see hello from sandbox, the SDK, API key, endpoint, domain, and built-in template are all working correctly.
Next steps
-
To view and manage sandboxes with the CLI, see Use FC Agent Sandbox with the CLI.
-
Learn more about Lifecycle, Run Commands, and Read and Write Files.