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 18 or later is installed.
For SDK version requirements, supported regions, and endpoint rules, see Usage Constraints.
1. Configure environment variables
Pass the API key and endpoint through environment variables. Do not hard-code them in source code.
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:
python3 -m venv .venv
source .venv/bin/activate
pip install e2b==2.31.0 e2b-code-interpreter==2.8.1TypeScript:
npm init -y
npm install e2b@^2.31.0 @e2b/code-interpreter@^2.8.1 dotenv
npm install -D tsx typescript @types/node3. 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:
python quickstart.pyTypeScript
Create quickstart.ts:
import "dotenv/config";
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.tsIf 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.
To learn more about the sandbox lifecycle, running commands, and reading or writing files, see Lifecycle, Run Commands, and Read and Write Files.