### Running the Node.js Script Setup Source: https://context7.com/luminati-io/bright-data-web-unlocker-nodejs-project/llms.txt Provides step-by-step instructions for setting up and running the Node.js project from a clean environment. Includes cloning the repository, setting environment variables, and executing the script. ```bash # 1. Clone the repository git clone https://github.com/luminati-io/bright-data-web-unlocker-nodejs-project.git cd bright-data-web-unlocker-nodejs-project # 2. Set credentials via environment variables export BRIGHT_DATA_API_TOKEN="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" export BRIGHT_DATA_ZONE="web_unlocker1" # 3. (Optional) Override the target URL inline BRIGHT_DATA_TARGET_URL="https://example.com" node index.js # Or edit CONFIG.targetUrl in index.js directly # 4. Run node index.js # Expected console output: # 🔄 Fetching https://geo.brdtest.com/welcome.txt through Bright Data Unlocker... # ✅ Request successful! # 📊 Response data: { status: 200, body: 'Welcome to Bright Data!', headers: { ... } } ``` -------------------------------- ### StackBlitz Setup and Execution Source: https://context7.com/luminati-io/bright-data-web-unlocker-nodejs-project/llms.txt Follow these steps to run the project directly in your browser using StackBlitz. Ensure you configure your Bright Data API token and zone in the `.env` file before running. ```bash 1. Open: https://stackblitz.com/~/github.com/luminati-io/bright-data-web-unlocker-nodejs-project?file=index.js 2. In StackBlitz, select the `.env` file (or create one) and add: BRIGHT_DATA_API_TOKEN=your_token_here BRIGHT_DATA_ZONE=web_unlocker1 3. Click "Run" or open the terminal and execute: node index.js 4. The output panel will display the unlocked page response. ``` -------------------------------- ### Configure Bright Data API Credentials Source: https://github.com/luminati-io/bright-data-web-unlocker-nodejs-project/blob/main/README.md Set your Bright Data API token and zone name. Use environment variables for security or configure directly in the script. Ensure the target URL is also correctly set. ```javascript const CONFIG = { apiToken: process.env.BRIGHT_DATA_API_TOKEN || 'YOUR_API_TOKEN', // Replace with your actual token zone: process.env.BRIGHT_DATA_ZONE || 'web_unlocker1', // Replace with your zone targetUrl: 'https://geo.brdtest.com/welcome.txt' // Replace with your target URL }; ``` -------------------------------- ### Equivalent `curl` Request for Bright Data API Source: https://context7.com/luminati-io/bright-data-web-unlocker-nodejs-project/llms.txt This `curl` command demonstrates how to make the same API call as the Node.js script. It's useful for testing credentials and zones independently before running the full script. ```bash curl -X POST https://api.brightdata.com/request \ -H "Authorization: Bearer $BRIGHT_DATA_API_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "zone": "web_unlocker1", "url": "https://geo.brdtest.com/welcome.txt", "format": "json" }' # Expected response: # { "status": 200, "body": "...", "headers": { "content-type": "text/plain", ... } } ``` -------------------------------- ### Configure Bright Data API Access Source: https://context7.com/luminati-io/bright-data-web-unlocker-nodejs-project/llms.txt Centralizes runtime settings for the script. Supports environment variables for secure configuration or direct values for local testing. ```javascript // Option 1: Environment variables (recommended for production/CI) // export BRIGHT_DATA_API_TOKEN=your_token_here // export BRIGHT_DATA_ZONE=web_unlocker1 // node index.js // Option 2: Direct configuration (quick local testing only) const CONFIG = { apiToken: process.env.BRIGHT_DATA_API_TOKEN || 'YOUR_API_TOKEN', // from https://brightdata.com/cp/setting/users zone: process.env.BRIGHT_DATA_ZONE || 'web_unlocker1', // from https://brightdata.com/cp/zones targetUrl: 'https://geo.brdtest.com/welcome.txt' // any URL you want to unlock }; ``` -------------------------------- ### Fetch Data with Bright Data Unlocker API Source: https://context7.com/luminati-io/bright-data-web-unlocker-nodejs-project/llms.txt An async function that posts a JSON request to the Bright Data Unlocker API endpoint. It validates the API token, builds the request with the correct authorization header and body, checks for HTTP-level errors, parses the JSON response, and re-throws any errors. ```javascript async function fetchWithBrightData() { // Guard: warn if placeholder token is still set if (CONFIG.apiToken === 'YOUR_API_TOKEN') { console.warn('⚠️ Please set your actual API token before making requests'); } console.log(`🔄 Fetching ${CONFIG.targetUrl} through Bright Data Unlocker...`); const response = await fetch('https://api.brightdata.com/request', { method: 'POST', headers: { 'Authorization': `Bearer ${CONFIG.apiToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ zone: CONFIG.zone, // identifies the Web Unlocker product zone url: CONFIG.targetUrl, // the site to fetch through the unlocker format: 'json' // ask for a structured JSON response }) }); // Throw on any non-2xx HTTP status if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); console.log('✅ Request successful!'); return data; } // Run and handle the result fetchWithBrightData() .then(data => { console.log('📊 Response data:', data); // Expected output structure (varies by target URL): // 📊 Response data: { status: 200, body: '...page content...', headers: { ... } } }) .catch(error => { console.error('❌ Error:', error.message); process.exit(1); // non-zero exit so shell scripts / CI detect the failure }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.