Skip to content

Commit 5847acd

Browse files
committed
fixes
1 parent 870eaee commit 5847acd

File tree

9 files changed

+269
-179
lines changed

9 files changed

+269
-179
lines changed

.fernignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Specify files that shouldn't be modified by Fern
22
src/wrapper/
33
examples/
4-
.vscode/
4+
.vscode/
5+
6+
.gitignore

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_modules
33
/dist
44
.cursor
55

6-
.env
6+
.env*
7+
!.env.example

README.md

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ The BrowserUse TypeScript library provides convenient access to the BrowserUse A
1212
1. ✌️ Automate the web!
1313

1414
```ts
15-
import BrowserUse from "browser-use-sdk";
15+
import { BrowserUseClient } from "browser-use-sdk";
1616

17-
const client = new BrowserUse({
17+
const client = new BrowserUseClient({
1818
apiKey: "bu_...",
1919
});
2020

21-
const result = await client.tasks.run({
21+
const task = await client.tasks.createTask({
2222
task: "Search for the top 10 Hacker News posts and return the title and url.",
2323
});
2424

25-
console.log(result.doneOutput);
25+
const result = await task.complete();
26+
27+
console.log(result.output);
2628
```
2729

2830
> The full API of this library can be found in [api.md](api.md).
@@ -41,29 +43,27 @@ const TaskOutput = z.object({
4143
),
4244
});
4345

