Skip to content

Commit 8eba39b

Browse files
authored
Merge pull request #21 from browser-use/feat/readme
Improve README
2 parents fc2c530 + 5e22f4e commit 8eba39b

File tree

3 files changed

+89
-16
lines changed

3 files changed

+89
-16
lines changed

README.md

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,105 @@ const client = new BrowserUse({
2626
apiKey: process.env['BROWSER_USE_API_KEY'], // This is the default and can be omitted
2727
});
2828

29-
const task = await client.tasks.create({
29+
// #1 - Run a task and get its result
30+
31+
const task = await client.tasks.run({
3032
task: 'Search for the top 10 Hacker News posts and return the title and url.',
3133
});
3234

33-
console.log(task.id);
34-
```
35+
console.log(task.doneOutput);
3536

36-
### Request & Response types
37+
// #2 - Run a task with a structured result
3738

38-
This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:
39+
import z from 'zod';
3940

40-
<!-- prettier-ignore -->
41-
```ts
42-
import BrowserUse from 'browser-use-sdk';
41+
const TaskOutput = z.object({
42+
posts: z.array(
43+
z.object({
44+
title: z.string(),
45+
url: z.string(),
46+
}),
47+
),
48+
});
4349

44-
const client = new BrowserUse({
45-
apiKey: process.env['BROWSER_USE_API_KEY'], // This is the default and can be omitted
50+
const posts = await client.tasks.run({
51+
task: 'Search for the top 10 Hacker News posts and return the title and url.',
4652
});
4753

48-
const params: BrowserUse.TaskCreateParams = {
54+
for (const post of posts.doneOutput.posts) {
55+
console.log(`${post.title} - ${post.url}`);
56+
}
57+
58+
// #3 - Stream Task Progress
59+
60+
const hn = await browseruse.tasks.create({
4961
task: 'Search for the top 10 Hacker News posts and return the title and url.',
50-
};
51-
const task: BrowserUse.TaskCreateResponse = await client.tasks.create(params);
62+
schema: TaskOutput,
63+
});
64+
65+
const stream = browseruse.tasks.stream({
66+
taskId: hn.id,
67+
schema: TaskOutput,
68+
});
69+
70+
for await (const msg of stream) {
71+
switch (msg.status) {
72+
case 'started':
73+
case 'paused':
74+
case 'stopped':
75+
console.log(`running: ${msg}`);
76+
break;
77+
78+
case 'finished':
79+
console.log(`done:`);
80+
81+
for (const post of msg.doneOutput.posts) {
82+
console.log(`${post.title} - ${post.url}`);
83+
}
84+
break;
85+
}
86+
}
5287
```
5388

54-
Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.
89+
## Webhook Verification
90+
91+
> We encourage you to use the SDK functions that verify and parse webhook events.
92+
93+
```ts
94+
import {
95+
verifyWebhookEventSignature,
96+
type WebhookAgentTaskStatusUpdatePayload,
97+
} from 'browser-use-sdk/lib/webhooks';
98+
99+
export async function POST(req: Request) {
100+
const signature = req.headers['x-browser-use-signature'] as string;
101+
const timestamp = req.headers['x-browser-use-timestamp'] as string;
102+
103+
const event = await verifyWebhookEventSignature(
104+
{
105+
body,
106+
signature,
107+
timestamp,
108+
},
109+
{
110+
secret: SECRET_KEY,
111+
},
112+
);
113+
114+
if (!event.ok) {
115+
return;
116+
}
117+
118+
switch (event.event.type) {
119+
case 'agent.task.status_update':
120+
break;
121+
case 'test':
122+
break;
123+
default:
124+
break;
125+
}
126+
}
127+
```
55128

56129
## Handling errors
57130

examples/run-zod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const TaskOutput = z.object({
2121
});
2222

2323
async function main() {
24-
console.log(`Creating Task...`);
24+
console.log(`Running Task...`);
2525

2626
// Create Task
2727
const rsp = await browseruse.tasks.run({

examples/run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env();
1010
const browseruse = new BrowserUse();
1111

1212
async function main() {
13-
console.log(`Creating Task...`);
13+
console.log(`Running Task...`);
1414

1515
// Create Task
1616
const rsp = await browseruse.tasks.run({

0 commit comments

Comments
 (0)