Skip to main content

The official Python library for the Browser Use API

Project description

Browser Use Python

BrowserUse Python Library

fern shield pypi

The BrowserUse Python library provides convenient access to the BrowserUse APIs from Python.

Three-Step QuickStart

  1. 📦 Install Browser Use SDK

    # PIP
    pip install browser-use-sdk
    
    # Poetry
    poetry add browser-use-sdk
    
    # UV
    uv add browser-use-sdk
    
  2. 🔑 Get your API Key at Browser Use Cloud!

  3. 🦄 Automate the Internet!

    from browser_use_sdk import BrowserUse
    
    client = BrowserUse(api_key="bu_...")
    
    task = client.tasks.create_task(
        task="Search for the top 10 Hacker News posts and return the title and url.",
        llm="gpt-4.1"
    )
    
    result = task.complete()
    
    result.output
    

The full API of this library can be found in api.md.

Structured Output with Pydantic

Browser Use Python SDK provides first class support for Pydantic models.

from browser_use_sdk import AsyncBrowserUse

client = AsyncBrowserUse(api_key=API_KEY)

class HackerNewsPost(BaseModel):
    title: str
    url: str

class SearchResult(BaseModel):
    posts: List[HackerNewsPost]

async def main() -> None:
    task = await client.tasks.create_task(
        task="""
        Find top 10 Hacker News articles and return the title and url.
        """,
        schema=SearchResult,
    )

    result = await task.complete()

    if result.parsed_output is not None:
        print("Top HackerNews Posts:")
        for post in result.parsed_output.posts:
            print(f" - {post.title} - {post.url}")

asyncio.run(main())

Streaming Updates with Async Iterators

When presenting a long running task you might want to show updates as they happen.

Browser Use SDK exposes a .stream method that lets you subscribe to a sync or an async generator that automatically polls Browser Use Cloud servers and emits a new event when an update happens (e.g., live url becomes available, agent takes a new step, or agent completes the task).

class HackerNewsPost(BaseModel):
    title: str
    url: str

class SearchResult(BaseModel):
    posts: List[HackerNewsPost]


async def main() -> None:
    task = await client.tasks.create_task(
        task="""
        Find top 10 Hacker News articles and return the title and url.
        """,
        schema=SearchResult,
    )

    async for step in task.stream():
        print(f"Step {step.number}: {step.url} ({step.next_goal})")

    result = await task.complete()

    if result.parsed_output is not None:
        print("Top HackerNews Posts:")
        for post in result.parsed_output.posts:
            print(f" - {post.title} - {post.url}")

asyncio.run(main())

Verifying Webhook Events

You can configure Browser Use Cloud to emit Webhook events and process them easily with Browser Use Python SDK.

Browser Use SDK lets you easily verify the signature and structure of the payload you receive in the webhook.

import uvicorn
import os
from browser_use_sdk import Webhook, verify_webhook_event_signature

from fastapi import FastAPI, Request, HTTPException

app = FastAPI()

SECRET_KEY = os.environ['SECRET_KEY']

@app.post('/webhook')
async def webhook(request: Request):
    body = await request.json()

    timestamp = request.headers.get('X-Browser-Use-Timestamp')
    signature = request.headers.get('X-Browser-Use-Signature')

    verified_webhook: Webhook = verify_webhook_event_signature(
        body=body,
        timestamp=timestamp,
        secret=SECRET_KEY,
        expected_signature=signature,
    )

    if verified_webhook is not None:
        print('Webhook received:', verified_webhook)
    else:
        print('Invalid webhook received')

    return {'status': 'success', 'message': 'Webhook received'}

if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=8080)

Async usage

Simply import AsyncBrowserUse instead of BrowserUse and use await with each API call:

import os
import asyncio
from browser_use_sdk import AsyncBrowserUse

client = AsyncBrowserUse(
    api_key=os.environ.get("BROWSER_USE_API_KEY"),  # This is the default and can be omitted
)


async def main() -> None:
    task = await client.tasks.create_task(
        task="Search for the top 10 Hacker News posts and return the title and url.",
    )

    print(task.id)


asyncio.run(main())

Requirements

Python 3.8 or higher.

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!

Reference

A full reference for this library is available here.

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

browser_use_sdk-2.0.4.tar.gz (41.1 kB view details)

Uploaded Source

Built Distribution

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

browser_use_sdk-2.0.4-py3-none-any.whl (79.3 kB view details)

Uploaded Python 3

File details

Details for the file browser_use_sdk-2.0.4.tar.gz.

File metadata

  • Download URL: browser_use_sdk-2.0.4.tar.gz
  • Upload date:
  • Size: 41.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.18 Linux/6.11.0-1018-azure

File hashes

Hashes for browser_use_sdk-2.0.4.tar.gz
Algorithm Hash digest
SHA256 c895ff11fca7e7b75e1dde82437e155c5c19bd24960d5f14ea167ed9df8f7796
MD5 535812f538ba6baf52e57504c8f926b6
BLAKE2b-256 378f62ddf607b11b3643aac640e744b35b1412a935b2aa0b026f794f49ed3ca0

See more details on using hashes here.

File details

Details for the file browser_use_sdk-2.0.4-py3-none-any.whl.

File metadata

  • Download URL: browser_use_sdk-2.0.4-py3-none-any.whl
  • Upload date:
  • Size: 79.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.18 Linux/6.11.0-1018-azure

File hashes

Hashes for browser_use_sdk-2.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 01898714aaee3f2996df8f134eb7eb3dd30064ebf4a620575653526ee8a891e0
MD5 ad7719c97ce64570914d1c901054e97f
BLAKE2b-256 6440e1edec8db433a20151ed222b7973b9a2a7d01cbe24ad06ec08f7e3c71d89

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