Skip to content

Commit 70be553

Browse files
committed
Improve Docs
1 parent d8d78e0 commit 70be553

File tree

10 files changed

+178
-213
lines changed

10 files changed

+178
-213
lines changed

README.md

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,28 @@
1-
# Browser Use TypeScript API Library
2-
3-
[![NPM version](<https://img.shields.io/npm/v/browser-use-sdk.svg?label=npm%20(stable)>)](https://npmjs.org/package/browser-use-sdk) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/browser-use-sdk)
4-
5-
This library provides convenient access to the Browser Use REST API from server-side TypeScript or JavaScript.
6-
7-
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).
8-
9-
It is generated with [Stainless](https://www.stainless.com/).
10-
11-
## Installation
1+
<img src="./assets/cloud-banner-js.png" alt="Browser Use JS" width="full"/>
122

133
```sh
14-
npm install browser-use-sdk
4+
pnpm add browser-use-sdk
155
```
166

17-
## Usage
7+
## QuickStart
188

19-
The full API of this library can be found in [api.md](api.md).
20-
21-
<!-- prettier-ignore -->
22-
```js
9+
```ts
2310
import BrowserUse from 'browser-use-sdk';
2411

25-
const client = new BrowserUse({
26-
apiKey: process.env['BROWSER_USE_API_KEY'], // This is the default and can be omitted
27-
});
28-
29-
// #1 - Run a task and get its result
12+
const client = new BrowserUse();
3013

31-
const task = await client.tasks.run({
14+
const result = await client.tasks.run({
3215
task: 'Search for the top 10 Hacker News posts and return the title and url.',
3316
});
3417

35-
console.log(task.doneOutput);
18+
console.log(result.doneOutput);
19+
```
3620

37-
// #2 - Run a task with a structured result
21+
> The full API of this library can be found in [api.md](api.md).
3822
23+
### Structured Output with Zod
24+
25+
```ts
3926
import z from 'zod';
4027

4128
const TaskOutput = z.object({
@@ -47,23 +34,25 @@ const TaskOutput = z.object({
4734
),
4835
});
4936

50-
const posts = await client.tasks.run({
37+
const result = await client.tasks.run({
5138
task: 'Search for the top 10 Hacker News posts and return the title and url.',
5239
});
5340

54-
for (const post of posts.doneOutput.posts) {
41+
for (const post of result.parsedOutput.posts) {
5542
console.log(`${post.title} - ${post.url}`);
5643
}
44+
```
5745

58-
// #3 - Stream Task Progress
46+
### Streaming Agent Updates
5947

60-
const hn = await browseruse.tasks.create({
48+
```ts
49+
const task = await browseruse.tasks.create({
6150
task: 'Search for the top 10 Hacker News posts and return the title and url.',
6251
schema: TaskOutput,
6352
});
6453

6554
const stream = browseruse.tasks.stream({
66-
taskId: hn.id,
55+
taskId: task.id,
6756
schema: TaskOutput,
6857
});
6958

@@ -78,7 +67,7 @@ for await (const msg of stream) {
7867
case 'finished':
7968
console.log(`done:`);
8069

81-
for (const post of msg.doneOutput.posts) {
70+
for (const post of msg.parsedOutput.posts) {
8271
console.log(`${post.title} - ${post.url}`);
8372
}
8473
break;

assets/cloud-banner-js.png

31.4 KB
Loading

examples/demo.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
11
#!/usr/bin/env -S npm run tsn -T
22

33
import { BrowserUse } from 'browser-use-sdk';
4-
import { z } from 'zod';
54

65
import { env, spinner } from './utils';
6+
import z from 'zod';
77

88
env();
99

1010
// gets API Key from environment variable BROWSER_USE_API_KEY
1111
const browseruse = new BrowserUse();
1212

13+
async function basic() {
14+
let log = 'starting';
15+
const stop = spinner(() => log);
16+
17+
// Create Task
18+
const rsp = await browseruse.tasks.create({
19+
task: "What's the weather line in SF and what's the temperature?",
20+
agentSettings: { llm: 'gemini-2.5-flash' },
21+
});
22+
23+
poll: do {
24+
// Wait for Task to Finish
25+
const status = await browseruse.tasks.retrieve(rsp.id);
26+
27+
switch (status.status) {
28+
case 'started':
29+
case 'paused':
30+
case 'stopped':
31+
log = `agent ${status.status} - live: ${status.session.liveUrl}`;
32+
33+
await new Promise((resolve) => setTimeout(resolve, 2000));
34+
break;
35+
36+
case 'finished':
37+
stop();
38+
39+
console.log(status.doneOutput);
40+
break poll;
41+
}
42+
} while (true);
43+
}
44+
1345
// Define Structured Output Schema
1446
const HackerNewsResponse = z.object({
1547
title: z.string(),
@@ -21,14 +53,15 @@ const TaskOutput = z.object({
2153
posts: z.array(HackerNewsResponse),
2254
});
2355

24-
async function main() {
56+
async function structured() {
2557
let log = 'starting';
2658
const stop = spinner(() => log);
2759

2860
// Create Task
2961
const rsp = await browseruse.tasks.create({
3062
task: 'Extract top 10 Hacker News posts and return the title, url, and score',
3163
schema: TaskOutput,
64+
agentSettings: { llm: 'gemini-2.5-flash' },
3265
});
3366

3467
poll: do {
@@ -42,30 +75,24 @@ async function main() {
4275
case 'started':
4376
case 'paused':
4477
case 'stopped': {
45-
const stepsCount = status.steps ? status.steps.length : 0;
46-
const steps = `${stepsCount} steps`;
47-
const lastGoalDescription = stepsCount > 0 ? status.steps![stepsCount - 1]!.nextGoal : undefined;
48-
const lastGoal = lastGoalDescription ? `, last: ${lastGoalDescription}` : '';
49-
const liveUrl = status.session.liveUrl ? `, live: ${status.session.liveUrl}` : '';
50-
51-
log = `agent ${status.status} (${steps}${lastGoal}${liveUrl}) `;
78+
log = `agent ${status.status} ${status.session.liveUrl} | ${status.steps.length} steps`;
5279

5380
await new Promise((resolve) => setTimeout(resolve, 2000));
5481

5582
break;
5683
}
5784

5885
case 'finished':
59-
if (status.doneOutput == null) {
86+
if (status.parsedOutput == null) {
6087
throw new Error('No output');
6188
}
6289

6390
stop();
6491

6592
// Print Structured Output
66-
console.log('TOP POSTS:');
93+
console.log('Top Hacker News Posts:');
6794

68-
for (const post of status.doneOutput.posts) {
95+
for (const post of status.parsedOutput.posts) {
6996
console.log(` - ${post.title} (${post.score}) ${post.url}`);
7097
}
7198

@@ -74,4 +101,6 @@ async function main() {
74101
} while (true);
75102
}
76103

77-
main().catch(console.error);
104+
basic()
105+
.then(() => structured())
106+
.catch(console.error);

examples/run-zod.ts

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

examples/run.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env -S npm run tsn -T
22

33
import { BrowserUse } from 'browser-use-sdk';
4+
import { z } from 'zod';
45

56
import { env } from './utils';
67

@@ -9,15 +10,55 @@ env();
910
// gets API Key from environment variable BROWSER_USE_API_KEY
1011
const browseruse = new BrowserUse();
1112

12-
async function main() {
13-
console.log(`Running Task...`);
13+
async function basic() {
14+
console.log(`Basic: Running Task...`);
1415

1516
// Create Task
1617
const rsp = await browseruse.tasks.run({
1718
task: "What's the weather line in SF and what's the temperature?",
19+
agentSettings: { llm: 'gemini-2.5-flash' },
1820
});
1921

20-
console.log(rsp.doneOutput);
22+
console.log(`Basic: ${rsp.doneOutput}`);
23+
24+
console.log(`Basic: DONE`);
25+
}
26+
27+
const HackerNewsResponse = z.object({
28+
title: z.string(),
29+
url: z.string(),
30+
score: z.number(),
31+
});
32+
33+
const TaskOutput = z.object({
34+
posts: z.array(HackerNewsResponse),
35+
});
36+
37+
async function structured() {
38+
console.log(`Structured: Running Task...`);
39+
40+
// Create Task
41+
const rsp = await browseruse.tasks.run({
42+
task: 'Search for the top 10 Hacker News posts and return the title, url, and score',
43+
schema: TaskOutput,
44+
agentSettings: { llm: 'gemini-2.5-flash' },
45+
});
46+
47+
const posts = rsp.parsedOutput?.posts;
48+
49+
if (posts == null) {
50+
throw new Error('Structured: No posts found');
51+
}
52+
53+
console.log(`Structured: Top Hacker News posts:`);
54+
55+
for (const post of posts) {
56+
console.log(`${post.title} (${post.score}) - ${post.url}`);
57+
}
58+
59+
console.log(`\nStructured: DONE`);
2160
}
2261

23-
main().catch(console.error);
62+
basic()
63+
.then(() => structured())
64+
.catch(console.error);

0 commit comments

Comments
 (0)