Now let's experience the core features of AgentBay through actual code.
If you haven't completed the setup yet, please complete the quick setup steps:
👉 Installation and API Key Setup Guide - Complete SDK installation and API key configuration in 2 minutes
Already done? Great! Let's verify everything works with a quick test.
Let's first verify everything works with the simplest possible example:
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.run("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!If this works, you're ready to explore more! 🎉
Let's do something more useful - process 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.fs.write("/tmp/process_data.py", script_content)
print("✅ Script uploaded to cloud")
# 3. Execute the script in cloud environment
result = session.command.run("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 gradesThe AgentBay Workflow:
- Create - Get a fresh cloud environment (
agent_bay.create()) - Use - Execute commands, upload/download files
- Cleanup - Delete session to free resources (
agent_bay.delete())
Key Operations:
agent_bay.create()- Create a new cloud sessionsession.command.run()- Run shell commands (alias ofexecute_command())session.fs.write()- Upload/write text files (aliases offile_system.write_file())agent_bay.delete(session)- Clean up resources
This quickstart uses synchronous API for simplicity. If you're building a web app or need high concurrency, check out:
- 📖 Async API Guide - Complete async patterns and examples
Now that you've created your first session, explore more capabilities:
Learn Core Features:
- 📝 Command Execution - Run shell commands and code
- 📁 File Operations - Upload, download, and manage files
- 🔧 Session Management - Advanced session patterns and best practices
Explore Use Cases:
- 🌐 Browser Automation - Web scraping and testing
- 📱 Mobile Testing - Android app automation
- 💻 Code Development - Cloud development environment
Ready to build something amazing? Check out the Feature Guides to explore all capabilities! 🚀