Hyperbrowser Python SDK
Checkout the full documentation here
Installation
Currently Hyperbrowser supports creating a browser session in two ways:
It can be installed from pypi by running :
pip install hyperbrowser
The browser-control examples below also use Playwright:
pip install playwright
Configuration
Both the sync and async client follow similar configuration params
API Key
The API key can be configured either from the constructor arguments or environment variables using HYPERBROWSER_API_KEY
If no API key is provided, the client falls back to a saved OAuth session created by hx auth login. By default it reads ~/.hx_config/auth/default.json, or ~/.hx_config/auth/<profile>.json when HYPERBROWSER_PROFILE or ClientConfig(profile=...) is set.
Profile names must match ^[A-Za-z0-9._-]+$.
base_url and HYPERBROWSER_BASE_URL accept either https://host or https://host/api. The client normalizes both to the same control-plane base URL.
WebMCP page tools
Create a session with {"enable_web_mcp": True} (default: false), then navigate
to a page that exposes WebMCP tools using Playwright. With that session open:
discovery = client.sessions.webmcp.list_tools(session.id)
# Select a tool using its name, source page/frame, and input schema.
tool = next(tool for tool in discovery.tools if tool.name == "search")
result = client.sessions.webmcp.invoke(session.id, {
"tool_ref": tool.tool_ref,
"input": {"query": "flights to Tokyo"}, # Match the selected tool's schema.
})
print(result.status, result.output)
For long-running tools or forms awaiting human submission, use
client.sessions.webmcp.start(session_id, params), then
client.sessions.webmcp.get_result(session_id, invocation_id, {"wait_seconds": 30})
until the status is terminal. client.sessions.webmcp.cancel(session_id, invocation_id)
requests best-effort cancellation. Blocking invocation defaults to 60 seconds
(maximum 120); start defaults to 300 seconds (maximum 3,600) via timeout_seconds.
All five methods also work with AsyncHyperbrowser by adding await.
Discover tools again after navigation. Invocations never automatically retry;
losing a response can leave an unknown outcome. Handles are ephemeral and
terminal results expire after 10 minutes or earlier eviction. Check result
status and truncation metadata before using output. Tool input and output keys
are not renamed. Request TypedDicts are available in hyperbrowser.types, and
Pydantic request/response models in hyperbrowser.models.
See the WebMCP guide for complete
examples, cancellation behavior, human submission, and limits.
Web Bot Auth
A session can sign its requests with Web Bot Auth so websites can verify that they
come from Hyperbrowser. Web Bot Auth is enabled for teams on request.
session = client.sessions.create({"web_bot_auth": True})
print(session.launch_state.web_bot_auth.signature_agent) # https://www.hyperbrowser.ai
# Sign only requests to these hosts and their subdomains:
client.sessions.create({"web_bot_auth": {"only_domains": ["example.com"]}})
Agent tasks accept web_bot_auth in session_options; scrape, crawl, and extract
jobs do not. It cannot be combined with use_ultra_stealth or
start_from_snapshot. See the
Web Bot Auth guide.
Proxy routes
proxy_routes sends some hosts' traffic straight out instead of through the
session's proxy, or the other way round. The first route that matches a host
decides; hosts that no route matches use the proxy. Proxy routes require
use_proxy and are enabled for teams on request.
session = client.sessions.create({
"use_proxy": True,
"proxy_routes": [
{"match": "api.example.com", "proxy": "default"}, # stays on the proxy
{"match": ["*.example.com", "cdn.example.net"], "proxy": "direct"},
],
})
A pattern is a hostname, *.example.com (its subdomains, not example.com
itself), or * (every host). Proxy routes only apply to client.sessions.create
and cannot be combined with allow_internet_access=False, allow_out, or
deny_out. See the
proxy routes guide.
Fill credentials into browser sessions
Any secret with allowed origins can be typed into pages of a browser session.
A website login is usually two such secrets grouped in a vault, plus a TOTP
secret when the site asks for a one-time code. Values are encrypted on your
machine; after upload, neither the API, your code, nor an LLM sees them again.
from playwright.sync_api import sync_playwright
from hyperbrowser import Hyperbrowser
client = Hyperbrowser(api_key="test-key")
origins = ["https://github.com"]
client.secrets.create(
{"name": "github-username", "value": "ada@example.com", "allowed_origins": origins}
)
client.secrets.create(
{"name": "github-password", "value": "correct horse", "allowed_origins": origins}
)
# A TOTP seed: base32 as the site shows it, or an otpauth://totp/ URI.
client.secrets.create(
{
"name": "github-otp",
"type": "totp",
"value": "JBSW Y3DP EHPK 3PXP JBSW Y3DP EHPK 3PXP",
"allowed_origins": origins,
}
)
client.vaults.create(
{
"name": "github-login",
"entries": {
"USERNAME": {"name": "github-username"},
"PASSWORD": {"name": "github-password"},
"OTP": {"name": "github-otp"},
},
}
)
# Secrets and vaults can only be attached when the session is created.
session = client.sessions.create({"credentials": [{"vault": "github-login"}]})
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(session.ws_endpoint)
page = browser.contexts[0].pages[0]
page.goto("https://github.com/login")
# With a vault, each field names the entry to type.
result = client.sessions.credentials.fill(
session.id,
{
"vault": "github-login",
"fields": [
{"field": "USERNAME", "selector": "#login_field"},
{"field": "PASSWORD", "selector": "#password"},
],
},
)
if all(field.outcome == "filled" for field in result.fields):
page.click("input[type=submit]") # Fill only types; submitting is up to you.
page.wait_for_selector("#app_totp")
# A TOTP entry types the code that is current at fill time.
client.sessions.credentials.fill(
session.id,
{"vault": "github-login", "fields": [{"field": "OTP", "selector": "#app_totp"}]},
)
client.sessions.stop(session.id)
A single secret works without a vault: attach it by name ("credentials": ["github-password"] or [{"secret": "github-password"}]) and fill it with
{"credential": "github-password", "fields": [{"selector": "#password"}]}.
fill() returns the filled page's page_id and origin, the version for a
single-secret fill, and per input its selector, outcome, secret_id,
version, field (vault fills), reference (1Password fills) and
expires_at (TOTP codes). Values are never returned.
| Outcome |
Meaning |
filled |
The input now holds the value. |
not_found |
No input matched the selector. |
ambiguous |
Several visible inputs matched the selector. |
not_editable |
The input is disabled, read-only, hidden, or not a text input. |
not_accepted |
The page didn't keep the value, for example because of maxlength. |
Things to know:
- A secret can be filled only on its allowed origins (
scheme://host[:port];
https://*.example.com matches any subdomain but not example.com itself).
A secret takes 1-20 of at most 512 characters, https only (http for
localhost, 127.0.0.1 and [::1]), with no path and *. only on a
registrable domain such as example.com, never *.com or an IP address.
The API validates and normalizes them.
Change them with client.secrets.set_allowed_origins("github-password", [...]); None clears them, after which it can't be filled.
client.secrets.update() takes {"metadata": {"browser": None}} too: each
metadata member sent replaces the stored one and None removes it.
- Secret types are
secret (the default), totp and op_token, fixed at
creation; client.secrets.list({"type": "totp"}) filters by type. TOTP
values are checked locally before upload: a base32 seed (spaces, hyphens, line breaks,
lowercase and trailing = are fine) or an otpauth://totp/ URI with
algorithm SHA1/SHA256/SHA512, digits 6 or 8 and period 1-300. Seeds
shorter than 10 bytes (16 characters) are accepted but weak. The platform
can't read values, so a bad seed would otherwise only fail when used. A TOTP
secret delivered to a sandbox holds the code current at process start.
- A fill takes one of
credential, vault and one_password, and 1-16
fields with unique selectors; field is required with a vault and left out
without one, and ref is required with one_password and left out without
it.
Without page_id, fill considers the tabs whose URL starts with page_url
(all tabs when it's not given). If one is left, that's the target. If several
are left, it prefers the tabs on an origin every secret allows (skipped with
frame_url, since a login frame's tab is usually on another origin), then
the visible one among them; if more than one still qualifies, it fails with
CREDENTIAL_PAGE_AMBIGUOUS. The chosen tab's origin is then checked: on any
other origin nothing is typed and the fill fails with
CREDENTIAL_ORIGIN_NOT_ALLOWED. Under Playwright every tab reports as visible, so pass
page_url (a URL prefix) or the tab's CDP target ID as page_id:
page.context.new_cdp_session(page).send("Target.getTargetInfo")["targetInfo"]["targetId"].
For a login form in an iframe, pass frame_url (a URL prefix).
- Fill is safe to retry and never retried automatically. It waits at least
30 seconds for the API (the client timeout when longer).
- Attaching secrets needs an API key with
secrets.use; filling also needs
browser.sessions.update. The team needs the secrets feature. Sessions with
credentials run as Firecracker browsers with Chrome's password manager off.
A vault is attached by reference, so each fill uses the secrets its entries
name at that moment; session.vaults lists the attached vaults as id and
name.
- Fills are recorded in the session's event log as
credential_filled and
credential_fill_refused. Their metadata names the credential or vault;
for 1Password fills, onePasswordToken (the op_token's name) and its
secretId.
- Errors are raised as
HyperbrowserError with the API's code, retryable,
and status_code. details holds the API's response body, so the
candidate tabs of an ambiguous fill are in error.details["details"]["pages"]
(each with pageId, url and title). Error codes from the API:
CREDENTIAL_PAGE_AMBIGUOUS (409): several tabs could be the target; pass
one's pageId as page_id, or a page_url. CREDENTIAL_FRAME_AMBIGUOUS
(409) is the same for frames.
CREDENTIAL_ORIGIN_NOT_ALLOWED (403): the page's origin (in details)
isn't allowed; nothing was typed.
CREDENTIAL_NOT_ATTACHED (403): the secret, vault or 1Password fill's
op_token wasn't attached to the session, directly or as an entry of an
attached vault.
SECRET_REQUIRES_CUSTOMER_UNWRAP (400): a customer-protected secret
can't be typed into a page (see
Customer-protected secrets).
CREDENTIAL_NOT_READY (409): the secret has no allowed origins or no
value.
SECRET_TYPE_MISMATCH (409): an op_token can't be attached or filled
as a secret (by name, {"secret": ...} or a vault entry); attach it with
{"one_password": ...} instead. Also another type named as one.
SECRET_VALUE_INVALID (422): the stored value is unusable, such as a bad
TOTP seed; add a new version.
CREDENTIAL_PAGE_NOT_FOUND, CREDENTIAL_FRAME_NOT_FOUND,
SECRET_NOT_FOUND, SECRET_VERSION_NOT_FOUND, VAULT_NOT_FOUND,
VAULT_ENTRY_NOT_FOUND (404), SESSION_NOT_ACTIVE (409).
CREDENTIAL_FILL_TIMEOUT (503/504, retryable) and
CREDENTIAL_FILL_FAILED (500/502/503, retryable as flagged).
CREDENTIALS_UNSUPPORTED_RUNTIME (400), CREDENTIAL_FILL_UNSUPPORTED
(501) and CREDENTIAL_VALUE_UNSUPPORTED (501): the session's browser
can't fill this, such as 1Password fields on an older host.
SECRET_ONEPASSWORD_REFERENCE_FAILED (422): a 1Password item or field
couldn't be read, so nothing was filled. details has the failing
selectors and reasons by selector. SECRET_ONEPASSWORD_AUTH_FAILED
(422): 1Password rejected the token, or the token is malformed or doesn't
sign in to 1password.com, .ca or .eu (or a subdomain), so it wasn't
sent to 1Password. SECRET_ONEPASSWORD_RATE_LIMITED (429) and
SECRET_ONEPASSWORD_FAILED (502/503) are retryable. Each has the
token's tokenSecretId and tokenVersion in details.
- Local fill shape errors raise
invalid_request; invalid secret values
raise invalid_secret_value before upload. Fill, origin, vault-entry and
1Password policy errors come from the API. Inspect the status, code (when
supplied) and details; schema errors may have no machine code.
Fill from 1Password
A fill can also type fields of a 1Password vault, read with a 1Password
service account token stored as an op_token secret (see
Manage secrets). Attach
the token when creating the session, then name the vault, the origins the
values may be typed on, and each input's field:
session = client.sessions.create({"credentials": [{"one_password": "op-token"}]})
# ... open https://www.scrapingcourse.com/login in the session ...
result = client.sessions.credentials.fill(
session.id,
{
"one_password": {
"token": "op-token", # the op_token secret; its latest version signs in
"vault": "Dev Vault", # the 1Password vault's name or ID
# Nothing stored says where a 1Password value belongs, so the
# request names the origins it may be typed on.
"allowed_origins": ["https://www.scrapingcourse.com"],
},
"fields": [
{"selector": "#email", "ref": {"item": "scrapingcourse login", "field": "username"}},
{"selector": "#password", "ref": {"item": "scrapingcourse login", "field": "password"}},
{
"selector": "#otp",
"ref": {"item": "scrapingcourse login", "section": "Security", "field": "otp?attribute=totp"},
},
],
"page_url": "https://www.scrapingcourse.com/login",
},
)
for field in result.fields:
print(field.selector, field.outcome, field.reference) # op://Dev Vault/...
token names the op_token secret, which must be attached to the session
({"one_password": name} counts toward the 20 credentials). An op_token is
never typed itself, and attaching or filling it as a plain secret fails with
SECRET_TYPE_MISMATCH.
allowed_origins takes 1-20 origins with the same rules as a secret's.
A page on another origin fails with
CREDENTIAL_ORIGIN_NOT_ALLOWED, as for stored secrets.
ref is {"item", "section"?, "field"} by name or ID, with the same rules
as a sandbox's one_password fields: a field may end with a query such as
?attribute=otp, and op://<vault>/<item>/[<section>/]<field> is at most
512 bytes. Every field takes ref and none takes field.
- The result has no top-level
version; each input's secret_id and
version are the op_token's, and reference is the op:// reference it
was typed from.
- The API validates these rules. Request models still reject unknown members
and invalid shapes locally.
Use with LLM SDKs