@brightdata/sdk

中文文档
1.2.0发布于 2026/08/02MITTypeScriptESM + CJS构建来源已验证近 30 天下载 7.3万

JavaScript SDK for Bright Data Web Scraping and SERP APIs

原始使用文档
文档目录32 节
  1. Bright Data JavaScript SDK providing easy and scalable meth…
  2. Installation
  3. Quick start
  4. 1. Signup and get your API token
  5. 2. Initialize the client
  6. 3. Launch your first request
  7. Features
  8. Usage
  9. Scrape websites
  10. Search engines
  11. Platform scrapers
  12. Crawl API
  13. Discover API
  14. Scraper Studio
  15. Browser API
  16. Datasets
  17. Saving results
  18. Configuration
  19. Runtime support
  20. API Token
  21. Environment Variables
  22. Client Options
  23. Resource Cleanup
  24. Constants
  25. Zone Management
  26. Subpath Exports
  27. Error Handling
  28. Troubleshooting
  29. Windows & corporate networks
  30. AuthenticationError: invalid API key
  31. Support
  32. License
sdk-banner(1)

Bright Data JavaScript SDK providing easy and scalable methods for scraping, web search, datasets, and more.

Installation @latest

npm install @brightdata/sdk

Quick start

1. Signup and get your API token

2. Initialize the client

import { bdclient } from '@brightdata/sdk';

const client = new bdclient({
    apiKey: '[your_api_token]', // or set BRIGHTDATA_API_TOKEN env variable
});

3. Launch your first request

// Scrape a webpage
const html = await client.scrapeUrl('https://example.com');
console.log(html);

// Search the web
const results = await client.search.google('pizza restaurants', { country: 'us' });
console.log(results);

Don't forget to close when done:

await client.close();

Features

  • Web Scraping — Scrape any website using anti-bot detection bypass and proxy support
  • Search Engine Results — Google, Bing, and Yandex search with batch support
  • Platform Scrapers — Structured data collection from LinkedIn, Amazon, Instagram, TikTok, YouTube, Reddit, and more
  • Crawl API — Crawl any URL(s) and get every output format (markdown, HTML, text) bundled per page
  • Discover API — AI-powered web search with intent-based relevance ranking
  • Scraper Studio — Trigger and fetch results from custom scrapers built in Bright Data's Scraper Studio
  • Browser API — CDP WebSocket URLs for connecting Playwright, Puppeteer, or Selenium to Bright Data's cloud browsers
  • Datasets — Access 126 pre-built datasets across dozens of platforms with query/download support
  • Parallel Processing — Concurrent processing for multiple URLs or queries
  • Robust Error Handling — Typed error classes with retry logic
  • Zone Management — Automatic zone creation and management
  • Multiple Output Formats — HTML, JSON, Markdown, and screenshots
  • Dual Build — Both ESM and CommonJS supported
  • TypeScript — Fully typed API with overloaded signatures
  • Subpath Exports — Tree-shakeable imports via @brightdata/sdk/scrapers, @brightdata/sdk/search, @brightdata/sdk/datasets

Usage

Scrape websites

// Single URL — returns HTML string by default
const html = await client.scrapeUrl('https://example.com');

// Multiple URLs (parallel processing)
const results = await client.scrapeUrl([
    'https://example1.com',
    'https://example2.com',
]);

// Get markdown content
const md = await client.scrapeUrl('https://example.com', {
    dataFormat: 'markdown',
});

// Get structured JSON
const data = await client.scrapeUrl('https://example.com', {
    format: 'json',
});

// Take a screenshot
const screenshot = await client.scrapeUrl('https://example.com', {
    dataFormat: 'screenshot',
});

// Full options
const result = await client.scrapeUrl('https://example.com', {
    format: 'raw',            // 'raw' (default) or 'json'
    dataFormat: 'html',       // 'html' (default), 'markdown' (alias: 'md'), 'screenshot'
    country: 'gb',            // two-letter country code
    method: 'GET',            // HTTP method
});

Search engines

// Google search
const results = await client.search.google('pizza restaurants');

// Bing search
const results = await client.search.bing('pizza restaurants');

// Yandex search
const results = await client.search.yandex('pizza restaurants');

// Batch search (parallel)
const results = await client.search.google(['pizza', 'sushi', 'tacos']);

// With options
const results = await client.search.google('pizza', {
    country: 'gb',
    format: 'json',
});

Note: If country is not specified, requests exit through an arbitrary proxy location, so results may be geo-located to an unexpected country. Always pass country when you need consistent, localized results.

Platform scrapers

