From d59e074efb40674c9f0c0529caa3554ef414698b Mon Sep 17 00:00:00 2001 From: Kunwar Date: Wed, 25 Nov 2026 09:58:16 -0800 Subject: [PATCH] feat(api): fetch accepts allowedDomains --- examples/fetch-allowed-domains.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 examples/fetch-allowed-domains.ts diff --git a/examples/fetch-allowed-domains.ts b/examples/fetch-allowed-domains.ts new file mode 100644 index 00000000..e984330a --- /dev/null +++ b/examples/fetch-allowed-domains.ts @@ -0,0 +1,30 @@ +import Browserbase from '@browserbasehq/sdk'; + +const BROWSERBASE_API_KEY = process.env['BROWSERBASE_API_KEY']!; + +const bb = new Browserbase({ + apiKey: BROWSERBASE_API_KEY, +}); + +(async () => { + // A host on the list (or a subdomain of one) is fetched as usual. + const page = await bb.fetchAPI.create({ + url: 'https://example.com/', + allowedDomains: ['example.com'], + }); + console.log(`Fetched ${page.statusCode} from example.com`); + + // Any other host, or a redirect to one, is rejected with a 400 before it is reached. + try { + await bb.fetchAPI.create({ + url: 'https://example.com/', + allowedDomains: ['example.org'], + }); + } catch (error) { + if (error instanceof Browserbase.BadRequestError) { + console.log(`Blocked: ${error.message}`); + } else { + throw error; + } + } +})().catch((error) => console.error(error.message));