You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Browser Use Python library provides convenient access to the Browser Use REST API from any Python 3.8+
7
-
application. The library includes type definitions for all request params and response fields,
8
-
and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx).
9
-
10
-
It is generated with [Stainless](https://www.stainless.com/).
11
-
12
-
## Documentation
13
-
14
-
The REST API documentation can be found on [docs.browser-use.com](https://docs.browser-use.com/cloud/). The full API of this library can be found in [api.md](api.md).
The full API of this library can be found in [api.md](api.md).
11
+
> Get your API Key at [Browser Use Cloud](https://cloud.browser-use.com)!
26
12
27
13
```python
28
-
import os
29
14
from browser_use_sdk import BrowserUse
30
15
31
-
client = BrowserUse(
32
-
api_key=os.environ.get("BROWSER_USE_API_KEY"), # This is the default and can be omitted
33
-
)
16
+
client = BrowserUse()
34
17
35
-
task= client.tasks.create(
36
-
task="Search for the top 10 Hacker News posts and return the title and url.",
18
+
run= client.tasks.run(
19
+
task="Search for the top 10 Hacker News posts and return the title and url."
37
20
)
38
-
print(task.id)
21
+
22
+
run.done_output
39
23
```
40
24
41
-
While you can provide an `api_key` keyword argument,
42
-
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
43
-
to add `BROWSER_USE_API_KEY="My API Key"` to your `.env` file
44
-
so that your API Key is not stored in source control.
25
+
> The full API of this library can be found in [api.md](api.md).
45
26
46
27
## Async usage
47
28
@@ -58,10 +39,10 @@ client = AsyncBrowserUse(
58
39
59
40
60
41
asyncdefmain() -> None:
61
-
task =await client.tasks.create(
42
+
task =await client.tasks.run(
62
43
task="Search for the top 10 Hacker News posts and return the title and url.",
63
44
)
64
-
print(task.id)
45
+
print(task.done_output)
65
46
66
47
67
48
asyncio.run(main())
@@ -93,38 +74,80 @@ async def main() -> None:
93
74
api_key="My API Key",
94
75
http_client=DefaultAioHttpClient(),
95
76
) as client:
96
-
task =await client.tasks.create(
77
+
task =await client.tasks.run(
97
78
task="Search for the top 10 Hacker News posts and return the title and url.",
98
79
)
99
-
print(task.id)
80
+
print(task.done_output)
81
+
82
+
83
+
asyncio.run(main())
84
+
```
85
+
86
+
## Structured Output with Pydantic
87
+
88
+
Browser Use Python SDK provides first class support for Pydantic models.
89
+
90
+
```py
91
+
classHackerNewsPost(BaseModel):
92
+
title: str
93
+
url: str
94
+
95
+
classSearchResult(BaseModel):
96
+
posts: List[HackerNewsPost]
97
+
98
+
asyncdefmain() -> None:
99
+
result =await client.tasks.run(
100
+
task="""
101
+
Find top 10 Hacker News articles and return the title and url.
102
+
""",
103
+
structured_output_json=SearchResult,
104
+
)
100
105
106
+
if structured_result.parsed_output isnotNone:
107
+
print("Top HackerNews Posts:")
108
+
for post in structured_result.parsed_output.posts:
109
+
print(f" - {post.title} - {post.url}")
101
110
102
111
asyncio.run(main())
103
112
```
104
113
105
-
## Using types
114
+
## Streaming Updates with Async Iterators
106
115
107
-
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:
116
+
```py
117
+
classHackerNewsPost(BaseModel):
118
+
title: str
119
+
url: str
108
120
109
-
- Serializing back into JSON, `model.to_json()`
110
-
- Converting to a dictionary, `model.to_dict()`
121
+
classSearchResult(BaseModel):
122
+
posts: List[HackerNewsPost]
111
123
112
-
Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `basic`.
113
124
114
-
## Nested params
125
+
asyncdefmain() -> None:
126
+
task =await client.tasks.create(
127
+
task="""
128
+
Find top 10 Hacker News articles and return the title and url.
129
+
""",
130
+
structured_output_json=SearchResult,
131
+
)
115
132
116
-
Nested parameters are dictionaries, typed using `TypedDict`, for example:
133
+
asyncfor update in client.tasks.stream(structured_task.id, structured_output_json=SearchResult):
0 commit comments