Collect structured data from popular platforms. Each platform supports sync collection (collect*) and async orchestrated scraping (trigger, poll, download).

// LinkedIn profiles
const data = await client.scrape.linkedin.collectProfiles(
    ['https://www.linkedin.com/in/satyanadella/'],
    { format: 'json' },
);

// Amazon products
const data = await client.scrape.amazon.collectProducts(
    ['https://www.amazon.com/dp/B0D77BX8Y4'],
    { format: 'json' },
);

// Instagram profiles
const data = await client.scrape.instagram.collectProfiles(
    ['https://www.instagram.com/natgeo/'],
    { format: 'json' },
);

// TikTok profiles
const data = await client.scrape.tiktok.collectProfiles(
    ['https://www.tiktok.com/@tiktok'],
    { format: 'json' },
);

// YouTube videos
const data = await client.scrape.youtube.collectVideos(
    ['https://www.youtube.com/watch?v=dQw4w9WgXcQ'],
    { format: 'json' },
);

// Reddit posts
const data = await client.scrape.reddit.collectPosts(
    ['https://www.reddit.com/r/technology/top/'],
    { format: 'json' },
);

Orchestrated scraping (async trigger → poll → download):

const result = await client.scrape.linkedin.profiles(
    ['https://www.linkedin.com/in/satyanadella/'],
    { pollInterval: 5000, pollTimeout: 180_000 },
);
console.log(result.data);    // structured data
console.log(result.status);  // 'ready'
console.log(result.rowCount);

Available platforms: linkedin, amazon, instagram, tiktok, youtube, reddit, facebook, pinterest, chatGPT, digikey, perplexity

Crawl API

Crawl one or more URLs and get every output format (markdown, HTML, text) bundled per page.

// Sync — single round-trip
const result = await client.crawler.crawl('https://example.com');
console.log(result.data[0].markdown);

// Batch
const result = await client.crawler.crawl([
    'https://example.com',
    'https://example.com/about',
]);
console.log(`${result.pageCount} pages`);

// Async — trigger, poll, download
const job = await client.crawler.trigger('https://example.com');
const status = await client.crawler.status(job.snapshotId);
const result = await client.crawler.download(job.snapshotId);

Discover API

AI-powered web search with relevance ranking based on intent.

discover() resolves to a DiscoverResult wrapper (not a bare array). The items are on result.data (or its alias result.results), and the result is iterable. On failure result.success is false, result.error carries the reason, and result.data / result.results stay an empty array — so iterating never throws.

// Basic search
const result = await client.discover('artificial intelligence trends 2026');

if (!result.success) {
    console.error('discover failed:', result.error);
} else {
    console.log(result.results); // [{ link, title, description, relevance_score }, ...]
    for (const item of result) {
        console.log(`[${item.relevance_score}] ${item.title}`);
    }
}

// With intent for semantic ranking
const result = await client.discover('Tesla battery technology', {
    intent: 'recent breakthroughs in EV battery chemistry',
});

// With filtering and localization
const result = await client.discover('sustainable fashion brands', {
    intent: 'eco-friendly clothing companies',
    filterKeywords: ['sustainability', 'eco-friendly', 'organic'],
    country: 'us',
    numResults: 10,
});

// Include full page content
const result = await client.discover('python asyncio tutorial', {
    includeContent: true,
    numResults: 3,
});

// Manual trigger/poll/fetch
const job = await client.discoverTrigger('market research SaaS', {
    intent: 'competitor pricing strategies',
});
await job.wait({ timeout: 60_000 });
const data = await job.fetch();

Scraper Studio

Trigger and fetch results from your custom scrapers built in Scraper Studio.

// Orchestrated — trigger + poll + return results
const results = await client.scraperStudio.run('c_your_collector_id', {
    input: { url: 'https://example.com/product/1' },
});
// results: RunResult[] — one per input with { input, data, error, responseId, elapsedMs }

// Multiple inputs (processed sequentially)
const results = await client.scraperStudio.run('c_your_collector_id', {
    input: [
        { url: 'https://example.com/product/1' },
        { url: 'https://example.com/product/2' },
    ],
});

// Manual control — trigger, then poll yourself
const job = await client.scraperStudio.trigger('c_your_collector_id', {
    url: 'https://example.com/product/1',
});
const data = await job.waitAndFetch();

// Check job status (by job ID from the dashboard)
const status = await client.scraperStudio.status('j_abc123');
console.log(status.status); // 'queued' | 'running' | 'done' | 'failed'

Browser API

