SKILL.md
Bright Data Webhooks & Events
Overview
Handle Bright Data webhook deliveries from the Web Scraper API and Datasets API. When you trigger an async collection, Bright Data sends the results to your webhook URL with the collected data in JSON, NDJSON, or CSV format.
Prerequisites
- Web Scraper API or Datasets API configured
- HTTPS endpoint accessible from internet
- API token for webhook Authorization header
Instructions
Step 1: Configure Webhook URL When Triggering Collection
// trigger-with-webhook.ts
const API_TOKEN = process.env.BRIGHTDATA_API_TOKEN!;
async function triggerWithWebhook(datasetId: string, urls: string[]) {
const params = new URLSearchParams({
dataset_id: datasetId,
format: 'json',
endpoint: 'https://your-app.com/webhooks/brightdata', // Your webhook URL
uncompressed_webhook: 'true', // Send uncompressed for easier handling
auth_header: `Bearer ${process.env.BRIGHTDATA_WEBHOOK_SECRET}`, // Auth header sent with delivery
});
// Optional: notification URL (lightweight ping when done)
params.set('notify', 'https://your-app.com/webhooks/brightdata-notify');
const response = await fetch(
`https://api.brightdata.com/datasets/v3/trigger?${params}`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${API_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(urls.map(url => ({ url }))),
}
);
const result = await response.json();
console.log('Snapshot ID:', result.snapshot_id);
return result;
}
Step 2: Webhook Endpoint — Receive Data Delivery
// api/webhooks/brightdata.ts
import express from 'express';
const app = express();
// Bright Data sends collected data as JSON array
app.post('/webhooks/brightdata',
express.json({ limit: '50mb' }), // Collections can be large
async (req, res) => {
// Validate Authorization header
const authHeader = req.headers.authorization;
if (authHeader !== `Bearer ${process.env.BRIGHTDATA_WEBHOOK_SECRET}`) {
console.error('Invalid webhook authorization');
return res.status(401).json({ error: 'Unauthorized' });
}
const records = req.body; // Array of scraped records
console.log(`Received ${records.length} records`);
// Process records
for (const record of records) {
console.log(`URL: ${record.url}`);
console.log(`Title: ${record.title}`);
console.log(`Data: ${JSON.stringify(record).substring(0, 200)}`);
}
// Store results
await saveToDatabase(records);
// Return 200 quickly — Bright Data retries on non-2xx
res.status(200).json({ received: records.length });
}
);
