diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..a79992b2 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +BROWSERBASE_API_KEY="your-api-key" +BROWSERBASE_PROJECT_ID="your-project-id" diff --git a/README.md b/README.md index 9035de99..28807645 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/examples/01_quickstart.py b/examples/01_quickstart.py new file mode 100644 index 00000000..6fb908ca --- /dev/null +++ b/examples/01_quickstart.py @@ -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) diff --git a/examples/02_create_session.py b/examples/02_create_session.py new file mode 100644 index 00000000..ec5d3b67 --- /dev/null +++ b/examples/02_create_session.py @@ -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")