Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BROWSERBASE_API_KEY="your-api-key"
BROWSERBASE_PROJECT_ID="your-project-id"
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
to add `BROWSERBASE_API_KEY="My API Key"` to your `.env` file
so that your API Key is not stored in source control.

## Examples

See the [examples](examples) directory for more usage examples.

To run the examples, clone this repository and run the following commands from the project root (this directory):

```bash
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install .
python3 -m examples/02_create_session.py # replace with the example you want to run
```

!!! note
Make sure you have a `.env` file that matches the [.env.example](.env.example) file in the root of this repository.

## Async usage

Simply import `AsyncBrowserbase` instead of `Browserbase` and use `await` with each API call:
Expand Down
28 changes: 28 additions & 0 deletions examples/01_quickstart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os

from dotenv import load_dotenv
from playwright.sync_api import Playwright, sync_playwright

load_dotenv(override=True)
BROWSERBASE_API_KEY = os.environ["BROWSERBASE_API_KEY"]
BROWSERBASE_PROJECT_ID = os.environ["BROWSERBASE_PROJECT_ID"]


def run(playwright: Playwright):
print("Api Key: " + os.environ["BROWSERBASE_API_KEY"])
connect_url = "wss://connect.browserbase.com?apiKey=" + BROWSERBASE_API_KEY
print("connect_url: " + connect_url)
chromium = playwright.chromium
browser = chromium.connect_over_cdp(
"wss://connect.browserbase.com?apiKey=" + BROWSERBASE_API_KEY
)
context = browser.contexts[0]
page = context.pages[0]

page.goto("https://www.google.com")
page.screenshot(path="screenshot.png")
print(page.title)


with sync_playwright() as playwright:
run(playwright)
44 changes: 44 additions & 0 deletions examples/02_create_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
from pprint import pprint

from dotenv import load_dotenv
from playwright.sync_api import Playwright, sync_playwright

from browserbase import Browserbase

load_dotenv(override=True)
BROWSERBASE_API_KEY = os.environ["BROWSERBASE_API_KEY"]
BROWSERBASE_PROJECT_ID = os.environ["BROWSERBASE_PROJECT_ID"]


bb = Browserbase(
api_key=BROWSERBASE_API_KEY
) # can be omitted if in environment variable

session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID)
pprint({"session": session})


def run(playwright: Playwright):
print("Api Key: " + os.environ["BROWSERBASE_API_KEY"])
connect_url = session.connectUrl
print("connect_url: " + connect_url)
chromium = playwright.chromium
browser = chromium.connect_over_cdp(connect_url)
context = browser.contexts[0]
page = context.pages[0]

page.goto("https://www.google.com")
page.screenshot(path="screenshot.png")
print(page.title)


with sync_playwright() as playwright:
run(playwright)

update_session_resp = bb.sessions.update(
id=session.id, status="REQUEST_RELEASE", project_id=BROWSERBASE_PROJECT_ID
)
pprint({"update_session_resp": update_session_resp})

print("done")
Loading