AgentBay Python SDK Examples
May 13, 2026 · View on GitHub
This directory contains comprehensive examples demonstrating various features of the AgentBay Python SDK.
Directory Structure
examples/
├── _async/ # Async examples (Source of Truth)
│ ├── browser-use/
│ ├── codespace/
│ ├── common-features/
│ ├── computer-use/
│ └── mobile-use/
└── _sync/ # Sync examples (Auto-generated)
├── browser-use/
├── codespace/
├── common-features/
├── computer-use/
└── mobile-use/
About Async/Sync Versions
_async/: Contains asynchronous examples usingAsyncAgentBayandasync/awaitsyntax_sync/: Contains synchronous examples usingAgentBay(auto-generated from async versions)
Both versions provide the same functionality. Choose based on your project requirements:
- Use async for modern async/await applications
- Use sync for traditional synchronous applications
Example Categories (in _async/ and _sync/ directories)
1. Browser-use Examples
Located in browser-use/browser/ and browser-use/extension/
Basic Browser Operations:
browser_screenshot.py- Taking screenshotsbrowser_viewport.py- Managing viewport sizesbrowser_type_example.py- Different browser types
Navigation and Interaction:
navigation_and_interaction.py- Browser navigation and element interactionpage_analysis.py- Page metadata and content extractionmulti_tab_management.py- Managing multiple browser tabs
Advanced Browser Features:
javascript_execution.py- Executing JavaScript in browser contextauthentication_flow.py- Handling authentication and login flowsresponsive_testing.py- Testing responsive design across viewportsnetwork_monitoring.py- Monitoring network requests and responsespopup_handling.py- Managing popups and dialogsiframe_handling.py- Working with iframes
File Operations:
file_upload_download.py- File upload/download operations
Browser Automation:
form_automation.py- Automated form fillingweb_scraping.py- Web scraping techniquescookies_management.py- Cookie managementlocal_storage_management.py- Local storage operationsscreenshot_comparison.py- Screenshot comparison
Browser Configuration:
browser_fingerprint_*.py- Browser fingerprintingbrowser_context_cookie_persistence.py- Cookie persistencebrowser-proxies.py- Proxy configurationbrowser_command_args.py- Browser command argumentsbrowser_replay.py- Browser session replay
Extension Development:
basic_extension_usage.py- Basic extension usageextension_development_workflow.py- Extension developmentextension_testing_automation.py- Extension testing
Real-world Examples:
search_agentbay_doc*.py- Documentation search examplesgame_*.py- Game automation examples
2. Codespace Examples
Located in codespace/
python_development.py- Python environment setup and package managementnodejs_development.py- Node.js environment and npm operationsgit_operations.py- Git repository initialization and commitsdatabase_operations.py- SQLite database operationstext_processing.py- Text manipulation with grep/sed/awksystem_monitoring.py- System resource monitoringfile_compression.py- File compression and archivingweb_server_setup.py- HTTP server setup and configurationbuild_automation.py- Build automation with Makefilescode_execution_example.py- Code execution patternsenhanced_code_execution.py- Advanced code execution with streamingjupyter_context_persistence.py- Jupyter-like Python context persistencejupyter_context_persistence_r_java.py- R/Java context persistencerun_code_streaming_beta.py- Streaming code execution (beta)
3. Common-features Examples
Located in common-features/basics/ and common-features/advanced/
Session Operations (basics/session_operations/):
session_creation.py- Creating sessions with various configurationssession_link_example.py- Getting session link URLssession_label_management.py- Managing session labels (CRUD + filtering)session_keep_alive.py- Keeping sessions alive
Session Management (basics/):
session_get/- Getting session by IDsession_pause_resume/- Pausing and resuming sessions
Command & PTY (basics/):
command_operations/- Command execution patternspty_operations/- PTY terminal operations
File System (basics/file_system/):
basic_file_operations_example.py- File read/write/list operationsfile_transfer_example.py- Session-based file upload/downloadwatch_directory_example.py- File change monitoring
Context & Data Persistence (basics/):
context_management/- Context CRUD, listing with pagination, session bindingcontext_file_transfer/- Context-based file transfer via presigned URLsdata_persistence/- SyncPolicy, RecyclePolicy, Archive mode, BWListdynamic_context_binding/- Dynamic context binding during session
Environment (basics/):
env_management/- Environment variable managementenvironment_operations/- Environment operations
Other (basics/):
mcp_tool_direct_call/- MCP tool direct calls
Advanced Features (advanced/):
agent_module/- Agent module usagebatch_operations/- Batch operationserror_handling/- Comprehensive error handlinglink_url_session/- Link URL session managementlogging_monitoring/- Logging and monitoringmulti_session_management/- Multi-session coordinationnetwork_testing/- Network diagnosticsoss_management/- OSS operationsparallel_execution/- Parallel execution patternsperformance_monitoring/- Performance monitoringretry_mechanism/- Retry patterns with circuit breakerscreenshot_download/- Screenshot operationssession_metrics/- Session metricssession_pooling/- Session pooling for efficiencyenvironment_variables/- Environment variable management
4. Computer-use Examples
Located in computer-use/computer/
screen_operations.py- Screenshot capture usingcomputer.beta_take_screenshot()windows_app_management_example.py- Windows application management
5. Mobile-use Examples
Located in mobile-use/
mobile_agent_streaming.py- Mobile Agent task execution with streaming outputmobile_app_operations.py- Mobile app operationsmobile_beta_screenshot.py- Mobile screenshot capturemobile_bounds_rect.py- Mobile UI bounds/rect operationsmobile_get_adb_url_example.py- Getting ADB connection URLmobile_simulate_basic_usage.py- Mobile simulate basic usagemobile_simulate_with_ctx.py- Mobile simulate with contextmobile_ui_automation.py- Mobile UI automationmobile_system/- Mobile system operationsget_all_ui_elements_xml/- Get all UI elements as XML
Getting Started
Prerequisites
pip install agentbay
Running Examples
Async version:
python python/docs/examples/_async/codespace/python_development.py
Sync version:
python python/docs/examples/_sync/codespace/python_development.py
Environment Variables
Most examples require the AGENTBAY_API_KEY environment variable:
export AGENTBAY_API_KEY="your_api_key_here"
Example Template
Async Example Template
import asyncio
from agentbay import AsyncAgentBay
from agentbay import CreateSessionParams
async def main():
client = AsyncAgentBay()
session = None
try:
# Create session
session_result = await client.create(
CreateSessionParams(image_id="linux_latest")
)
session = session_result.session
# Your code here
result = await session.command.execute_command("echo 'Hello'")
print(result.output)
finally:
if session:
await client.delete(session)
if __name__ == "__main__":
asyncio.run(main())
Sync Example Template
from agentbay import AgentBay
from agentbay import CreateSessionParams
def main():
client = AgentBay()
session = None
try:
# Create session
session_result = client.create(
CreateSessionParams(image_id="linux_latest")
)
session = session_result.session
# Your code here
result = session.command.execute_command("echo 'Hello'")
print(result.output)
finally:
if session:
client.delete(session)
if __name__ == "__main__":
main()
Contributing
When adding new examples:
- Always create async version first in
_async/directory - Run the generation script to create sync version:
cd python make generate-examples-sync - Verify both versions work correctly
- Update this README with the new example
Maintenance
The sync examples are auto-generated from async examples. To regenerate:
cd python
python scripts/generate_sync.py
Or use the Makefile:
cd python
make generate-examples-sync
Need Help?
- Check the API Documentation
- Visit the Guides
- See the Quickstart
License
These examples are part of the AgentBay SDK and follow the same license.