@browserbasehq/sdk 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,16 +1,96 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
- export type BrowserbaseLoadOptions = {
2
+ export type ClientOptions = {
3
+ apiKey?: string;
4
+ projectId?: string;
5
+ baseURL?: string;
6
+ };
7
+ export type LoadOptions = {
8
+ sessionId?: string;
3
9
  textContent?: boolean;
4
10
  };
5
- export type BrowserbaseScreenshotOptions = {
11
+ export type ScreenshotOptions = {
6
12
  fullPage?: boolean;
7
13
  };
14
+ export type CreateSessionOptions = {
15
+ projectId?: string;
16
+ extensionId?: string;
17
+ fingerprint?: {
18
+ browserListQuery?: string;
19
+ httpVersion?: 1 | 2;
20
+ browsers?: Array<'chrome' | 'firefox' | 'edge' | 'safari'>;
21
+ devices?: Array<'desktop' | 'mobile'>;
22
+ locales?: string[];
23
+ operatingSystems?: Array<'windows' | 'macos' | 'linux' | 'ios' | 'android'>;
24
+ screen?: {
25
+ maxHeight?: number;
26
+ maxWidth?: number;
27
+ minHeight?: number;
28
+ minWidth?: number;
29
+ };
30
+ };
31
+ };
32
+ export type Session = {
33
+ id: string;
34
+ createdAt: string;
35
+ startedAt: string;
36
+ endedAt: string;
37
+ projectId: string;
38
+ status: 'NEW' | 'CREATED' | 'ERROR' | 'RUNNING' | 'REQUEST_RELEASE' | 'RELEASING';
39
+ taskId: string;
40
+ proxyBytes: number;
41
+ expiresAt: string;
42
+ avg_cpu_usage: number;
43
+ memory_usage: number;
44
+ details: string;
45
+ logs: string;
46
+ };
47
+ export type UpdateSessionOptions = {
48
+ projectId?: string;
49
+ status: Session['status'];
50
+ };
51
+ export type SessionRecording = {
52
+ type: number;
53
+ timestamp: number;
54
+ data: object;
55
+ };
56
+ export type SessionLog = {
57
+ sessionId: string;
58
+ id: string;
59
+ timestamp: string;
60
+ method: string;
61
+ request: {
62
+ timestamp: string;
63
+ params: object;
64
+ rawBody: string;
65
+ };
66
+ response: {
67
+ timestamp: string;
68
+ result: object;
69
+ rawBody: string;
70
+ };
71
+ pageId: string;
72
+ };
73
+ export type DebugConnectionURLs = {
74
+ debuggerFullscreenUrl: string;
75
+ debuggerUrl: string;
76
+ wsUrl: string;
77
+ };
8
78
  export default class Browserbase {
9
79
  private apiKey;
10
- constructor(apiKey?: string);
11
- load(url: string | string[], options?: BrowserbaseLoadOptions): Promise<string> | AsyncGenerator<string, any, unknown>;
12
- loadURL(url: string, options?: BrowserbaseLoadOptions): Promise<string>;
13
- loadURLs(urls: string[], options?: BrowserbaseLoadOptions): AsyncGenerator<string>;
14
- screenshot(url: string, options?: BrowserbaseScreenshotOptions): Promise<Buffer>;
80
+ private projectId;
81
+ private baseURL;
82
+ constructor(options?: ClientOptions);
83
+ listSessions(): Promise<Session[]>;
84
+ createSession(options?: CreateSessionOptions): Promise<Session>;
85
+ getSession(sessionId: string): Promise<Session>;
86
+ updateSession(sessionId: string, options: UpdateSessionOptions): Promise<Session>;
87
+ getSessionRecording(sessionId: string): Promise<SessionRecording[]>;
88
+ getSessionLogs(sessionId: string): Promise<SessionLog[]>;
89
+ getSessionDownloads(sessionId: string, filePath: string, retryInterval?: number, retryCount?: number): Promise<void>;
90
+ getDebugConnectionURLs(sessionId: string): Promise<DebugConnectionURLs>;
91
+ load(url: string | string[], options?: LoadOptions): Promise<string> | AsyncGenerator<string, any, unknown>;
92
+ loadURL(url: string, options?: LoadOptions): Promise<string>;
93
+ loadURLs(urls: string[], options?: LoadOptions): AsyncGenerator<string>;
94
+ screenshot(url: string, options?: ScreenshotOptions): Promise<Buffer>;
15
95
  }
16
96
  export { Browserbase };
package/dist/index.js CHANGED
@@ -1,8 +1,108 @@
1
1
  import { chromium } from 'playwright';
2
+ import fs from 'fs/promises';
2
3
  export default class Browserbase {
3
4
  apiKey;
4
- constructor(apiKey) {
5
- this.apiKey = apiKey || process.env.BROWSERBASE_API_KEY;
5
+ projectId;
6
+ baseURL;
7
+ constructor(options = {}) {
8
+ this.apiKey = options.apiKey || process.env.BROWSERBASE_API_KEY;
9
+ this.projectId = options.projectId || process.env.BROWSERBASE_PROJECT_ID;
10
+ this.baseURL = options.baseURL || 'https://www.browserbase.com';
11
+ }
12
+ async listSessions() {
13
+ const response = await fetch(`${this.baseURL}/v1/sessions`, {
14
+ headers: {
15
+ 'x-bb-api-key': this.apiKey,
16
+ 'Content-Type': 'application/json',
17
+ },
18
+ });
19
+ return await response.json();
20
+ }
21
+ async createSession(options) {
22
+ const response = await fetch(`${this.baseURL}/v1/sessions`, {
23
+ method: 'POST',
24
+ headers: {
25
+ 'x-bb-api-key': this.apiKey,
26
+ 'Content-Type': 'application/json',
27
+ },
28
+ body: JSON.stringify({ projectId: this.projectId, ...options }),
29
+ });
30
+ return await response.json();
31
+ }
32
+ async getSession(sessionId) {
33
+ const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, {
34
+ headers: {
35
+ 'x-bb-api-key': this.apiKey,
36
+ 'Content-Type': 'application/json',
37
+ },
38
+ });
39
+ return await response.json();
40
+ }
41
+ async updateSession(sessionId, options) {
42
+ const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, {
43
+ method: 'POST',
44
+ headers: {
45
+ 'x-bb-api-key': this.apiKey,
46
+ 'Content-Type': 'application/json',
47
+ },
48
+ body: JSON.stringify({ projectId: this.projectId, ...options }),
49
+ });
50
+ return await response.json();
51
+ }
52
+ async getSessionRecording(sessionId) {
53
+ const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/recording`, {
54
+ headers: {
55
+ 'x-bb-api-key': this.apiKey,
56
+ 'Content-Type': 'application/json',
57
+ },
58
+ });
59
+ return await response.json();
60
+ }
61
+ async getSessionLogs(sessionId) {
62
+ const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/logs`, {
63
+ headers: {
64
+ 'x-bb-api-key': this.apiKey,
65
+ 'Content-Type': 'application/json',
66
+ },
67
+ });
68
+ return await response.json();
69
+ }
70
+ async getSessionDownloads(sessionId, filePath, retryInterval = 2000, retryCount = 2) {
71
+ return new Promise((resolve, reject) => {
72
+ const fetchDownload = async () => {
73
+ const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/downloads`, {
74
+ method: 'GET',
75
+ headers: {
76
+ 'x-bb-api-key': this.apiKey,
77
+ },
78
+ });
79
+ const arrayBuffer = await response.arrayBuffer();
80
+ if (arrayBuffer.byteLength > 0) {
81
+ const buffer = Buffer.from(arrayBuffer);
82
+ await fs.writeFile(filePath, buffer);
83
+ resolve();
84
+ }
85
+ else {
86
+ retryCount--;
87
+ if (retryCount <= 0) {
88
+ reject();
89
+ }
90
+ setTimeout(fetchDownload, retryInterval);
91
+ }
92
+ };
93
+ fetchDownload();
94
+ });
95
+ }
96
+ async getDebugConnectionURLs(sessionId) {
97
+ const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}/debug`, {
98
+ method: 'GET',
99
+ headers: {
100
+ 'x-bb-api-key': this.apiKey,
101
+ 'Content-Type': 'application/json',
102
+ },
103
+ });
104
+ const json = await response.json();
105
+ return json;
6
106
  }
7
107
  load(url, options = {}) {
8
108
  if (typeof url === 'string') {
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
- import Browserbase, { BrowserbaseLoadOptions } from '../index.js';
3
- export default function BrowserbaseAISDK(b: Browserbase, options?: BrowserbaseLoadOptions): {
2
+ import Browserbase, { LoadOptions } from '../index.js';
3
+ export default function BrowserbaseAISDK(b: Browserbase, options?: LoadOptions): {
4
4
  description: string;
5
5
  parameters: z.ZodObject<{
6
6
  url: z.ZodString;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@browserbasehq/sdk",
4
- "version": "1.0.1",
4
+ "version": "1.1.0",
5
5
  "description": "Browserbase JS SDK",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
package/src/index.ts CHANGED
@@ -1,21 +1,243 @@
1
1
  import { chromium } from 'playwright'
2
+ import fs from 'fs/promises'
2
3
 
3
- export type BrowserbaseLoadOptions = {
4
+ export type ClientOptions = {
5
+ apiKey?: string
6
+ projectId?: string
7
+ baseURL?: string
8
+ }
9
+
10
+ export type LoadOptions = {
11
+ sessionId?: string
4
12
  textContent?: boolean
5
13
  }
6
14
 
7
- export type BrowserbaseScreenshotOptions = {
15
+ export type ScreenshotOptions = {
8
16
  fullPage?: boolean
9
17
  }
10
18
 
19
+ export type CreateSessionOptions = {
20
+ projectId?: string
21
+ extensionId?: string
22
+ fingerprint?: {
23
+ browserListQuery?: string
24
+ httpVersion?: 1 | 2
25
+ browsers?: Array<'chrome' | 'firefox' | 'edge' | 'safari'>
26
+ devices?: Array<'desktop' | 'mobile'>
27
+ locales?: string[]
28
+ operatingSystems?: Array<'windows' | 'macos' | 'linux' | 'ios' | 'android'>
29
+ screen?: {
30
+ maxHeight?: number
31
+ maxWidth?: number
32
+ minHeight?: number
33
+ minWidth?: number
34
+ }
35
+ }
36
+ }
37
+
38
+ export type Session = {
39
+ id: string
40
+ createdAt: string
41
+ startedAt: string
42
+ endedAt: string
43
+ projectId: string
44
+ status:
45
+ | 'NEW'
46
+ | 'CREATED'
47
+ | 'ERROR'
48
+ | 'RUNNING'
49
+ | 'REQUEST_RELEASE'
50
+ | 'RELEASING'
51
+ taskId: string
52
+ proxyBytes: number
53
+ expiresAt: string
54
+ avg_cpu_usage: number
55
+ memory_usage: number
56
+ details: string
57
+ logs: string
58
+ }
59
+
60
+ export type UpdateSessionOptions = {
61
+ projectId?: string
62
+ status: Session['status']
63
+ }
64
+
65
+ export type SessionRecording = {
66
+ type: number
67
+ timestamp: number
68
+ data: object
69
+ }
70
+
71
+ export type SessionLog = {
72
+ sessionId: string
73
+ id: string
74
+ timestamp: string
75
+ method: string
76
+ request: {
77
+ timestamp: string
78
+ params: object
79
+ rawBody: string
80
+ }
81
+ response: {
82
+ timestamp: string
83
+ result: object
84
+ rawBody: string
85
+ }
86
+ pageId: string
87
+ }
88
+
89
+ export type DebugConnectionURLs = {
90
+ debuggerFullscreenUrl: string
91
+ debuggerUrl: string
92
+ wsUrl: string
93
+ }
94
+
11
95
  export default class Browserbase {
12
96
  private apiKey: string
97
+ private projectId: string
98
+ private baseURL: string
99
+
100
+ constructor(options: ClientOptions = {}) {
101
+ this.apiKey = options.apiKey || process.env.BROWSERBASE_API_KEY!
102
+ this.projectId = options.projectId || process.env.BROWSERBASE_PROJECT_ID!
103
+ this.baseURL = options.baseURL || 'https://www.browserbase.com'
104
+ }
105
+
106
+ async listSessions(): Promise<Session[]> {
107
+ const response = await fetch(`${this.baseURL}/v1/sessions`, {
108
+ headers: {
109
+ 'x-bb-api-key': this.apiKey,
110
+ 'Content-Type': 'application/json',
111
+ },
112
+ })
113
+
114
+ return await response.json()
115
+ }
116
+
117
+ async createSession(options?: CreateSessionOptions): Promise<Session> {
118
+ const response = await fetch(`${this.baseURL}/v1/sessions`, {
119
+ method: 'POST',
120
+ headers: {
121
+ 'x-bb-api-key': this.apiKey,
122
+ 'Content-Type': 'application/json',
123
+ },
124
+ body: JSON.stringify({ projectId: this.projectId, ...options }),
125
+ })
126
+
127
+ return await response.json()
128
+ }
129
+
130
+ async getSession(sessionId: string): Promise<Session> {
131
+ const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, {
132
+ headers: {
133
+ 'x-bb-api-key': this.apiKey,
134
+ 'Content-Type': 'application/json',
135
+ },
136
+ })
13
137
 
14
- constructor(apiKey?: string) {
15
- this.apiKey = apiKey || process.env.BROWSERBASE_API_KEY!
138
+ return await response.json()
16
139
  }
17
140
 
18
- load(url: string | string[], options: BrowserbaseLoadOptions = {}) {
141
+ async updateSession(
142
+ sessionId: string,
143
+ options: UpdateSessionOptions
144
+ ): Promise<Session> {
145
+ const response = await fetch(`${this.baseURL}/v1/sessions/${sessionId}`, {
146
+ method: 'POST',
147
+ headers: {
148
+ 'x-bb-api-key': this.apiKey,
149
+ 'Content-Type': 'application/json',
150
+ },
151
+ body: JSON.stringify({ projectId: this.projectId, ...options }),
152
+ })
153
+
154
+ return await response.json()
155
+ }
156
+
157
+ async getSessionRecording(sessionId: string): Promise<SessionRecording[]> {
158
+ const response = await fetch(
159
+ `${this.baseURL}/v1/sessions/${sessionId}/recording`,
160
+ {
161
+ headers: {
162
+ 'x-bb-api-key': this.apiKey,
163
+ 'Content-Type': 'application/json',
164
+ },
165
+ }
166
+ )
167
+
168
+ return await response.json()
169
+ }
170
+
171
+ async getSessionLogs(sessionId: string): Promise<SessionLog[]> {
172
+ const response = await fetch(
173
+ `${this.baseURL}/v1/sessions/${sessionId}/logs`,
174
+ {
175
+ headers: {
176
+ 'x-bb-api-key': this.apiKey,
177
+ 'Content-Type': 'application/json',
178
+ },
179
+ }
180
+ )
181
+
182
+ return await response.json()
183
+ }
184
+
185
+ async getSessionDownloads(
186
+ sessionId: string,
187
+ filePath: string,
188
+ retryInterval: number = 2000,
189
+ retryCount: number = 2
190
+ ) {
191
+ return new Promise<void>((resolve, reject) => {
192
+ const fetchDownload = async () => {
193
+ const response = await fetch(
194
+ `${this.baseURL}/v1/sessions/${sessionId}/downloads`,
195
+ {
196
+ method: 'GET',
197
+ headers: {
198
+ 'x-bb-api-key': this.apiKey,
199
+ },
200
+ }
201
+ )
202
+
203
+ const arrayBuffer = await response.arrayBuffer()
204
+ if (arrayBuffer.byteLength > 0) {
205
+ const buffer = Buffer.from(arrayBuffer)
206
+ await fs.writeFile(filePath, buffer)
207
+ resolve()
208
+ } else {
209
+ retryCount--
210
+ if (retryCount <= 0) {
211
+ reject()
212
+ }
213
+
214
+ setTimeout(fetchDownload, retryInterval)
215
+ }
216
+ }
217
+
218
+ fetchDownload()
219
+ })
220
+ }
221
+
222
+ async getDebugConnectionURLs(
223
+ sessionId: string
224
+ ): Promise<DebugConnectionURLs> {
225
+ const response = await fetch(
226
+ `${this.baseURL}/v1/sessions/${sessionId}/debug`,
227
+ {
228
+ method: 'GET',
229
+ headers: {
230
+ 'x-bb-api-key': this.apiKey,
231
+ 'Content-Type': 'application/json',
232
+ },
233
+ }
234
+ )
235
+
236
+ const json = await response.json()
237
+ return json
238
+ }
239
+
240
+ load(url: string | string[], options: LoadOptions = {}) {
19
241
  if (typeof url === 'string') {
20
242
  return this.loadURL(url, options)
21
243
  } else if (Array.isArray(url)) {
@@ -25,10 +247,7 @@ export default class Browserbase {
25
247
  }
26
248
  }
27
249
 
28
- async loadURL(
29
- url: string,
30
- options: BrowserbaseLoadOptions = {}
31
- ): Promise<string> {
250
+ async loadURL(url: string, options: LoadOptions = {}): Promise<string> {
32
251
  if (!url) {
33
252
  throw new Error('Page URL was not provided')
34
253
  }
@@ -58,7 +277,7 @@ export default class Browserbase {
58
277
 
59
278
  async *loadURLs(
60
279
  urls: string[],
61
- options: BrowserbaseLoadOptions = {}
280
+ options: LoadOptions = {}
62
281
  ): AsyncGenerator<string> {
63
282
  if (!urls.length) {
64
283
  throw new Error('Page URLs were not provided')
@@ -93,7 +312,7 @@ export default class Browserbase {
93
312
 
94
313
  async screenshot(
95
314
  url: string,
96
- options: BrowserbaseScreenshotOptions = { fullPage: false }
315
+ options: ScreenshotOptions = { fullPage: false }
97
316
  ): Promise<Buffer> {
98
317
  if (!url) {
99
318
  throw new Error('Page URL was not provided')
@@ -1,9 +1,9 @@
1
1
  import { z } from 'zod'
2
- import Browserbase, { BrowserbaseLoadOptions } from '../index.js'
2
+ import Browserbase, { LoadOptions } from '../index.js'
3
3
 
4
4
  export default function BrowserbaseAISDK(
5
5
  b: Browserbase,
6
- options: BrowserbaseLoadOptions = {}
6
+ options: LoadOptions = {}
7
7
  ) {
8
8
  return {
9
9
  description:
package/test/lib.test.ts CHANGED
@@ -10,6 +10,33 @@ describe('Browserbase', () => {
10
10
  browserbase = new Browserbase()
11
11
  })
12
12
 
13
+ it('shoud create and retrieve session', async () => {
14
+ const { id } = await browserbase.createSession()
15
+ const session = await browserbase.getSession(id)
16
+
17
+ expect(session.status).to.equal('RUNNING')
18
+ expect(session.id).to.equal(id)
19
+ })
20
+
21
+ it('shoud create and update session', async () => {
22
+ const { id } = await browserbase.createSession()
23
+ const updated = await browserbase.updateSession(id, { status: 'RELEASING' })
24
+
25
+ expect(updated.status).to.equal('COMPLETED')
26
+ })
27
+
28
+ it('shoud create a session and retrieve a recording', async () => {
29
+ const { id } = await browserbase.createSession()
30
+ const recording = await browserbase.getSessionRecording(id)
31
+
32
+ expect(recording.length).to.equal(0)
33
+ })
34
+
35
+ it('shoud create a session and get debug url', async () => {
36
+ const { id } = await browserbase.createSession()
37
+ const debug = await browserbase.getDebugConnectionURLs(id)
38
+ })
39
+
13
40
  it('should load a webpage', async () => {
14
41
  const result = await browserbase.load('https://example.com')
15
42
  expect(result).contain('Example Domain')
@@ -19,6 +46,7 @@ describe('Browserbase', () => {
19
46
  const result = await browserbase.load('https://example.com/', {
20
47
  textContent: true,
21
48
  })
49
+
22
50
  expect(result).contain('Example Domain')
23
51
  })
24
52