UNPKG

@brightdata/sdk

Version:

JavaScript SDK for Bright Data Web Scraping and SERP APIs

189 lines (188 loc) 6.72 kB
import { Router as DatasetsRouter } from './api/datasets/router'; import type { BdClientOptions, SaveOptions } from './types/client'; import type { ZoneInfo } from './types/zones'; import type { ScrapeJSONOptions, SearchJSONOptions, SingleJSONResponse, BatchJSONResponse, ScrapeOptions, SearchOptions, SingleRawResponse, BatchRawResponse, AnyResponse } from './types/request'; /** * Create a new bdclient instance * * @example * ```javascript * // Basic usage * const client = new bdclient({ * api_token: 'your-api-token' * }); * * // Advanced configuration * const client = new bdclient({ * api_token: 'brd-customer-hl_12345-zone-web:abc123', * auto_create_zones: true, * web_unlocker_zone: 'my_web_zone', * serp_zone: 'my_serp_zone', * log_level: 'DEBUG', * verbose: true * }); * * // Using environment variables * process.env.BRIGHTDATA_API_KEY = 'your-key'; * const client = new bdclient(); // Automatically uses env var * ``` */ export declare class bdclient { private scrapeAPI; private searchAPI; private zonesAPI; private logger; datasets: DatasetsRouter; constructor(options?: BdClientOptions); /** * Scrape a single URL using Bright Data Web Unlocker API * * Bypasses anti-bot protection and returns website content * * @example * ```javascript * // Simple scraping (returns HTML) * const html = await client.scrape('https://example.com'); * * // Get structured JSON data * const data = await client.scrape('https://example.com', { * format: 'json' * }); * * // Advanced options * const result = await client.scrape('https://example.com', { * method: 'GET', // 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' * country: 'us', // 'us' | 'gb' | 'de' | 'jp' etc. * format: 'raw', // 'raw' | 'json' * dataFormat: 'markdown', // 'html' | 'markdown' | 'screenshot' * timeout: 30000, // 5000-300000 milliseconds * zone: 'my_custom_zone' // Custom zone name * }); * * // E-commerce scraping * const productData = await client.scrape('https://amazon.com/dp/B123', { * format: 'json', * country: 'us' * }); * ``` */ scrape(url: string, opts?: ScrapeJSONOptions): Promise<SingleJSONResponse>; scrape(url: string, opts?: ScrapeOptions): Promise<SingleRawResponse>; scrape(url: string[], opts?: ScrapeJSONOptions): Promise<BatchJSONResponse>; scrape(url: string[], opts?: ScrapeOptions): Promise<BatchRawResponse>; /** * Search using a single query via Bright Data SERP API * * Performs web search on Google, Bing, or Yandex with anti-bot protection bypass * * @example * ```javascript * // Simple Google search * const results = await client.search('pizza restaurants'); * * // Structured search results * const data = await client.search('best laptops 2024', { * format: 'json' * }); * * // Advanced search options * const results = await client.search('machine learning courses', { * searchEngine: 'bing', // 'google' | 'bing' | 'yandex' * country: 'us', // 'us' | 'gb' | 'de' | 'jp' etc. * format: 'json', // 'raw' | 'json' * dataFormat: 'markdown', // 'html' | 'markdown' | 'screenshot' * timeout: 20000, // 5000-300000 milliseconds * zone: 'my_serp_zone' // Custom zone * }); * * // Different search engines * const googleResults = await client.search('nodejs tutorial', { * searchEngine: 'google', * country: 'us' * }); * * const bingResults = await client.search('nodejs tutorial', { * searchEngine: 'bing', * country: 'us' * }); * * const yandexResults = await client.search('nodejs tutorial', { * searchEngine: 'yandex', * country: 'ru' * }); * ``` */ search(query: string, options: SearchJSONOptions): Promise<SingleJSONResponse>; search(query: string, options?: SearchOptions): Promise<SingleRawResponse>; search(query: string[], options: SearchJSONOptions): Promise<BatchJSONResponse>; search(query: string[], options?: SearchOptions): Promise<BatchRawResponse>; /** * Write content to a local file * * Saves scraped data or search results to disk in various formats * * @example * ```javascript * // Save scraped data as JSON * const data = await client.scrape('https://example.com'); * const filePath = await client.saveResults(data, { * filename: 'scraped_data.json', * format: 'json', * }); * * // Auto-generate filename * const filePath = await client.saveResults(data); * // Creates: brightdata_content_1758705609651.json * * // Save as plain text * const html = await client.scrape('https://example.com'); * const txtPath = await client.saveResults(html, { * filename: 'page.txt', * format: 'txt', * }); * * // Different formats * await client.saveResults(data, { * filename: 'data.json', * format: 'json', // JSON format * }); * await client.saveResults(data, { * filename: 'data.txt', * format: 'txt', // Text format * }); * ``` */ saveResults(content: AnyResponse, options?: SaveOptions): Promise<string>; /** * List all active zones in your Bright Data account * * Retrieves information about available proxy zones and their status * * @example * ```javascript * // List all zones * const zones = await client.listZones(); * * // Process zone information * for (let zone of zones) { * console.log(`Zone: ${zone.name}`); * console.log(`Type: ${zone.type}`); * console.log(`Status: ${zone.status}`); * console.log(`IPs: ${zone.ips}`); * console.log(`Bandwidth: ${zone.bandwidth}`); * console.log('---'); * }; * * // Find specific zone * const webZone = zones.find(z => z.name === 'web_unlocker_1'); * if (webZone) { * console.log(`Found zone: ${webZone.name}, Status: ${webZone.status}`); * } * * // Check zone availability * const activeZones = zones.filter(z => z.status === 'active'); * console.log(`Active zones: ${activeZones.length}`); * ``` */ listZones(): Promise<ZoneInfo[]>; }