Node.js SDK
Scrape, crawl, and extract structured data from websites using the MrScraper Node SDK.
The official @mrscraper/sdk package allows you to fetch HTML, run AI scrapers, rerun existing scrapers, and retrieve results from your Node.js applications.
Requirements
- Node.js 18 or latest
- ES Modules enabled in your
package.json
{
"type": "module"
}Package Information
See @mrscraper/sdk on npm for the latest release, version history, and installation details.
Installation
Install the SDK using npm:
npm install @mrscraper/sdkAuthentication
Set your API token as an environment variable. You can get your API token from the MrScraper dashboard.
export MRSCRAPER_API_TOKEN=your_token_hereToken Override
Every SDK method accepts an optional token parameter if you need to override MRSCRAPER_API_TOKEN for specific requests.
For more information on generating API tokens, see the Generate Token guide.
Quick Start
import {
fetchHtml,
createAiScraper,
MrScraperError,
} from "@mrscraper/sdk";
try {
// 1. Fetch raw HTML from a URL
const html = await fetchHtml({
url: "https://example.com",
});
console.log(html);
// 2. Create an AI scraper (listing agent)
const scraper = await createAiScraper({
url: "https://example.com/products",
message: "Extract all product names and prices",
agent: "listing",
});
console.log(scraper);
} catch (err) {
if (err instanceof MrScraperError) {
console.error(`[${err.status ?? "network"}] ${err.message}`);
} else {
throw err;
}
}Core Methods
Fetch Raw HTML
Fetch rendered HTML or JSON text from a target URL using the MrScraper stealth browser.
import { fetchHtml } from "@mrscraper/sdk";
const html = await fetchHtml({
url: "https://example.com",
timeout: 120,
geoCode: "US",
blockResources: false,
// token: "optional_override_token"
});Parameters:
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
url | string | Yes | - | URL to fetch |
timeout | number | No | 120 | Timeout in seconds (1-600) |
geoCode | string | No | "US" | Two-letter ISO country code for proxy |
blockResources | boolean | No | false | Block images, fonts, and CSS for faster fetching |
token | string | No | - | Per-request token override |
Create AI Scraper
Create and run a new AI-powered scraper using natural language instructions.
Agent Types:
| Agent | Best For | Additional Parameters |
|---|---|---|
"general" | Single pages, product details, articles | None |
"listing" | Product listings, search results, directories | max_pages |
"map" | Site crawling, URL discovery, sitemaps | max_depth, max_pages, limit, include_patterns, exclude_patterns |
Note
For detailed information on agents, see AI Scraper Agents.
General/Listing Agent Example
import { createAiScraper } from "@mrscraper/sdk";
const scraper = await createAiScraper({
url: "https://example.com/products",
message: "Extract all product names and prices",
agent: "listing",
proxyCountry: "US",
// token: "optional_override_token"
});Map Agent Example
import { createAiScraper } from "@mrscraper/sdk";
const mapResult = await createAiScraper({
url: "https://example.com",
agent: "map",
maxDepth: 2,
maxPages: 50,
limit: 1000,
includePatterns: "/blog",
excludePatterns: "/admin",
// token: "optional_override_token"
});Parameters:
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
url | string | Yes | - | Target URL to scrape |
message | string | No | "" | Natural language instructions for extraction |
agent | "general" | "listing" | "map" | No | "general" | Agent type to use |
proxyCountry | string | null | No | - | Country code for proxy (e.g., "US", "UK") |
maxDepth | number | No | 2 | For map agent: link depth to follow |
maxPages | number | No | 50 | For map/listing agent: maximum pages to scrape |
limit | number | No | 1000 | For map agent: maximum results to return |
includePatterns | string | No | "" | For map agent: URL patterns to include (regex) |
excludePatterns | string | No | "" | For map agent: URL patterns to exclude (regex) |
token | string | No | - | Per-request token override |
Rerun AI Scraper
Rerun an existing AI scraper on a new URL without creating a new scraper configuration.
import { rerunAiScraper } from "@mrscraper/sdk";
const result = await rerunAiScraper({
scraperId: "your-scraper-id",
url: "https://example.com/new-page",
maxDepth: 2,
maxPages: 50,
limit: 1000,
includePatterns: "",
excludePatterns: "",
// token: "optional_override_token"
});Parameters:
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
scraperId | string | Yes | - | ID of the existing scraper |
url | string | Yes | - | New URL to scrape |
maxDepth | number | No | 2 | For map agents: link depth to follow |
maxPages | number | No | 50 | For map/listing agents: maximum pages to scrape |
limit | number | No | 1000 | For map agents: maximum results to return |
includePatterns | string | No | "" | For map agents: URL patterns to include |
excludePatterns | string | No | "" | For map agents: URL patterns to exclude |
token | string | No | - | Per-request token override |
Bulk Rerun AI Scraper
Run an existing AI scraper on multiple URLs in a single request.
import { bulkRerunAiScraper } from "@mrscraper/sdk";
const bulkResult = await bulkRerunAiScraper({
scraperId: "your-scraper-id",
urls: ["https://example.com/page1", "https://example.com/page2"],
// token: "optional_override_token"
});Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
scraperId | string | Yes | ID of the existing AI scraper |
urls | string[] | Yes | Array of URLs to scrape |
token | string | No | Per-request token override |
Performance Tip
Bulk operations are more efficient than individual rerun calls. Use this method when scraping multiple URLs to reduce API calls and improve performance.
Rerun Manual Scraper
Rerun an existing manual scraper (created in the MrScraper dashboard) on a new URL.
import { rerunManualScraper } from "@mrscraper/sdk";
const result = await rerunManualScraper({
scraperId: "your-manual-scraper-id",
url: "https://example.com/target",
// token: "optional_override_token"
});Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
scraperId | string | Yes | ID of the manual scraper from the dashboard |
url | string | Yes | URL to scrape |
token | string | No | Per-request token override |
Bulk Rerun Manual Scraper
Run an existing manual scraper on multiple URLs in a single request.
import { bulkRerunManualScraper } from "@mrscraper/sdk";
const bulk = await bulkRerunManualScraper({
scraperId: "your-manual-scraper-id",
urls: ["https://example.com/a", "https://example.com/b"],
// token: "optional_override_token"
});Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
scraperId | string | Yes | ID of the manual scraper from the dashboard |
urls | string[] | Yes | Array of URLs to scrape |
token | string | No | Per-request token override |
Retrieving Results
Get All Results
Retrieve a paginated list of scraping results with filtering and sorting options.
import { getAllResults } from "@mrscraper/sdk";
const results = await getAllResults({
sortField: "updatedAt",
sortOrder: "DESC",
pageSize: 10,
page: 1,
search: "example.com",
dateRangeColumn: "createdAt",
startAt: "2024-01-01T00:00:00Z",
endAt: "2024-12-31T23:59:59Z",
// token: "optional_override_token"
});Parameters:
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
sortField | string | No | "updatedAt" | Field to sort by. Supported values: "createdAt", "updatedAt", "id", "type", "url", "status", "error", "tokenUsage", "runtime" |
sortOrder | "ASC" | "DESC" | No | "DESC" | Sort direction |
pageSize | number | No | 10 | Number of results per page |
page | number | No | 1 | Page number to retrieve |
search | string | No | "" | Keyword to filter results |
dateRangeColumn | string | No | - | Date field to filter by |
startAt | string | No | - | Start date for filtering (ISO 8601 format) |
endAt | string | No | - | End date for filtering (ISO 8601 format) |
token | string | No | - | Per-request token override |
Get Result by ID
Retrieve a single scraping result using its unique ID.
import { getResultById } from "@mrscraper/sdk";
const result = await getResultById({
resultId: "your-result-id",
// token: "optional_override_token"
});Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
resultId | string | Yes | Unique identifier of the result |
token | string | No | Per-request token override |
Error Handling
All SDK methods throw MrScraperError for API, network, or timeout failures.
Exception Properties
| Property | Type | Description |
|---|---|---|
message | string | Human-readable error message |
status | number | undefined | HTTP status code (401, 429, 500, etc.) or undefined for network errors |
name | string | Always "MrScraperError" |
Example Error Handling
import { fetchHtml, MrScraperError } from "@mrscraper/sdk";
try {
await fetchHtml({ url: "https://example.com" });
} catch (err) {
if (err instanceof MrScraperError) {
console.error(`Error: ${err.message}`);
console.error(`Status: ${err.status ?? "network error"}`);
} else {
// Handle unexpected errors
throw err;
}
}Best Practices
- Always wrap SDK calls in try-catch blocks
- Check
err instanceof MrScraperErrorbefore accessing error properties - Log errors with appropriate context for debugging
- Implement retry logic for network errors (when
statusisundefined) - Verify your API token if you encounter status
401
Migration from Firecrawl
If you're migrating from the Firecrawl Node SDK, use this mapping to find equivalent MrScraper methods.
| Firecrawl Method | MrScraper Equivalent |
|---|---|
| Scrape a URL with HTML output | fetchHtml |
| Scrape a URL with structured JSON | createAiScraper with agent: "general" or rerunAiScraper |
| Crawl a website or map URLs | createAiScraper with agent: "map" or rerunAiScraper with map options |
| Batch scrape | bulkRerunAiScraper or bulkRerunManualScraper |
| Scrape listing/pagination pages | createAiScraper with agent: "listing" and maxPages parameter |
Key Differences
-
Agent-Based Approach: MrScraper uses specialized agents (
general,listing,map) instead of format-based parameters. -
Natural Language Instructions: Instead of defining extraction schemas, use the
messageparameter to describe what data you want in plain English. -
Pagination Handling: Use
agent: "listing"withmaxPagesto automatically handle paginated content.