Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ import Browserbase from 'browserbase';

const client = new Browserbase({
apiKey: process.env['BROWSERBASE_API_KEY'], // This is the default and can be omitted
environment: 'development', // or 'production' | 'local'; defaults to 'production'
});

async function main() {
const context = await client.contexts.create({ projectId: 'projectId' });
const session = await client.sessions.create({ projectId: 'your_project_id', proxies: true });

console.log(context.id);
console.log(session.id);
}

main();
Expand All @@ -49,12 +48,11 @@ import Browserbase from 'browserbase';

const client = new Browserbase({
apiKey: process.env['BROWSERBASE_API_KEY'], // This is the default and can be omitted
environment: 'development', // or 'production' | 'local'; defaults to 'production'
});

async function main() {
const params: Browserbase.ContextCreateParams = { projectId: 'projectId' };
const context: Browserbase.ContextCreateResponse = await client.contexts.create(params);
const params: Browserbase.SessionCreateParams = { projectId: 'your_project_id', proxies: true };
const session: Browserbase.SessionCreateResponse = await client.sessions.create(params);
}

main();
Expand All @@ -71,15 +69,17 @@ a subclass of `APIError` will be thrown:
<!-- prettier-ignore -->
```ts
async function main() {
const context = await client.contexts.create({ projectId: 'projectId' }).catch(async (err) => {
if (err instanceof Browserbase.APIError) {
console.log(err.status); // 400
console.log(err.name); // BadRequestError
console.log(err.headers); // {server: 'nginx', ...}
} else {
throw err;
}
});
const session = await client.sessions
.create({ projectId: 'your_project_id', proxies: true })
.catch(async (err) => {
if (err instanceof Browserbase.APIError) {
console.log(err.status); // 400
console.log(err.name); // BadRequestError
console.log(err.headers); // {server: 'nginx', ...}
} else {
throw err;
}
});
}

main();
Expand Down Expand Up @@ -114,7 +114,7 @@ const client = new Browserbase({
});

// Or, configure per-request:
await client.contexts.create({ projectId: 'projectId' }, {
await client.sessions.create({ projectId: 'your_project_id', proxies: true }, {
maxRetries: 5,
});
```
Expand All @@ -131,7 +131,7 @@ const client = new Browserbase({
});

// Override per-request:
await client.contexts.create({ projectId: 'projectId' }, {
await client.sessions.create({ projectId: 'your_project_id', proxies: true }, {
timeout: 5 * 1000,
});
```
Expand All @@ -152,15 +152,15 @@ You can also use the `.withResponse()` method to get the raw `Response` along wi
```ts
const client = new Browserbase();

const response = await client.contexts.create({ projectId: 'projectId' }).asResponse();
const response = await client.sessions.create({ projectId: 'your_project_id', proxies: true }).asResponse();
console.log(response.headers.get('X-My-Header'));
console.log(response.statusText); // access the underlying Response object

const { data: context, response: raw } = await client.contexts
.create({ projectId: 'projectId' })
const { data: session, response: raw } = await client.sessions
.create({ projectId: 'your_project_id', proxies: true })
.withResponse();
console.log(raw.headers.get('X-My-Header'));
console.log(context.id);
console.log(session.id);
```

### Making custom/undocumented requests
Expand Down Expand Up @@ -264,8 +264,8 @@ const client = new Browserbase({
});

// Override per-request:
await client.contexts.create(
{ projectId: 'projectId' },
await client.sessions.create(
{ projectId: 'your_project_id', proxies: true },
{
httpAgent: new http.Agent({ keepAlive: false }),
},
Expand Down
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ or products provided by Browserbase please follow the respective company's secur

### Browserbase Terms and Policies

Please contact dev-feedback@browserbase.com for any questions or concerns regarding security of our services.
Please contact support@browserbase.com for any questions or concerns regarding security of our services.

---

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "browserbase",
"version": "0.1.0-alpha.1",
"description": "The official TypeScript library for the Browserbase API",
"author": "Browserbase <dev-feedback@browserbase.com>",
"author": "Browserbase <support@browserbase.com>",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"type": "commonjs",
Expand Down
29 changes: 2 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,12 @@ import { type Agent } from './_shims/index';
import * as Core from './core';
import * as API from './resources/index';

const environments = {
production: 'https://api.browserbase.com',
development: 'https://api.dev.browserbase.com',
local: 'http://api.localhost',
};
type Environment = keyof typeof environments;

export interface ClientOptions {
/**
* Your [Browserbase API Key](https://www.browserbase.com/settings).
*/
apiKey?: string | undefined;

/**
* Specifies the environment to use for the API.
*
* Each environment maps to a different base URL:
* - `production` corresponds to `https://api.browserbase.com`
* - `development` corresponds to `https://api.dev.browserbase.com`
* - `local` corresponds to `http://api.localhost`
*/
environment?: Environment;

/**
* Override the default base URL for the API, e.g., "https://api.example.com/v2/"
*
Expand Down Expand Up @@ -98,7 +81,6 @@ export class Browserbase extends Core.APIClient {
* API Client for interfacing with the Browserbase API.
*
* @param {string | undefined} [opts.apiKey=process.env['BROWSERBASE_API_KEY'] ?? undefined]
* @param {Environment} [opts.environment=production] - Specifies the environment URL to use for the API.
* @param {string} [opts.baseURL=process.env['BROWSERBASE_BASE_URL'] ?? https://api.browserbase.com] - Override the default base URL for the API.
* @param {number} [opts.timeout=1 minute] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
* @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections.
Expand All @@ -121,18 +103,11 @@ export class Browserbase extends Core.APIClient {
const options: ClientOptions = {
apiKey,
...opts,
baseURL,
environment: opts.environment ?? 'production',
baseURL: baseURL || `https://api.browserbase.com`,
};

if (baseURL && opts.environment) {
throw new Errors.BrowserbaseError(
'Ambiguous URL; The `baseURL` option (or BROWSERBASE_BASE_URL env var) and the `environment` option are given. If you want to use the environment you must pass baseURL: null',
);
}

super({
baseURL: options.baseURL || environments[options.environment || 'production'],
baseURL: options.baseURL!,
timeout: options.timeout ?? 60000 /* 1 minute */,
httpAgent: options.httpAgent,
maxRetries: options.maxRetries,
Expand Down
13 changes: 0 additions & 13 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,6 @@ describe('instantiate client', () => {
const client = new Browserbase({ apiKey: 'My API Key' });
expect(client.baseURL).toEqual('https://api.browserbase.com');
});

test('env variable with environment', () => {
process.env['BROWSERBASE_BASE_URL'] = 'https://example.com/from_env';

expect(
() => new Browserbase({ apiKey: 'My API Key', environment: 'production' }),
).toThrowErrorMatchingInlineSnapshot(
`"Ambiguous URL; The \`baseURL\` option (or BROWSERBASE_BASE_URL env var) and the \`environment\` option are given. If you want to use the environment you must pass baseURL: null"`,
);

const client = new Browserbase({ apiKey: 'My API Key', baseURL: null, environment: 'production' });
expect(client.baseURL).toEqual('https://api.browserbase.com');
});
});

test('maxRetries option is correctly set', () => {
Expand Down