This topic explains the core concepts of the AgentBay software development kit (SDK). It shows you how to prepare your environment, configure authentication, and create and operate your first cloud session.
Environment setup
System requirements
Python
Python
3.10or later.pip or poetry
TypeScript/JavaScript
Node.js 14 or later.
npm or yarn
Golang
Go
1.24.4or later.
Quick installation
Python
Recommended: Use a virtual environment
# Create and activate virtual environment
python3 -m venv agentbay-env
source agentbay-env/bin/activate # Linux/macOS
# agentbay-env\Scripts\activate # Windows
# Install the package
pip install wuying-agentbay-sdk
# Verify installation
python -c "import agentbay; print('Installation successful')"Alternative: Use the system Python (if allowed)
# Install with user flag (if system allows)
pip install --user wuying-agentbay-sdk
# Verify installation
python -c "import agentbay; print('Installation successful')"TypeScript/JavaScript
# Initialize project (if new project)
mkdir my-agentbay-project && cd my-agentbay-project
npm init -y
# Install the package
npm install wuying-agentbay-sdk
# Verify installation
node -e "const {AgentBay} = require('wuying-agentbay-sdk'); console.log('Installation successful')"Golang
# Initialize module (if new project)
mkdir my-agentbay-project && cd my-agentbay-project
go mod init my-agentbay-project
# Install the package
GOPROXY=direct go get github.com/aliyun/wuying-agentbay-sdk/golang/pkg/agentbay
# Verify installation
go list -m github.com/aliyun/wuying-agentbay-sdk/golang && echo "Installation successful"API key setup
Get an API key
Go to the AgentBay console.
In the navigation pane on the left, choose Service Management and click the copy icon for the target API key.
NoteIf no API key is available, click Create API Key, enter a name, and click Confirm to create one.
Set environment variable
Linux/macOS:
export AGENTBAY_API_KEY=your_api_key_hereWindows:
setx AGENTBAY_API_KEY your_api_key_hereVerify the installation
Python
import os
from agentbay import AgentBay
# Get API key from environment
api_key = os.getenv("AGENTBAY_API_KEY")
if not api_key:
print("Please set AGENTBAY_API_KEY environment variable")
exit(1)
try:
# Initialize SDK
agent_bay = AgentBay(api_key=api_key)
print("SDK initialized successfully")
# Create a session (requires valid API key and network)
session_result = agent_bay.create()
if session_result.success:
session = session_result.session
print(f"Session created: {session.session_id}")
# Clean up
agent_bay.delete(session)
print("Test completed successfully")
else:
print(f"Session creation failed: {session_result.error_message}")
except Exception as e:
print(f"Error: {e}")TypeScript
import { AgentBay } from 'wuying-agentbay-sdk';
const apiKey = process.env.AGENTBAY_API_KEY;
if (!apiKey) {
console.log("Please set AGENTBAY_API_KEY environment variable");
process.exit(1);
}
async function test() {
try {
// Initialize SDK
const agentBay = new AgentBay({ apiKey });
console.log("SDK initialized successfully");
// Create a session (requires valid API key and network)
const sessionResult = await agentBay.create();
if (sessionResult.success) {
const session = sessionResult.session;
console.log(`Session created: ${session.sessionId}`);
// Clean up
await agentBay.delete(session);
console.log("Test completed successfully");
} else {
console.log(`Session creation failed: ${sessionResult.errorMessage}`);
}
} catch (error) {
console.log(`Error: ${error}`);
}
}
test();Golang
package main
import (
"fmt"
"os"
"github.com/aliyun/wuying-agentbay-sdk/golang/pkg/agentbay"
)
func main() {
// Get API key from environment
apiKey := os.Getenv("AGENTBAY_API_KEY")
if apiKey == "" {
fmt.Println("Please set AGENTBAY_API_KEY environment variable")
return
}
// Initialize SDK
client, err := agentbay.NewAgentBay(apiKey, nil)
if err != nil {
fmt.Printf("Failed to initialize SDK: %v\n", err)
return
}
fmt.Println("SDK initialized successfully")
// Create a session (requires valid API key and network)
sessionResult, err := client.Create(nil)
if err != nil {
fmt.Printf("Session creation failed: %v\n", err)
return
}
if sessionResult.Session != nil {
fmt.Printf("Session created: %s\n", sessionResult.Session.SessionID)
// Clean up
_, err = client.Delete(sessionResult.Session, false)
if err != nil {
fmt.Printf("Session cleanup failed: %v\n", err)
} else {
fmt.Println("Test completed successfully")
}
}
}Advanced configuration (Optional)
By default, the SDK uses the China (Shanghai) API Gateway. If you connect from other regions, such as Singapore, you can configure a different gateway for better network performance.
Supported API Gateway regions
The SDK configuration specifies which API Gateway to connect to. For the best network performance, choose the gateway closest to your location.
Gateway location | Endpoint |
(Default) China (Shanghai) |
|
Singapore |
|
Switch to the Singapore gateway
Linux/macOS:
export AGENTBAY_ENDPOINT=wuyingai.ap-southeast-1.aliyuncs.comWindows:
set AGENTBAY_ENDPOINT=wuyingai.ap-southeast-1.aliyuncs.comTroubleshooting
Python issues
externally-managed-environment error:
# Solution: Use virtual environment
python3 -m venv agentbay-env
source agentbay-env/bin/activate
pip install wuying-agentbay-sdkModuleNotFoundError: No module named 'agentbay':
# Check if virtual environment is activated
which python # Should show venv path
# Re-install if needed
pip install --force-reinstall wuying-agentbay-sdkTypeScript issues
Cannot find module 'wuying-agentbay-sdk':
# Ensure you're in the project directory with package.json
pwd
ls package.json # Should exist
# Re-install if needed
npm install wuying-agentbay-sdkrequire() is not defined:
# Check Node.js version (requires 14+)
node --version
# Ensure you're using CommonJS (default) or update to ES modulesGolang issues
checksum mismatch error (most common):
# Always use direct proxy for this package
GOPROXY=direct go get github.com/aliyun/wuying-agentbay-sdk/golang/pkg/agentbayIncorrect import path:
# Check Go version (requires 1.24.4+)
go version
# Ensure module is initialized
go mod init your-project-nameBuild failed:
# Clean module cache and retry
go clean -modcache
go mod tidy
go get github.com/aliyun/wuying-agentbay-sdk/golang/pkg/agentbayNetwork and API issues
Connection timeout:
Check your network connectivity.
Verify that the API Gateway endpoint is appropriate for your location.
If possible, try a different gateway endpoint for a better connection.
API key errors:
Verify that the API key is correct and valid.
Check the required API key permissions in the console.
Ensure the environment variable is set correctly.
Session creation failed:
Verify that your account has a sufficient quota.
Check the service status in the console.
Try again in a few minutes.
Key concepts
AgentBay class
The AgentBay class is the main interface for interacting with the cloud service. It includes the following key features:
Session manager: Creates, deletes, and manages cloud sessions.
API client: Handles all communication with the AgentBay cloud service.
Authentication handler: Automatically manages API keys and security.
Basic use pattern
# 1. Initialize client
agent_bay = AgentBay()
# 2. Create session (uses linux_latest by default)
session = agent_bay.create().session
# 3. Use session for your tasks
# ... your automation tasks ...
# 4. Clean up resources
agent_bay.delete(session)
Session
A session is a connection between a user and a cloud environment.
Key characteristics
Temporary: Sessions are created when needed and destroyed when finished.
Isolated: Each session is completely independent of other sessions.
Billed: You are billed for the time a session is active.
Basic use pattern
# Create a session
session = agent_bay.create().session
# Use the session for your tasks
session.command.execute_command("echo 'Hello World'")
# Always clean up when done
agent_bay.delete(session)
Session lifecycle
Create Session → Use Session → Delete Session
↓ ↓ ↓
Allocate Execute Release
Resources Operations ResourcesSession release
You must release a session when you are finished to free up cloud resources. There are two ways to release a session:
Manual release (Recommended)
# Explicitly delete when done
agent_bay.delete(session)
Automatic timeout release
If a session is not released manually, it is automatically released after a timeout period.
Go to the AgentBay console. In the navigation pane on the left, choose Policy Management.
Click Create Policy. Enable Release Inactive Desktop.
Enter the timeout durations for Release Desktop after MCP Interaction Terminates and Release Desktop after User Interaction Terminates.
Click Create Policy.
In the navigation pane on the left, choose Service Management. Find the target API key, and in the Actions column, click the ⋮ icon.
Choose View/Associate Policy. In the Associated Policy area, click Associate Policy. Select the policy you created and click Confirm Association.
NoteEach API key can be associated with only one policy. When an API key is created, it is automatically associated with the default system policy. You must first Dissociate the default policy.
Image types
Official OS images
The following table lists the latest official OS images provided by AgentBay.
Image ID | Environment | Suitable for |
| Cloud computer | General computing, server tasks (default if not specified) |
| Cloud computer | General Windows tasks, |
| Browser | Web scraping, browser automation, testing websites |
| Code sandbox | Coding, developer tools, programming tasks |
| Cloud Phone | Mobile app testing, Android automation |
If you do not specify an
image_id, AgentBay automatically useslinux_latestas the default environment.You can create and use custom images in the AgentBay console to meet specific needs.
Choose the right image
Windows environment example
from agentbay.session_params import CreateSessionParams
# Create Windows environment and automate notepad
params = CreateSessionParams(image_id="windows_latest")
session = agent_bay.create(params).session
# Start Notepad application
session.computer.start_app("notepad.exe")
# Returns: ProcessListResult with started process info
# Input text into notepad
session.computer.input_text("Hello from Windows!")
# Returns: BoolResult with success status
agent_bay.delete(session)
Browser environment example
# Create browser environment
params = CreateSessionParams(image_id="browser_latest")
session = agent_bay.create(params).session
# Initialize and navigate
from agentbay.browser import BrowserOption
session.browser.initialize(BrowserOption())
session.browser.agent.navigate("https://www.baidu.com")
print("Web navigation successful")
agent_bay.delete(session)Code sandbox environment example
# Create development environment and execute code
params = CreateSessionParams(image_id="code_latest")
session = agent_bay.create(params).session
# Execute code
result = session.code.run_code("print('Hello from CodeSpace!')", "python")
# Returns: CodeExecutionResult with output
# Example: result.result = "Hello from CodeSpace!"
agent_bay.delete(session)Cloud phone environment example
# Create Android environment and send HOME key
params = CreateSessionParams(image_id="mobile_latest")
session = agent_bay.create(params).session
# Press HOME key to return to home screen
from agentbay.mobile import KeyCode
session.mobile.send_key(KeyCode.HOME)
# Returns: BoolResult with success status
# Example: result.success = True (returns to Android home screen)
agent_bay.delete(session)
Learn more about each environment
Data persistence
Temporary data (default behavior)
By default, all data in a session is temporary.
All content is lost when the session ends.
This is suitable for processing tasks, temporary files, or caches.
# This data will be LOST when session ends
session.file_system.write_file("/tmp/temp_data.txt", "This will disappear")
Persistent data (Context)
You can retain data across sessions.
This feature must be configured explicitly.
This is suitable for project files, configurations, and important results.
from agentbay import ContextSync
# Create persistent storage
context = agent_bay.context.get("my-project", create=True).context
context_sync = ContextSync.new(context.id, "/tmp/persistent")
# Create session with persistent data
params = CreateSessionParams(context_syncs=[context_sync])
session = agent_bay.create(params).session
# This data will be SAVED across sessions
session.file_system.write_file("/tmp/persistent/important.txt", "This will persist")To save data, you must use a Context. Otherwise, the data will be permanently lost after the session ends.
API results and request IDs
API results
When you call an AgentBay API, the result is encapsulated in a result object.
# Example API call
screenshot = session.computer.screenshot()
# The result object contains:
print(screenshot.success) # True/False - whether the operation succeeded
print(screenshot.data) # Your actual data (screenshot URL)
print(screenshot.request_id) # Request ID for troubleshooting
Request ID
Each API call to AgentBay returns a unique request ID. The structure is similar to "ABC12345-XXXX-YYYY-ZZZZ-123456789ABC".
Purpose of the request ID
Troubleshooting: If a problem occurs, you can provide this ID to the support team for faster resolution.
Tracking: Helps track individual operations in trace logs.
Debugging: Makes it easier to identify which specific API call is causing a problem.
When to use a request ID
An API call fails unexpectedly.
Performance issues with a specific operation.
When you contact the support team about an issue.
Troubleshooting example
result = session.code.run_code("print('hello')", "python")
if not result.success:
print(f"Code execution failed! Request ID: {result.request_id}")
# Share this Request ID with support for faster help
Create a session
Quick verification
Use a simple example to verify that your configuration is correct.
import os
from agentbay import AgentBay
api_key = os.getenv("AGENTBAY_API_KEY")
agent_bay = AgentBay(api_key=api_key)
result = agent_bay.create()
if result.success:
session = result.session
cmd_result = session.command.execute_command("echo 'Hello from the cloud!'")
print(f"Cloud says: {cmd_result.output.strip()}")
agent_bay.delete(session)
else:
print(f"Failed: {result.error_message}")
# Expected output:
# Cloud says: Hello from the cloud!
Cloud data processing
The following code processes a data file in the cloud.
import os
from agentbay import AgentBay
agent_bay = AgentBay(api_key=os.getenv("AGENTBAY_API_KEY"))
result = agent_bay.create()
session = result.session
try:
# 1. Create a Python script for data processing
script_content = '''
import json
import sys
data = {
"students": [
{"name": "Alice", "scores": [85, 92, 88]},
{"name": "Bob", "scores": [78, 85, 80]},
{"name": "Charlie", "scores": [92, 95, 98]}
]
}
results = []
for student in data["students"]:
avg = sum(student["scores"]) / len(student["scores"])
results.append({
"name": student["name"],
"average": round(avg, 2),
"grade": "A" if avg >= 90 else "B" if avg >= 80 else "C"
})
print(json.dumps(results, indent=2))
'''
# 2. Upload script to cloud
session.file_system.write_file("/tmp/process_data.py", script_content)
print("Script uploaded to cloud")
# 3. Execute the script in cloud environment
result = session.command.execute_command("python3 /tmp/process_data.py")
print(f"\n Processing results:\n{result.output}")
# Expected output:
# [
# {"name": "Alice", "average": 88.33, "grade": "B"},
# {"name": "Bob", "average": 81.0, "grade": "B"},
# {"name": "Charlie", "average": 95.0, "grade": "A"}
# ]
print("\n What happened:")
print(" 1. Uploaded Python script to cloud environment")
print(" 2. Executed script with pre-installed Python")
print(" 3. Got results back - all without local setup!")
finally:
agent_bay.delete(session)
print("\n Session cleaned up")
# Expected output includes JSON formatted student grades