Skip to content

cleanup docs and quickstart #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2024
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
71 changes: 33 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,45 @@ The full API of this library can be found in [api.md](api.md).

```python
import os
from playwright.sync_api import Playwright, sync_playwright
from browserbase import Browserbase

client = Browserbase(
BROWSERBASE_API_KEY = os.environ.get("BROWSERBASE_API_KEY")
BROWSERBASE_PROJECT_ID = os.environ.get("BROWSERBASE_PROJECT_ID")

bb = Browserbase(
# This is the default and can be omitted
api_key=os.environ.get("BROWSERBASE_API_KEY"),
api_key=BROWSERBASE_API_KEY,
)

context = client.contexts.create(
project_id="projectId",
)
print(context.id)
```
def run(playwright: Playwright) -> None:
# Create a session on Browserbase
session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID)

# Connect to the remote session
chromium = playwright.chromium
browser = chromium.connect_over_cdp(session.connect_url)
context = browser.contexts[0]
page = context.pages[0]

While you can provide an `api_key` keyword argument,
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.
# Execute Playwright actions on the remote browser tab
page.goto("https://news.ycombinator.com/")
page_title = page.title()
assert (
page_title == "Hacker News"
), f"Page title is not 'Hacker News', it is '{page_title}'"
page.screenshot(path="screenshot.png")

page.close()
browser.close()
print("Done!")


if __name__ == "__main__":
with sync_playwright() as playwright:
run(playwright)

```

## Examples

Expand All @@ -63,33 +85,6 @@ rye run example playwright_basic # 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:

```python
import os
import asyncio
from browserbase import AsyncBrowserbase

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


async def main() -> None:
context = await client.contexts.create(
project_id="projectId",
)
print(context.id)


asyncio.run(main())
```

Functionality between the synchronous and asynchronous clients is otherwise identical.

## Using types

Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev) which also provide helper methods for things like:
Expand Down
19 changes: 14 additions & 5 deletions examples/playwright_basic.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import os

from playwright.sync_api import Playwright, sync_playwright

from examples import (
BROWSERBASE_PROJECT_ID,
bb,
from browserbase import Browserbase

BROWSERBASE_API_KEY = os.environ.get("BROWSERBASE_API_KEY", "")
if not BROWSERBASE_API_KEY:
raise ValueError("BROWSERBASE_API_KEY is not set")
BROWSERBASE_PROJECT_ID = os.environ.get("BROWSERBASE_PROJECT_ID", "")
if not BROWSERBASE_PROJECT_ID:
raise ValueError("BROWSERBASE_PROJECT_ID is not set")

bb = Browserbase(
# This is the default and can be omitted
api_key=BROWSERBASE_API_KEY,
)


def run(playwright: Playwright) -> None:
# Create a session on Browserbase
session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID)
assert session.id is not None
assert session.status == "RUNNING", f"Session status is {session.status}"

# Connect to the remote session
chromium = playwright.chromium
Expand Down