Skip to main content

The official Python library for the computer API

Project description

Tzafon Python SDK

PyPI version

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()  # Reads TZAFON_API_KEY from environment

# 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, desktop, and code 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()

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

    # 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()

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 URL
  • click(x, y) - Click at coordinates
  • double_click(x, y) - Double-click
  • right_click(x, y) - Right-click
  • drag(x1, y1, x2, y2) - Click and drag
  • type(text) - Type text
  • hotkey(*keys) - Press key combination
  • scroll(dx, dy, x, y) - Scroll viewport with starting coordinates
  • screenshot(base64=False) - Capture screenshot
  • html(auto_detect_encoding=False) - Get page HTML
  • debug(command, timeout_seconds=120, max_output_length=65536) - Run shell command
  • set_viewport(width, height, scale_factor=1.0) - Set viewport size
  • terminate() - End session

Helper Methods

  • get_screenshot_url(result) - Extract screenshot URL from result
  • get_html_content(result) - Extract HTML from result
  • get_debug_response(result) - Extract command output from result

Error Codes

Status Error Type
400 BadRequestError
401 AuthenticationError
403 PermissionDeniedError
404 NotFoundError
422 UnprocessableEntityError
429 RateLimitError
≥500 InternalServerError

Documentation

Requirements

Python 3.9+

License

See LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tzafon-2.7.0.tar.gz (105.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tzafon-2.7.0-py3-none-any.whl (84.9 kB view details)

Uploaded Python 3

File details

Details for the file tzafon-2.7.0.tar.gz.

File metadata

  • Download URL: tzafon-2.7.0.tar.gz
  • Upload date:
  • Size: 105.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.9

File hashes

Hashes for tzafon-2.7.0.tar.gz
Algorithm Hash digest
SHA256 399911e9090e4f8c5bb8fe08a0da6eb6a38e1dea49829c8e24aac3b3851b30be
MD5 dd01e8ff9a29fc307282461182774a22
BLAKE2b-256 90a3f231dfb357cea2bda800affb20c5a2fd030a090258d3bb35aadfbb92358d

See more details on using hashes here.

File details

Details for the file tzafon-2.7.0-py3-none-any.whl.

File metadata

  • Download URL: tzafon-2.7.0-py3-none-any.whl
  • Upload date:
  • Size: 84.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.9

File hashes

Hashes for tzafon-2.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f7abc094dcf2d8c5c7db9b508930bcecbfbda2046b16ff1bb1dae809c2ba900
MD5 a4b4c844ae02b6e54caf7ca51f80dc8d
BLAKE2b-256 d4dc9b655f1207330dd8e09ea6d93b77f32d250f7ed1d81f8c633bde7f76e1c8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page