@@ -26,32 +26,105 @@ const client = new BrowserUse({
26
26
apiKey: process .env [' BROWSER_USE_API_KEY' ], // This is the default and can be omitted
27
27
});
28
28
29
- const task = await client .tasks .create ({
29
+ // #1 - Run a task and get its result
30
+
31
+ const task = await client .tasks .run ({
30
32
task: ' Search for the top 10 Hacker News posts and return the title and url.' ,
31
33
});
32
34
33
- console .log (task .id );
34
- ```
35
+ console .log (task .doneOutput );
35
36
36
- ### Request & Response types
37
+ // #2 - Run a task with a structured result
37
38
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 ' ;
39
40
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
+ });
43
49
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. ' ,
46
52
});
47
53
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 ({
49
61
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
+ }
52
87
```
53
88
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
+ ```
55
128
56
129
## Handling errors
57
130
0 commit comments