|
1 | 1 | # Hyperbrowser Python SDK |
2 | 2 |
|
3 | | -Checkout the full documentation [here](https://hyperbrowser.ai/docs) |
| 3 | +Python SDK for the Hyperbrowser API. |
4 | 4 |
|
5 | | -## Installation |
| 5 | +- Full docs: https://hyperbrowser.ai/docs |
| 6 | +- Package: https://pypi.org/project/hyperbrowser/ |
6 | 7 |
|
7 | | -Currently Hyperbrowser supports creating a browser session in two ways: |
| 8 | +## Requirements |
8 | 9 |
|
9 | | -- Async Client |
10 | | -- Sync Client |
| 10 | +- Python `>=3.9` |
11 | 11 |
|
12 | | -It can be installed from `pypi` by running : |
| 12 | +## Installation |
13 | 13 |
|
14 | | -```shell |
| 14 | +```bash |
15 | 15 | pip install hyperbrowser |
16 | 16 | ``` |
17 | 17 |
|
18 | 18 | ## Configuration |
19 | 19 |
|
20 | | -Both the sync and async client follow similar configuration params |
| 20 | +You can pass credentials directly, or use environment variables. |
| 21 | + |
| 22 | +```bash |
| 23 | +export HYPERBROWSER_API_KEY="your_api_key" |
| 24 | +export HYPERBROWSER_BASE_URL="https://api.hyperbrowser.ai" # optional |
| 25 | +``` |
| 26 | + |
| 27 | +## Clients |
21 | 28 |
|
22 | | -### API Key |
23 | | -The API key can be configured either from the constructor arguments or environment variables using `HYPERBROWSER_API_KEY` |
| 29 | +The SDK provides both sync and async clients with mirrored APIs: |
24 | 30 |
|
25 | | -## Usage |
| 31 | +- `Hyperbrowser` (sync) |
| 32 | +- `AsyncHyperbrowser` (async) |
26 | 33 |
|
27 | | -### Async |
| 34 | +### Sync quickstart |
| 35 | + |
| 36 | +```python |
| 37 | +from hyperbrowser import Hyperbrowser |
| 38 | + |
| 39 | +with Hyperbrowser(api_key="your_api_key") as client: |
| 40 | + session = client.sessions.create() |
| 41 | + print(session.id, session.ws_endpoint) |
| 42 | + client.sessions.stop(session.id) |
| 43 | +``` |
| 44 | + |
| 45 | +### Async quickstart |
28 | 46 |
|
29 | 47 | ```python |
30 | 48 | import asyncio |
31 | | -from pyppeteer import connect |
32 | 49 | from hyperbrowser import AsyncHyperbrowser |
33 | 50 |
|
34 | | -HYPERBROWSER_API_KEY = "test-key" |
35 | | - |
36 | | -async def main(): |
37 | | - async with AsyncHyperbrowser(api_key=HYPERBROWSER_API_KEY) as client: |
| 51 | +async def main() -> None: |
| 52 | + async with AsyncHyperbrowser(api_key="your_api_key") as client: |
38 | 53 | session = await client.sessions.create() |
| 54 | + print(session.id, session.ws_endpoint) |
| 55 | + await client.sessions.stop(session.id) |
39 | 56 |
|
40 | | - ws_endpoint = session.ws_endpoint |
41 | | - browser = await connect(browserWSEndpoint=ws_endpoint, defaultViewport=None) |
| 57 | +asyncio.run(main()) |
| 58 | +``` |
42 | 59 |
|
43 | | - # Get pages |
44 | | - pages = await browser.pages() |
45 | | - if not pages: |
46 | | - raise Exception("No pages available") |
| 60 | +## Main manager surface |
47 | 61 |
|
48 | | - page = pages[0] |
| 62 | +Both clients expose: |
49 | 63 |
|
50 | | - # Navigate to a website |
51 | | - print("Navigating to Hacker News...") |
52 | | - await page.goto("https://news.ycombinator.com/") |
53 | | - page_title = await page.title() |
54 | | - print("Page title:", page_title) |
| 64 | +- `client.sessions` |
| 65 | +- `client.scrape` (+ `client.scrape.batch`) |
| 66 | +- `client.crawl` |
| 67 | +- `client.extract` |
| 68 | +- `client.web` (+ `client.web.batch_fetch`, `client.web.crawl`) |
| 69 | +- `client.agents` (`browser_use`, `cua`, `claude_computer_use`, `gemini_computer_use`, `hyper_agent`) |
| 70 | +- `client.profiles` |
| 71 | +- `client.extensions` |
| 72 | +- `client.team` |
| 73 | +- `client.computer_action` |
55 | 74 |
|
56 | | - await page.close() |
57 | | - await browser.disconnect() |
58 | | - await client.sessions.stop(session.id) |
59 | | - print("Session completed!") |
| 75 | +## Job polling (`start_and_wait`) |
60 | 76 |
|
61 | | -# Run the asyncio event loop |
62 | | -asyncio.get_event_loop().run_until_complete(main()) |
63 | | -``` |
64 | | -### Sync |
| 77 | +Long-running APIs expose `start_and_wait(...)`. |
| 78 | + |
| 79 | +These methods now support explicit polling controls: |
| 80 | + |
| 81 | +- `poll_interval_seconds` (default `2.0`) |
| 82 | +- `max_wait_seconds` (default `600.0`) |
| 83 | + |
| 84 | +Example: |
65 | 85 |
|
66 | 86 | ```python |
67 | | -from playwright.sync_api import sync_playwright |
68 | 87 | from hyperbrowser import Hyperbrowser |
| 88 | +from hyperbrowser.models import StartExtractJobParams |
| 89 | + |
| 90 | +with Hyperbrowser(api_key="your_api_key") as client: |
| 91 | + result = client.extract.start_and_wait( |
| 92 | + StartExtractJobParams( |
| 93 | + urls=["https://hyperbrowser.ai"], |
| 94 | + prompt="Extract the main headline", |
| 95 | + ), |
| 96 | + poll_interval_seconds=1.5, |
| 97 | + max_wait_seconds=300, |
| 98 | + ) |
| 99 | + print(result.status, result.data) |
| 100 | +``` |
69 | 101 |
|
70 | | -HYPERBROWSER_API_KEY = "test-key" |
| 102 | +## Error handling |
71 | 103 |
|
72 | | -def main(): |
73 | | - client = Hyperbrowser(api_key=HYPERBROWSER_API_KEY) |
74 | | - session = client.sessions.create() |
| 104 | +SDK errors are raised as `HyperbrowserError`. |
75 | 105 |
|
76 | | - ws_endpoint = session.ws_endpoint |
77 | | - |
78 | | - # Launch Playwright and connect to the remote browser |
79 | | - with sync_playwright() as p: |
80 | | - browser = p.chromium.connect_over_cdp(ws_endpoint) |
81 | | - context = browser.new_context() |
82 | | - |
83 | | - # Get the first page or create a new one |
84 | | - if len(context.pages) == 0: |
85 | | - page = context.new_page() |
86 | | - else: |
87 | | - page = context.pages[0] |
88 | | - |
89 | | - # Navigate to a website |
90 | | - print("Navigating to Hacker News...") |
91 | | - page.goto("https://news.ycombinator.com/") |
92 | | - page_title = page.title() |
93 | | - print("Page title:", page_title) |
94 | | - |
95 | | - page.close() |
96 | | - browser.close() |
97 | | - print("Session completed!") |
98 | | - client.sessions.stop(session.id) |
| 106 | +```python |
| 107 | +from hyperbrowser import Hyperbrowser |
| 108 | +from hyperbrowser.exceptions import HyperbrowserError |
99 | 109 |
|
100 | | -# Run the asyncio event loop |
101 | | -main() |
| 110 | +try: |
| 111 | + with Hyperbrowser(api_key="invalid") as client: |
| 112 | + client.team.get_credit_info() |
| 113 | +except HyperbrowserError as exc: |
| 114 | + print(exc) |
102 | 115 | ``` |
| 116 | + |
| 117 | +## Development |
| 118 | + |
| 119 | +```bash |
| 120 | +pip install -e . pytest ruff build |
| 121 | +python -m ruff check . |
| 122 | +python -m pytest -q |
| 123 | +python -m build |
| 124 | +``` |
| 125 | + |
103 | 126 | ## License |
104 | 127 |
|
105 | | -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 128 | +MIT — see [LICENSE](LICENSE). |
0 commit comments