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
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/sdk

Authentication

Set your API token as an environment variable. You can get your API token from the MrScraper dashboard.

export MRSCRAPER_API_TOKEN=your_token_here

Token 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:

ParameterTypeRequiredDefaultDescription
urlstringYes-URL to fetch
timeoutnumberNo120Timeout in seconds (1-600)
geoCodestringNo"US"Two-letter ISO country code for proxy
blockResourcesbooleanNofalseBlock images, fonts, and CSS for faster fetching
tokenstringNo-Per-request token override

Create AI Scraper

Create and run a new AI-powered scraper using natural language instructions.

Agent Types:

AgentBest ForAdditional Parameters
"general"Single pages, product details, articlesNone
"listing"Product listings, search results, directoriesmax_pages
"map"Site crawling, URL discovery, sitemapsmax_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:

ParameterTypeRequiredDefaultDescription
urlstringYes-Target URL to scrape
messagestringNo""Natural language instructions for extraction
agent"general" | "listing" | "map"No"general"Agent type to use
proxyCountrystring | nullNo-Country code for proxy (e.g., "US", "UK")
maxDepthnumberNo2For map agent: link depth to follow
maxPagesnumberNo50For map/listing agent: maximum pages to scrape
limitnumberNo1000For map agent: maximum results to return
includePatternsstringNo""For map agent: URL patterns to include (regex)
excludePatternsstringNo""For map agent: URL patterns to exclude (regex)
tokenstringNo-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:

ParameterTypeRequiredDefaultDescription
scraperIdstringYes-ID of the existing scraper
urlstringYes-New URL to scrape
maxDepthnumberNo2For map agents: link depth to follow
maxPagesnumberNo50For map/listing agents: maximum pages to scrape
limitnumberNo1000For map agents: maximum results to return
includePatternsstringNo""For map agents: URL patterns to include
excludePatternsstringNo""For map agents: URL patterns to exclude
tokenstringNo-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:

ParameterTypeRequiredDescription
scraperIdstringYesID of the existing AI scraper
urlsstring[]YesArray of URLs to scrape
tokenstringNoPer-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:

ParameterTypeRequiredDescription
scraperIdstringYesID of the manual scraper from the dashboard
urlstringYesURL to scrape
tokenstringNoPer-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:

ParameterTypeRequiredDescription
scraperIdstringYesID of the manual scraper from the dashboard
urlsstring[]YesArray of URLs to scrape
tokenstringNoPer-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:

ParameterTypeRequiredDefaultDescription
sortFieldstringNo"updatedAt"Field to sort by. Supported values: "createdAt", "updatedAt", "id", "type", "url", "status", "error", "tokenUsage", "runtime"
sortOrder"ASC" | "DESC"No"DESC"Sort direction
pageSizenumberNo10Number of results per page
pagenumberNo1Page number to retrieve
searchstringNo""Keyword to filter results
dateRangeColumnstringNo-Date field to filter by
startAtstringNo-Start date for filtering (ISO 8601 format)
endAtstringNo-End date for filtering (ISO 8601 format)
tokenstringNo-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:

ParameterTypeRequiredDescription
resultIdstringYesUnique identifier of the result
tokenstringNoPer-request token override

Error Handling

All SDK methods throw MrScraperError for API, network, or timeout failures.

Exception Properties

PropertyTypeDescription
messagestringHuman-readable error message
statusnumber | undefinedHTTP status code (401, 429, 500, etc.) or undefined for network errors
namestringAlways "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 MrScraperError before accessing error properties
  • Log errors with appropriate context for debugging
  • Implement retry logic for network errors (when status is undefined)
  • 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 MethodMrScraper Equivalent
Scrape a URL with HTML outputfetchHtml
Scrape a URL with structured JSONcreateAiScraper with agent: "general" or rerunAiScraper
Crawl a website or map URLscreateAiScraper with agent: "map" or rerunAiScraper with map options
Batch scrapebulkRerunAiScraper or bulkRerunManualScraper
Scrape listing/pagination pagescreateAiScraper 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 message parameter to describe what data you want in plain English.

  • Pagination Handling: Use agent: "listing" with maxPages to automatically handle paginated content.

On this page