The official Python library for the computer API
Project description
Tzafon Python SDK
Remote browser and desktop automation. Control browsers and desktops programmatically through a simple Python API.
Installation
pip install tzafon
Quick Start
from tzafon import Computer
client = Computer(
api_key=os.environ.get("TZAFON_API_KEY"), # This is the default and can be omitted
)
computer = client.computers.create(
kind="browser",
)
print(computer.id)
While you can provide an api_key keyword argument,
we recommend using python-dotenv
to add TZAFON_API_KEY="My API Key" to your .env file
so that your API Key is not stored in source control.
Async usage
Simply import AsyncComputer instead of Computer and use await with each API call:
import os
import asyncio
from tzafon import AsyncComputer
client = AsyncComputer(
api_key=os.environ.get("TZAFON_API_KEY"), # This is the default and can be omitted
)
# Create and control a browser instance
with client.create(kind="browser") as computer:
computer.navigate("https://wikipedia.com")
result = computer.screenshot()
url = computer.get_screenshot_url(result)
print(f"Screenshot: {url}")
computer.click(100, 200)
computer.type("Hello, world!")
# Automatically terminates when exiting context
Set your API key:
export TZAFON_API_KEY="your-api-key"
Or use a .env file with python-dotenv.
Features
Session Management
- Context manager support for automatic cleanup
- Manual session control when needed
- Browser and desktop environments
Browser Actions
- Navigation:
navigate(url) - Mouse:
click(),double_click(),right_click(),drag() - Keyboard:
type(),hotkey() - Viewport:
scroll(),set_viewport() - Capture:
screenshot(),html() - Debug: Execute shell commands with
debug()
Advanced Features
- Multi-tab management
- Batch action execution
- Low-level input control (key_down/key_up, mouse_down/mouse_up)
- Proxy configuration
- Real-time streaming (events, screencast)
Type Safety
- Full type annotations for IDE autocomplete
- Pydantic models for responses
- TypedDict for request parameters
- Helper methods for result extraction
Examples
Browser Automation
with client.create(kind="browser") as computer:
# Navigate and interact
computer.navigate("https://github.com/login")
computer.click(300, 400)
computer.type("username")
computer.hotkey("Control", "a") # Select all
# Wait for page loads
computer.wait(2)
# Capture state
html_result = computer.html()
html = computer.get_html_content(html_result)
# Execute commands
debug_result = computer.debug("ls -la")
output = computer.get_debug_response(debug_result)
Manual Session Management
computer = client.create(kind="browser")
try:
computer.navigate("https://example.com")
screenshot = computer.screenshot()
finally:
computer.terminate()
Batch Actions
# Execute multiple actions in one request
result = client.computers.execute_batch(computer.id, actions=[
{"type": "go_to_url", "url": "https://example.com"},
{"type": "wait", "ms": 2000},
{"type": "click", "x": 100, "y": 200},
{"type": "type", "text": "search query"},
{"type": "screenshot"}
])
Low-Level Input Control
# Shift-click selection
computer.execute_action({"type": "key_down", "key": "Shift"})
computer.click(100, 200)
computer.click(100, 400)
computer.execute_action({"type": "key_up", "key": "Shift"})
Proxy Configuration
# Set proxy for the browser session
computer.execute_action({
"type": "change_proxy",
"proxy_url": "http://user:pass@proxy:port"
})
Multi-Tab Management
# Create new tab
result = computer.execute_action({
"type": "new_tab",
"url": "https://example.com"
})
tab_id = result.executed_tab_id
# List all tabs
tabs = computer.execute_action({"type": "list_tabs"})
# Switch to tab
computer.execute_action({"type": "switch_tab", "tab_id": tab_id})
# Close tab
computer.execute_action({"type": "close_tab", "tab_id": tab_id})
Async Support
from tzafon import AsyncComputer
async with AsyncComputer() as client:
computer = await client.computers.create(kind="browser")
await client.computers.navigate(computer.id, url="https://example.com")
await client.computers.terminate(computer.id)
Advanced Usage
Raw SDK Access
The simplified wrapper uses the generated SDK under the hood. You can access it directly:
# Low-level API
response = client.computers.create(kind="browser")
client.computers.navigate(response.id, url="https://example.com")
result = client.computers.capture_screenshot(response.id)
client.computers.terminate(response.id)
Error Handling
import tzafon
try:
with client.create(kind="browser") as computer:
computer.navigate("https://example.com")
except tzafon.RateLimitError:
print("Rate limit exceeded, back off")
except tzafon.AuthenticationError:
print("Invalid API key")
except tzafon.APIError as e:
print(f"API error: {e}")
Configuration
from tzafon import Computer
client = Computer(
api_key="your-api-key", # Or use TZAFON_API_KEY env var
base_url="https://...", # Optional: custom endpoint
timeout=120.0, # Request timeout in seconds
max_retries=2, # Retry failed requests
)
API Reference
Session Methods
navigate(url)- Navigate to URLclick(x, y)- Click at coordinatesdouble_click(x, y)- Double-clickright_click(x, y)- Right-clickdrag(x1, y1, x2, y2)- Click and dragtype(text)- Type texthotkey(*keys)- Press key combinationscroll(dx, dy, x, y)- Scroll viewport with starting coordinatesscreenshot(base64=False)- Capture screenshothtml(auto_detect_encoding=False)- Get page HTMLdebug(command, timeout_seconds=120, max_output_length=65536)- Run shell commandset_viewport(width, height, scale_factor=1.0)- Set viewport sizewait(seconds)- Wait for durationexecute_action(action)- Execute any actionterminate()- End session
Helper Methods
get_screenshot_url(result)- Extract screenshot URL from resultget_html_content(result)- Extract HTML from resultget_debug_response(result)- Extract command output from result
Low-Level Actions (via execute_action)
key_down,key_up- Hold/release keyboard keysmouse_down,mouse_up- Hold/release mouse buttonchange_proxy- Set browser proxynew_tab,switch_tab,close_tab,list_tabs- Tab management
Error Codes
| Status | Error Type |
|---|---|
| 400 | BadRequestError |
| 401 | AuthenticationError |
| 403 | PermissionDeniedError |
| 404 | NotFoundError |
| 422 | UnprocessableEntityError |
| 429 | RateLimitError |
| >=500 | InternalServerError |
Documentation
- REST API: docs.tzafon.ai
- Full API Reference: api.md
- Contributing: CONTRIBUTING.md
Requirements
Python 3.9+
License
See LICENSE
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tzafon-2.10.0.tar.gz.
File metadata
- Download URL: tzafon-2.10.0.tar.gz
- Upload date:
- Size: 114.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e5a72bfe2877c723a4c24790a311c1b0b888ed72cf8a669576eedeb099fdb0b
|
|
| MD5 |
960d8a302d16a97d61c8bc9e59f22bd2
|
|
| BLAKE2b-256 |
d6dc913fd578d31208dfcdcba246ce2f1f80c6ae6cc8dff98a40f8efa773eb31
|
File details
Details for the file tzafon-2.10.0-py3-none-any.whl.
File metadata
- Download URL: tzafon-2.10.0-py3-none-any.whl
- Upload date:
- Size: 104.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
489502f2d915a4529b6acabd09ee25ca131c292939f5ae1cba66d1824a097a73
|
|
| MD5 |
39c455d64d6cbdb91739228001bdeb2e
|
|
| BLAKE2b-256 |
74be2eeec42bbcdb70f5ee602c1b9eb7d48ba403c99979caa0d712aef48686d5
|