Skip to content

Commit e896a6c

Browse files
committed
improve examples
1 parent bff0d1c commit e896a6c

File tree

10 files changed

+77
-25
lines changed

10 files changed

+77
-25
lines changed

examples/.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Browser Use API Key
2+
BROWSER_USE_API_KEY=""
3+
4+
# Webhook Verification Secret key
5+
SECRET_KEY=""

examples/demo.ts

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

33
import { BrowserUse } from 'browser-use-sdk';
4-
import { spinner } from './utils';
4+
5+
import { env, spinner } from './utils';
6+
7+
env();
58

69
// gets API Key from environment variable BROWSER_USE_API_KEY
710
const browseruse = new BrowserUse();

examples/stream-zod.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import { BrowserUse } from 'browser-use-sdk';
44
import z from 'zod';
55

6+
import { env } from './utils';
7+
8+
env();
9+
610
const HackerNewsResponse = z.object({
711
title: z.string(),
812
url: z.string(),

examples/stream.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import { BrowserUse } from 'browser-use-sdk';
44

5+
import { env } from './utils';
6+
7+
env();
8+
59
async function main() {
610
// gets API Key from environment variable BROWSER_USE_API_KEY
711
const browseruse = new BrowserUse();

examples/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import dotenv from '@dotenvx/dotenvx';
2+
13
const SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
24

35
/**
@@ -26,3 +28,7 @@ export function spinner(renderText: () => string): () => void {
2628
}
2729
};
2830
}
31+
32+
export function env() {
33+
dotenv.config({ path: [__dirname + '/.env', '.env'] });
34+
}

examples/webhook.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import {
77
} from 'browser-use-sdk/lib/webhooks';
88
import { createServer, IncomingMessage, type Server, type ServerResponse } from 'http';
99

10+
import { env } from './utils';
11+
12+
env();
13+
1014
const PORT = 3000;
11-
const WAIT_FOR_TASK_FINISH_TIMEOUT = 30_000;
15+
const WAIT_FOR_TASK_FINISH_TIMEOUT = 60_000;
1216

1317
// Environment ---------------------------------------------------------------
1418

15-
const WEBHOOK_SECRET = process.env['WEBHOOK_SECRET'];
19+
const SECRET_KEY = process.env['SECRET_KEY'];
1620

1721
// API -----------------------------------------------------------------------
1822

@@ -24,8 +28,8 @@ const browseruse = new BrowserUse();
2428
const whServerRef: { current: Server | null } = { current: null };
2529

2630
async function main() {
27-
if (!WEBHOOK_SECRET) {
28-
console.error('WEBHOOK_SECRET is not set');
31+
if (!SECRET_KEY) {
32+
console.error('SECRET_KEY is not set');
2933
process.exit(1);
3034
}
3135

@@ -58,7 +62,7 @@ async function main() {
5862
timestamp,
5963
},
6064
{
61-
secret: WEBHOOK_SECRET,
65+
secret: SECRET_KEY,
6266
},
6367
);
6468

@@ -67,7 +71,7 @@ async function main() {
6771
console.log(body);
6872
console.log(signature, 'signature');
6973
console.log(timestamp, 'timestamp');
70-
console.log(WEBHOOK_SECRET, 'WEBHOOK_SECRET');
74+
console.log(SECRET_KEY, 'SECRET_KEY');
7175

7276
res.writeHead(401, { 'Content-Type': 'application/json' });
7377
res.end(JSON.stringify({ error: 'Invalid signature' }));

examples/zod.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import { BrowserUse } from 'browser-use-sdk';
44
import { z } from 'zod';
5-
import { spinner } from './utils';
5+
6+
import { env, spinner } from './utils';
7+
8+
env();
69

710
// gets API Key from environment variable BROWSER_USE_API_KEY
811
const browseruse = new BrowserUse();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"format": "./scripts/format",
2424
"prepare": "if ./scripts/utils/check-is-in-git-install.sh; then ./scripts/build && ./scripts/utils/git-swap.sh; fi",
2525
"tsn": "ts-node -r tsconfig-paths/register",
26+
"cli": "ts-node -r tsconfig-paths/register --cwd $PWD ./src/lib/bin/cli.ts",
2627
"lint": "./scripts/lint",
2728
"fix": "./scripts/format"
2829
},

src/lib/bin/auth.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,62 @@ import * as dotenv from '@dotenvx/dotenvx';
22

33
import { BrowserUse } from '../../';
44

5-
const ENV_VAR_KEY = 'BROWSER_USE_API_KEY';
5+
const API_KEY_ENV_VAR_KEY = 'BROWSER_USE_API_KEY';
66

77
/**
88
* Creates a new BrowserUse client with the API key from the environment variable.
99
*/
1010
export function createBrowserUseClient() {
1111
let apiKey: string | null = null;
1212

13-
if (process.env[ENV_VAR_KEY]) {
14-
apiKey = process.env[ENV_VAR_KEY];
13+
if (process.env[API_KEY_ENV_VAR_KEY]) {
14+
apiKey = process.env[API_KEY_ENV_VAR_KEY];
1515
}
1616

1717
if (apiKey == null) {
1818
const env = dotenv.config({ path: '.env' });
1919

20-
const envApiKey = env.parsed?.[ENV_VAR_KEY];
20+
const envApiKey = env.parsed?.[API_KEY_ENV_VAR_KEY];
2121

2222
if (envApiKey) {
2323
apiKey = envApiKey;
2424
}
2525
}
2626

2727
if (apiKey == null) {
28-
console.error(`Missing BROWSER_USE_API_KEY environment variable!`);
28+
console.error(`Missing ${API_KEY_ENV_VAR_KEY} environment variable!`);
2929
process.exit(1);
3030
}
3131

3232
return new BrowserUse({ apiKey });
3333
}
34+
35+
const SECRET_ENV_VAR_KEY = 'SECRET_KEY';
36+
37+
/**
38+
* Loads the Browser Use webhook secret from the environment variable.
39+
*/
40+
export function getBrowserUseWebhookSecret() {
41+
let secret: string | null = null;
42+
43+
if (process.env[SECRET_ENV_VAR_KEY]) {
44+
secret = process.env[SECRET_ENV_VAR_KEY];
45+
}
46+
47+
if (secret == null) {
48+
const env = dotenv.config({ path: '.env' });
49+
50+
const envSecret = env.parsed?.[SECRET_ENV_VAR_KEY];
51+
52+
if (envSecret) {
53+
secret = envSecret;
54+
}
55+
}
56+
57+
if (secret == null) {
58+
console.error(`Missing ${SECRET_ENV_VAR_KEY} environment variable!`);
59+
process.exit(1);
60+
}
61+
62+
return secret;
63+
}

src/lib/bin/commands/listen.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Command } from 'commander';
22

33
import { BrowserUse } from '../../../';
44
import { createWebhookSignature, Webhook } from '../../webhooks';
5-
import { createBrowserUseClient } from '../auth';
5+
import { createBrowserUseClient, getBrowserUseWebhookSecret } from '../auth';
66

77
// NOTE: We perform task list refresh to get all running tasks and then
88
const tickRef: {
@@ -13,22 +13,14 @@ const tickRef: {
1313
export const listen = new Command('listen')
1414
.description(`Open a local webhook to receive Cloud API updates from the CLI on your local machine.`)
1515
.option('-d, --dev <endpoint>', 'The endpoint to forward updates to.')
16-
.option('--secret <secret>', 'The secret to use for webhook signature verification.')
1716
.action(async (options) => {
18-
//
19-
20-
const { secret } = options;
21-
22-
if (secret == null) {
23-
console.error('Missing --secret option');
24-
process.exit(1);
25-
}
26-
27-
//
2817
// Auth
18+
2919
const client = createBrowserUseClient();
20+
const secret = getBrowserUseWebhookSecret();
3021

3122
// Proxy
23+
3224
const { dev: localTargetEndpoint } = options;
3325

3426
if (typeof localTargetEndpoint !== 'string') {

0 commit comments

Comments
 (0)