44-
const result = await client.tasks.run({
46+
const task = await client.tasks.createTask({
4547
task: "Search for the top 10 Hacker News posts and return the title and url.",
48+
schema: TaskOutput,
4649
});
4750

48-
for (const post of result.parsedOutput.posts) {
51+
const result = await task.complete();
52+
53+
for (const post of result.parsed.posts) {
4954
console.log(`${post.title} - ${post.url}`);
5055
}
5156
```
5257

5358
### Streaming Agent Updates
5459

5560
```ts
56-
const task = await browseruse.tasks.create({
61+
const task = await browseruse.tasks.createTask({
5762
task: "Search for the top 10 Hacker News posts and return the title and url.",
5863
schema: TaskOutput,
5964
});
6065

61-
const stream = browseruse.tasks.stream({
62-
taskId: task.id,
63-
schema: TaskOutput,
64-
});
65-
66-
for await (const msg of stream) {
66+
for await (const msg of task.stream()) {
6767
switch (msg.status) {
6868
case "started":
6969
case "paused":
@@ -74,51 +74,14 @@ for await (const msg of stream) {
7474
case "finished":
7575
console.log(`done:`);
7676

77-
for (const post of msg.parsedOutput.posts) {
77+
for (const post of msg.parsed.posts) {
7878
console.log(`${post.title} - ${post.url}`);
7979
}
8080
break;
8181
}
8282
}
8383
```
8484

85-
## Webhook Verification
86-
87-
> We encourage you to use the SDK functions that verify and parse webhook events.
88-
89-
```ts
90-
import { verifyWebhookEventSignature, type WebhookAgentTaskStatusUpdatePayload } from "browser-use-sdk/lib/webhooks";
91-
92-
export async function POST(req: Request) {
93-
const signature = req.headers["x-browser-use-signature"] as string;
94-
const timestamp = req.headers["x-browser-use-timestamp"] as string;
95-
96-
const event = await verifyWebhookEventSignature(
97-
{
98-
body,
99-
signature,
100-
timestamp,
101-
},
102-
{
103-
secret: SECRET_KEY,
104-
},
105-
);
106-
107-
if (!event.ok) {
108-
return;
109-
}
110-
111-
switch (event.event.type) {
112-
case "agent.task.status_update":
113-
break;
114-
case "test":
115-
break;
116-
default:
117-
break;
118-
}
119-
}
120-
```
121-
12285
## Advanced Usage
12386

12487
## Handling errors

examples/.env

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/retrieve.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ env();
1111
// gets API Key from environment variable BROWSER_USE_API_KEY
1212
const browseruse = new BrowserUseClient({
1313
apiKey: process.env.BROWSER_USE_API_KEY!,
14-
environment: "production",
14+
environment: "https://api.browser-use.com/api/v2",
1515
});
1616

1717
// Basic ---------------------------------------------------------------------
@@ -22,7 +22,7 @@ async function basic() {
2222

2323
// Create Task
2424
const rsp = await browseruse.tasks.createTask({
25-
task: "What's the weather line in SF and what's the temperature?",
25+
task: "What's the weather in SF and what's the temperature?",
2626
agent: { llm: "gemini-2.5-flash" },
2727
});
2828

@@ -34,14 +34,16 @@ async function basic() {
3434
case "started":
3535
case "paused":
3636
case "stopped":
37-
log = `agent ${status.status}`;
37+
log = `agent ${status.status} ${status.sessionId} ${status.steps.length} steps`;
3838

3939
await new Promise((resolve) => setTimeout(resolve, 2000));
4040
break;
4141

4242
case "finished":
4343
stop();
4444

45+
console.log(status);
46+
4547
console.log(status.output);
4648
break poll;
4749
}
@@ -50,15 +52,11 @@ async function basic() {
5052

5153
// Structured ----------------------------------------------------------------
5254

53-
// Define Structured Output Schema
54-
const HackerNewsResponse = z.object({
55-
title: z.string(),
56-
url: z.string(),
57-
score: z.number(),
58-
});
59-
6055
const TaskOutput = z.object({
61-
posts: z.array(HackerNewsResponse),
56+
degreesFahrenheit: z.number(),
57+
degreesCelsius: z.number(),
58+
59+
description: z.string(),
6260
});
6361

6462
async function structured() {
@@ -67,7 +65,7 @@ async function structured() {
6765

6866
// Create Task
6967
const rsp = await browseruse.tasks.createTask({
70-
task: "Extract top 10 Hacker News posts and return the title, url, and score",
68+
task: "What's the weather in SF and what's the temperature?",
7169
schema: TaskOutput,
7270
agent: { llm: "gpt-4.1" },
7371
});
@@ -98,11 +96,14 @@ async function structured() {
9896
stop();
9997

10098
// Print Structured Output
101-
console.log("Top Hacker News Posts:");
99+
console.log("Weather in SF:");
100+
console.log("--------------------------------");
101+
console.log(status.parsed.description);
102102

103-
for (const post of status.parsed.posts) {
104-
console.log(` - ${post.title} (${post.score}) ${post.url}`);
105-
}
103+
console.log(` - ${status.parsed.degreesFahrenheit} degrees Fahrenheit`);
104+
console.log(` - ${status.parsed.degreesCelsius} degrees Celsius`);
105+
106+
console.log("--------------------------------");
106107

107108
break poll;
108109
}

examples/run.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env();
1010
// gets API Key from environment variable BROWSER_USE_API_KEY
1111
const browseruse = new BrowserUseClient({
1212
apiKey: process.env.BROWSER_USE_API_KEY!,
13-
environment: "production",
13+
environment: "https://api.browser-use.com/api/v2",
1414
});
1515

1616
// Basic ---------------------------------------------------------------------
@@ -20,7 +20,7 @@ async function basic() {
2020

2121
// Create Task
2222
const rsp = await browseruse.tasks.createTask({
23-
task: "What's the weather line in SF and what's the temperature?",
23+
task: "What's the weather in SF and what's the temperature?",
2424
});
2525

2626
const result = await rsp.complete();

examples/stream.ts

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,34 @@
33
import z from "zod";
44

55
import { BrowserUseClient } from "..";
6+
67
import { env } from "./utils";
78

89
env();
910

11+
const browseruse = new BrowserUseClient({
12+
apiKey: process.env.BROWSER_USE_API_KEY!,
13+
environment: "https://api.browser-use.com/api/v2",
14+
});
15+
1016
// Basic ---------------------------------------------------------------------
1117

1218
async function basic() {
13-
// gets API Key from environment variable BROWSER_USE_API_KEY
14-
const browseruse = new BrowserUseClient({
15-
apiKey: process.env.BROWSER_USE_API_KEY!,
16-
environment: "production",
17-
});
18-
1919
console.log("Basic: Creating task and starting stream...");
2020

2121
// Create a task and get the stream
2222
const task = await browseruse.tasks.createTask({
23-
task: "What's the weather line in SF and what's the temperature?",
23+
task: "What's the weather in SF and what's the temperature?",
2424
agent: { llm: "gemini-2.5-flash" },
2525
});
2626

27-
const gen = task.stream();
28-
29-
for await (const msg of gen) {
30-
console.log(
31-
`Basic: ${msg.data.status} ${msg.data.sessionId} ${msg.data.steps[msg.data.steps.length - 1]?.nextGoal}`,
32-
);
33-
34-
if (msg.data.status === "finished") {
35-
console.log(`Basic: ${msg.data.output}`);
36-
}
27+
for await (const msg of task.stream()) {
28+
console.log(msg);
3729
}
3830

31+
const result = await task.complete();
32+
console.log(result);
33+
3934
console.log("\nBasic: Stream completed");
4035
}
4136

@@ -53,12 +48,6 @@ const TaskOutput = z.object({
5348
});
5449

5550
async function structured() {
56-
// gets API Key from environment variable BROWSER_USE_API_KEY
57-
const browseruse = new BrowserUseClient({
58-
apiKey: process.env.BROWSER_USE_API_KEY!,
59-
environment: "production",
60-
});
61-
6251
console.log("Structured: Creating task and starting stream...\n");
6352

6453
// Create a task and get the stream
@@ -68,32 +57,13 @@ async function structured() {
6857
agent: { llm: "gpt-4.1" },
6958
});
7059

71-
const stream = task.stream();
72-
73-
for await (const msg of stream) {
74-
// Regular
75-
process.stdout.write(`Structured: ${msg.data.status}`);
76-
if (msg.data.sessionId) {
77-
process.stdout.write(` | Live URL: ${msg.data.sessionId}`);
78-
}
79-
80-
if (msg.data.steps.length > 0) {
81-
const latestStep = msg.data.steps[msg.data.steps.length - 1];
82-
process.stdout.write(` | ${latestStep!.nextGoal}`);
83-
}
84-
85-
process.stdout.write("\n");
86-
87-
// Output
88-
if (msg.data.status === "finished") {
89-
process.stdout.write(`\n\nOUTPUT:`);
90-
91-
for (const post of msg.data.parsed!.posts) {
92-
process.stdout.write(`\n - ${post.title} (${post.score}) ${post.url}`);
93-
}
94-
}
60+
for await (const msg of task.stream()) {
61+
console.log(msg);
9562
}
9663

64+
const result = await task.complete();
65+
console.log(result);
66+
9767
console.log("\nStructured: Stream completed");
9868
}
9969

0 commit comments

Comments
 (0)