Quick Start: Node.js / TypeScript
Make your first API call with Node.js and parse the HTML response with cheerio.
Prerequisites
- Node.js 18+ (for built-in fetch) or Node.js 14+ with node-fetch
- A FastWebScraper API key (get one free)
Installation
Install cheerio for HTML parsing. If you're on Node.js 18+, fetch is built-in. For older versions, install node-fetch as well.
npm install cheerioOptional for Node.js < 18: npm install node-fetch
Sync Scraping (Simple)
The sync endpoint waits for the scrape to complete and returns the HTML in a single response. Best for quick scripts and testing.
// scrape.ts
const response = await fetch('https://api.fastwebscraper.com', {
method: 'POST',
headers: {
'X-API-Key': 'YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: 'https://example.com',
mode: 'auto',
}),
});
const result = await response.json();
if (!response.ok) {
console.error('Error:', result.error);
process.exit(1);
}
console.log('Status code:', result.data.statusCode);
console.log('HTML length:', result.data.html.length);Async Scraping (Production)
The async endpoint returns a job ID immediately. Poll for the result when it's ready. Use this for production workloads and batch processing.
// scrape-async.ts
// Step 1: Submit the scrape job
const submitResponse = await fetch('https://api.fastwebscraper.com', {
method: 'POST',
headers: {
'X-API-Key': 'YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: 'https://example.com',
mode: 'auto',
waitForSelector: '.main-content', // Wait for dynamic content
}),
});
const { data } = await submitResponse.json();
console.log('Job submitted:', data.jobId);
// Step 2: Poll for the result
let job;
do {
await new Promise((resolve) => setTimeout(resolve, 2000));
const statusResponse = await fetch(
'https://api.fastwebscraper.com/' + data.jobId,
{ headers: { 'X-API-Key': 'YOUR_API_KEY' } }
);
job = await statusResponse.json();
console.log('Status:', job.data.status);
} while (job.data.status === 'PENDING' || job.data.status === 'IN_PROGRESS');
if (job.data.status === 'COMPLETED') {
console.log('HTML length:', job.data.html.length);
} else {
console.error('Job failed:', job.data.error);
}Parsing HTML with cheerio
Once you have the HTML, use cheerio to extract structured data. cheerio provides a jQuery-like API for traversing and manipulating HTML.
import * as cheerio from 'cheerio';
// Assuming 'html' contains the scraped HTML string
const $ = cheerio.load(html);
// Extract text content
const pageTitle = $('h1').first().text().trim();
console.log('Page title:', pageTitle);
// Extract all links
const links = $('a[href]')
.map((_, el) => ({
text: $(el).text().trim(),
href: $(el).attr('href'),
}))
.get();
console.log('Links found:', links.length);
// Extract product data (example)
const products = $('.product-card')
.map((_, el) => ({
name: $(el).find('.product-name').text().trim(),
price: $(el).find('.product-price').text().trim(),
url: $(el).find('a').attr('href'),
}))
.get();
console.log('Products:', products);
// Extract table data
const tableRows = $('table tbody tr')
.map((_, row) => {
return [
$(row)
.find('td')
.map((_, cell) => $(cell).text().trim())
.get(),
];
})
.get();
console.log('Table rows:', tableRows);Complete Example: Scrape and Parse
Here's a complete script that scrapes a page and extracts structured data.
import * as cheerio from 'cheerio';
async function scrapeAndParse(url: string) {
// Scrape the page
const response = await fetch('https://api.fastwebscraper.com', {
method: 'POST',
headers: {
'X-API-Key': 'YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
url,
mode: 'auto',
waitForSelector: '.product-card',
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error?.message ?? 'Scrape failed');
}
const result = await response.json();
// Parse the HTML
const $ = cheerio.load(result.data.html);
const products = $('.product-card')
.map((_, el) => ({
name: $(el).find('.product-name').text().trim(),
price: $(el).find('.price').text().trim(),
image: $(el).find('img').attr('src'),
link: $(el).find('a').attr('href'),
}))
.get();
return { url, scrapedAt: new Date().toISOString(), products };
}
// Usage
const data = await scrapeAndParse('https://store.example.com/products');
console.log(JSON.stringify(data, null, 2));Error Handling
Always handle errors and implement retries for production use.
async function scrapeWithRetry(url: string, retries = 3) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const response = await fetch('https://api.fastwebscraper.com', {
method: 'POST',
headers: {
'X-API-Key': 'YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({ url, mode: 'auto' }),
signal: AbortSignal.timeout(60_000), // 60s timeout
});
if (response.status === 429) {
// Rate limited — wait and retry
const delay = Math.pow(2, attempt) * 1000;
console.warn('Rate limited. Waiting', delay, 'ms...');
await new Promise((r) => setTimeout(r, delay));
continue;
}
if (!response.ok) {
throw new Error('HTTP ' + response.status);
}
return await response.json();
} catch (error) {
if (attempt === retries) throw error;
const delay = Math.pow(2, attempt) * 1000;
console.warn('Attempt', attempt, 'failed. Retrying in', delay, 'ms...');
await new Promise((r) => setTimeout(r, delay));
}
}
}Next Steps
- API Reference — all endpoints, parameters, and response schemas
- Async vs Sync Guide — when to use each scraping mode
- Error Handling Guide — production-ready retry patterns
- Use Cases — industry-specific scraping examples