Build CDP WebSocket URLs for connecting Playwright, Puppeteer, or Selenium to Bright Data's cloud browsers. Credentials come from browserUsername/browserPassword options or BRIGHTDATA_BROWSERAPI_USERNAME/BRIGHTDATA_BROWSERAPI_PASSWORD env vars.

// Get a connection URL
const url = client.browser.getConnectUrl();

// Geo-target the browser with a 2-letter country code
const usUrl = client.browser.getConnectUrl({ country: 'us' });

// Connect with Playwright
import { chromium } from 'playwright';
const browser = await chromium.connectOverCDP(url);
const page = await browser.newPage();
await page.goto('https://example.com');
const html = await page.content();
await browser.close();

Datasets

Access 126 pre-built datasets for querying and downloading structured data snapshots.

const ds = client.datasets;

// List all datasets available on your account
const list = await ds.list();

// Get field metadata for a dataset
const meta = await ds.instagramProfiles.getMetadata();
console.log(meta.fields); // [{ name, type, description }, ...]

// Query a dataset (triggers a snapshot)
const snapshotId = await ds.instagramProfiles.query(
    { url: 'https://www.instagram.com/natgeo/' },
    { records_limit: 10 },
);

// Check snapshot status
const status = await ds.instagramProfiles.getStatus(snapshotId);
console.log(status.status); // 'running' | 'ready' | ...

// Download when ready
const rows = await ds.instagramProfiles.download(snapshotId);

Available datasets:

PlatformDatasets
LinkedInlinkedinProfiles, linkedinCompanies
AmazonamazonProducts, amazonReviews, amazonSellers, amazonBestSellers, amazonProductsSearch, amazonProductsGlobal, amazonWalmart
InstagraminstagramProfiles, instagramPosts, instagramComments, instagramReels
TikToktiktokProfiles, tiktokPosts, tiktokComments, tiktokShop
X/TwitterxTwitterPosts, xTwitterProfiles

Saving results

const data = await client.scrapeUrl('https://example.com');
const filePath = await client.saveResults(data, {
    filename: 'results.json',
    format: 'json',
});
console.log(`Saved to: ${filePath}`);

Configuration

Runtime support

Node.js (>= 20) is the supported and tested runtime. The SDK also runs under Bun without crashing, but with a real caveat: Bun's bundled undici provides only a bare-bones Agent (no compose(), no close(), no custom dispatch), and Bun's request()/stream() ignore the dispatcher option entirely, routing through Bun's own native HTTP client instead. In practice this means requests still succeed under Bun, but none of Transport's tuning — connection pooling, keep-alive, custom timeouts, automatic retry on 429/500/502/503/504, and DNS caching — has any effect there; Bun's own defaults apply instead. The SDK detects the missing capabilities automatically (never crashes construction or close()), rather than failing.

API Token

Get your API token from Bright Data Control Panel.

Already logged in with the CLI? The SDK works with no configuration. If you've run brightdata login with the Bright Data CLI, the SDK automatically picks up those stored credentials. The token is resolved in this order:

  1. apiKey passed to new bdclient({ apiKey })
  2. BRIGHTDATA_API_TOKEN (or BRIGHTDATA_API_KEY) environment variable
  3. Credentials stored by the CLI (brightdata login)

If none are found, the client throws with instructions to log in or set a token.

Environment Variables

BRIGHTDATA_API_TOKEN=your_api_token                  # BRIGHTDATA_API_KEY also accepted
BRIGHTDATA_WEB_UNLOCKER_ZONE=your_web_unlocker_zone  # Optional
BRIGHTDATA_SERP_ZONE=your_serp_zone                  # Optional
BRIGHTDATA_BROWSERAPI_USERNAME=your_browser_username # Optional, for Browser API
BRIGHTDATA_BROWSERAPI_PASSWORD=your_browser_password # Optional, for Browser API
BRIGHTDATA_VERBOSE=1                                  # Optional, enable verbose logging

Tip: When loading these from a .env file with node --env-file=.env, note that variables already set in your shell take precedence over the file.

Client Options

const client = new bdclient({
    apiKey: 'string',           // API token (or use BRIGHTDATA_API_TOKEN env var)
    timeout: 120000,            // Request timeout in ms (1000–300000)
    autoCreateZones: true,      // Auto-create zones if they don't exist
    webUnlockerZone: 'string',  // Custom web unlocker zone name
    serpZone: 'string',         // Custom SERP zone name
    logLevel: 'INFO',           // 'DEBUG' | 'INFO' | 'WARNING' | 'ERROR' | 'CRITICAL'
    structuredLogging: true,    // Use structured JSON logging
    verbose: false,             // Enable verbose logging
    rateLimit: 0,               // Max requests per period (0 = unlimited)
    ratePeriod: 1000,           // Rate limit period in ms
});

