Get Started
Get started with Lybic Python SDK
This guide will walk you through the basics of installing and using the Lybic Python SDK.
Installation
First, install the package from PyPI:
pip install --upgrade lybic
Initialization
Next, initialize the client in your Python application. For better security, we recommend using environment variables (LYBIC_ORG_ID
, LYBIC_API_KEY
).
import asyncio
from lybic import LybicClient
# The LybicClient automatically picks up credentials from your environment
# def __init__(self,
# org_id: str = os.getenv("LYBIC_ORG_ID"),
# api_key: str = os.getenv("LYBIC_API_KEY"),
# endpoint: str = os.getenv("LYBIC_API_ENDPOINT", "https://api.lybic.cn"),
# timeout: int = 10,
# extra_headers: dict | None = None) -> None
async def main():
# Initialize with environment variables
async with LybicClient() as client:
pass
# Or, initialize with explicit credentials
async with LybicClient(
org_id="your_org_id", # Lybic organization ID
api_key="your_api_key", # Lybic API key
endpoint="https://api.lybic.cn", # Lybic API endpoint
timeout=10, # Timeout for API requests
extra_headers={"User-Agent": "MyAgent/1.0"}, # Custom headers
) as client:
pass
if __name__=='__main__':
asyncio.run(main())
Core Workflow
With the client initialized, the typical workflow follows these logical steps:
- Register (or be invited into) an Organization: Lybic allows you to register a new organization to manage your projects and resources.
- Create a
Project
: Projects are the primary way to organize your work. They act as containers for your sandboxes, team members, and secrets. - Launch a
Sandbox
: Within a project, you can launch a GUI sandbox. This is your agent's secure, cloud-based home. - Automate and Interact: Once the sandbox is running, your agent can begin its work. The SDK provides all the necessary tools to interact with the sandbox, from executing commands to capturing screenshots.
Hello World
Here is a simple example of creating a sandbox:
import asyncio
from lybic import LybicClient, Sandbox
async def main():
async with LybicClient() as client:
sandbox = Sandbox(client)
new_sandbox = await sandbox.create(name="my-sandbox")
print(new_sandbox)
if __name__ == '__main__':
asyncio.run(main())
Exception Handling
For the robustness of your code, all exceptions need to be handled by you. Wrap your SDK calls in a try...except
block:
import asyncio
from lybic import LybicClient, Sandbox
async def main():
async with LybicClient() as client:
try:
preview_result = await sandbox.preview('sandbox_id')
print(preview_result)
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == '__main__':
asyncio.run(main())