Resource Cleanup

The client maintains HTTP connections. Always close when done:

await client.close();

// Or use Symbol.asyncDispose (TypeScript 5.2+)
await using client = new bdclient();

Constants

ConstantDefaultDescription
DEFAULT_CONCURRENCY10Max parallel tasks
DEFAULT_TIMEOUT120000Request timeout (milliseconds)
MAX_RETRIES3Retry attempts on failure
RETRY_BACKOFF_FACTOR1.5Exponential backoff multiplier

Zone Management

const zones = await client.listZones();
console.log(`Found ${zones.length} zones`);

Subpath Exports

For tree-shaking or importing only what you need:

import { ScrapeRouter, LinkedinAPI } from '@brightdata/sdk/scrapers';
import { SearchRouter } from '@brightdata/sdk/search';
import { DatasetsClient, BaseDataset } from '@brightdata/sdk/datasets';

Error Handling

The SDK exports typed error classes that extend BRDError:

import { bdclient, ValidationError, AuthenticationError, BRDError } from '@brightdata/sdk';

try {
    const result = await client.scrapeUrl('https://example.com');
} catch (error) {
    if (error instanceof ValidationError) {
        console.error('Invalid input:', error.message);
    } else if (error instanceof AuthenticationError) {
        console.error('Auth failed:', error.message);
    } else if (error instanceof BRDError) {
        console.error('SDK error:', error.message);
    }
}

Error types: ValidationError, AuthenticationError, ZoneError, NetworkError, NetworkTimeoutError, TimeoutError, APIError, DataNotReadyError, FSError

Troubleshooting

Windows & corporate networks

ProblemFix
npm.ps1 cannot be loaded because running scripts is disabled (PowerShell)Use cmd instead of PowerShell, or run npm.cmd <command>, or run Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned. If overridden by Group Policy, contact your IT team.
SELF_SIGNED_CERT_IN_CHAIN during npm installYour network uses SSL inspection. Point npm to your corporate root certificate: npm config set cafile "C:\path\to\corporate-root.cer"
Certificate errors at runtimeSet the env variable NODE_EXTRA_CA_CERTS=C:\path\to\corporate-root.cer so Node trusts your corporate certificate.

AuthenticationError: invalid API key

If you get this error, check the following in order:

  1. Verify the token itself works, outside the SDK:
    curl -H "Authorization: Bearer YOUR_TOKEN" https://api.brightdata.com/zone/get_active_zones
    
    If this fails, generate a new API key with admin permissions in the control panel. Note the SDK needs an account-level API token — not a zone password.
  2. Check for shell overrides. Environment variables set in your shell take precedence over .env files loaded with node --env-file=.env. Run set BRIGHTDATA (Windows) or env | grep BRIGHTDATA (macOS/Linux) and clear any leftover values.
  3. Check your .env file: no quotes, no spaces around =, no trailing whitespace, and the token copied exactly (including dashes). Prefer LF line endings.
  4. Print what actually loaded:
    console.log(JSON.stringify(process.env.BRIGHTDATA_API_TOKEN));
    
    Hidden characters like \r will be visible in the output.

Support

For any issues, contact Bright Data support, or open an issue in this repository.

License

This project is licensed under the MIT License.

版本与发布记录

@brightdata/sdk 共发布 6 个版本,首次发布于 2025/09/26;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.2.0latest2026/08/023 个月前
1.1.02026/04/236 个月前
1.0.02026/03/297 个月前
0.2.02025/10/081.1 年前
0.1.12025/10/011.1 年前
0.1.02025/09/261.1 年前

关于 @brightdata/sdk 的常见问题

@brightdata/sdk 是什么?

JavaScript SDK for Bright Data Web Scraping and SERP APIs

如何安装 @brightdata/sdk?

在项目目录运行 npm install @brightdata/sdk,或使用 pnpm add / yarn add / bun add。

@brightdata/sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@brightdata/sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@brightdata/sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@brightdata/sdk 可以商用吗?

@brightdata/sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@brightdata/sdk 的下载量有多少?

根据 npm 官方下载统计,@brightdata/sdk 在 2026/10/09 – 2026/11/07 这 30 天内被下载 73,473 次,日均约 2,449 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@brightdata/sdk 还在维护吗?

最新版本 1.2.0 发布于 2026/08/02(3 个月前);至今共发布 6 个版本,其中过去 12 个月发布了 3 个;首次发布于 2025/09/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。