SDKs

Node.js SDK

Install the official @mrscraper/sdk package to fetch page content with the Scraper API and get real-time web search results from your Node.js applications.

The official @mrscraper/sdk package exposes three functions: scraper_api to fetch page content (HTML, Markdown, or screenshots), web_search to retrieve real-time web search results, and account to check your subscription and token usage from your Node.js applications.

Requirements

  • Node.js 18 or later
  • ES Modules enabled in your package.json
package.json
{
  "type": "module"
}

Package Information

This page documents version 5.0.0. 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. The SDK reads MRSCRAPER_API_TOKEN automatically. You can get your API token from the MrScraper dashboard.

export MRSCRAPER_API_TOKEN="MRSCRAPER_API_TOKEN"

Token Override

All functions accept an optional token in their options object 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 { scraper_api, web_search, account, MrScraperError } from "@mrscraper/sdk";

try {
  // 1. Fetch page content
  const page = await scraper_api("https://example.com");
  console.log(page.status_code, page.data);

  // 2. Search the web
  const search = await web_search("python web scraping", { country: "us" });
  console.log(search.data.data);

  // 3. Check remaining tokens
  const status = await account();
  console.log(status.data.token_remaining);
} catch (err) {
  if (err instanceof MrScraperError) {
    console.error(`[${err.status ?? "network"}] ${err.message}`);
  } else {
    throw err;
  }
}

Response Format

All functions are async and resolve to a MrScraperResponse object:

PropertyTypeDescription
status_codenumberHTTP status code of the API response
dataT (default unknown)Parsed JSON for JSON responses, a string for HTML and other text, or a Node.js Buffer for binary responses
headersRecord<string, string>Response headers

You can type data with a generic parameter:

const result = await scraper_api<string>("https://example.com");

Core Methods

Scraper API

Use scraper_api to fetch a page through the MrScraper Scraper API. The service handles JavaScript rendering, bot detection, and optional geolocation proxying.

import { writeFile } from "node:fs/promises";
import { scraper_api } from "@mrscraper/sdk";

// HTML only (the default) returns the page as a string
const page = await scraper_api("https://example.com");
console.log(page.data);

// Markdown or a screenshot returns an object with one entry per output
const result = await scraper_api("https://example.com", {
  html: true,
  markdown: true,
  screenshot: true,
  geo_code: "US",
  timeout: 120,
  // token: "optional_override_token"
});
if (typeof result.data !== "string") {
  console.log(result.data.markdown);
  if (result.data.screenshot) {
    await writeFile("page.jpg", Buffer.from(result.data.screenshot, "base64"));
  }
}

Requesting only HTML returns the page as a string. Requesting Markdown or a screenshot returns an object such as { html, markdown, screenshot } with an entry for each requested output, typed as ScraperApiData. The screenshot is a base64-encoded JPEG. At least one of html, markdown, or screenshot must be true.

The SDK sends a GET request to https://api.mrscraper.com, with the target URL and every option in the query string.

Parameters:

The first argument is the target URL. All other parameters go in the optional options object.

ParameterTypeRequiredDefaultDescription
urlstringYes-Target page URL (first argument)
tokenstringNo-Per-request token override
htmlbooleanNotrueReturn HTML content
markdownbooleanNofalseReturn Markdown content
timeoutnumberNo120Page timeout in seconds (1-300)
geo_codestringNo"US"Two-letter country code for geolocation
proxy_countrystring | nullNo-Optional proxy country
browser_renderingboolean | nullNo-true forces browser rendering, false disables it, unset tries without a browser first and falls back automatically
screenshotbooleanNofalseCapture a screenshot. Also forces browser rendering
superbooleanNofalseEnable real device routing
use_proxybooleanNotrueAllow the service to use a proxy
proxystring | nullNo-Custom proxy in username:password@host:port format
home_pagebooleanNofalseVisit the site's root page before the target URL
wait_for_selectorstring | nullNo-CSS selector to wait for before returning content
wait_untilstring | nullNo-Page lifecycle event to wait for, such as "domcontentloaded"
block_resourcesboolean | nullNofalseBlock images, CSS, and fonts to speed up the request
bypass_proxybooleanNofalseLoad static assets such as images, fonts and stylesheets outside the residential proxy to save bandwidth
cookie_jarstring | nullNo-Cookie configuration, e.g. "domain=example.com&country=us&max_used=100&max_age=50m"
headersobject | string | nullNo-Headers forwarded to the target site, as an object or JSON string
max_retriesnumberNo3Maximum number of retries, a nonnegative integer
token_capnumber | nullNo-Optional token budget for retries
streamboolean | nullNo-Request a streamed response. The SDK buffers it into the normal return value
discard_token_secretstringNo""Value sent in the x-discard-token-secret request header

Tip

Setting screenshot: true always enables browser rendering, even if you pass browser_rendering: false.

For more examples of what you can fetch, see Scrape HTML, Scrape Markdown, and Get Screenshot.

Use web_search to search the web and retrieve real-time search results, including relevant website URLs, titles, and descriptions. The first argument is the search query.

import { web_search } from "@mrscraper/sdk";

const result = await web_search("python web scraping", {
  country: "us",
  num_results: 10,
  // token: "optional_override_token"
});
for (const item of result.data.data) {
  console.log(item.rank, item.title, item.url);
}

The SDK sends this request:

curl --location 'https://api.mrscraper.com' \
--header 'x-api-token: {MRSCRAPER_API_TOKEN}' \
--header 'Content-Type: application/json' \
--data '{
    "action": "websearch",
    "query": "python web scraping",
    "country": "us",
    "numResults": 10
}'

Parameters:

ParameterTypeRequiredDefaultDescription
querystringYes-Search query (first argument). Must not be empty
tokenstringNo-Per-request token override
countrystring | nullNo-Country code for localized results, such as "us". Omitted to use the service default
num_resultsnumberNo10Number of results, an integer from 10 to 100. Sent as numResults
timeoutnumberNo600Request timeout in seconds

Response (result.data, typed as WebSearchResponse):

PropertyTypeDescription
successbooleanWhether the search succeeded
messagestringStatus message
dataWebSearchResult[]Ranked results. Each has rank, title, url, description, and source

Account

Use account to check the identity, subscription, token quota, and rate limits of the account that owns the API token.

import { account } from "@mrscraper/sdk";

const result = await account({
  timeout: 30,
  // token: "optional_override_token"
});
console.log(result.data.subscription_status);
console.log(result.data.token_remaining);
console.log(result.data.user.email);

Parameters:

ParameterTypeRequiredDefaultDescription
tokenstringNo-Per-request token override
timeoutnumberNo30HTTP request timeout in seconds. Must be positive

Account summary (result.data, typed as AccountSummary):

PropertyTypeDescription
subscription_statusstring | nullSubscription status, such as "active"
enterprisebooleanWhether the account is on an enterprise plan
token_usagenumberTokens used in the current period
token_limitnumberToken limit for the current period
token_remainingnumberRemaining tokens, never below 0
usage_percentnumberToken usage as a percentage of the limit
rate_limitnumberRequest rate limit
rate_ttlnumberRate-limit window
auto_renewbooleanWhether the subscription renews automatically
ends_atstring | nullSubscription end date
userobjectname, email, and verified

Note

API keys, billing identifiers, and sensitive response headers are excluded from the result.

TypeScript Types

The package ships its own type definitions:

import type {
  ScraperApiOptions,
  ScraperApiData,
  WebSearchOptions,
  WebSearchResponse,
  WebSearchResult,
  AccountOptions,
  AccountSummary,
  MrScraperResponse,
} from "@mrscraper/sdk";

Error Handling

All functions reject with MrScraperError for API, authentication, network, and timeout failures. Invalid options throw a TypeError or RangeError before any request is sent.

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 { scraper_api, MrScraperError } from "@mrscraper/sdk";

try {
  await scraper_api("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

Migrating from Earlier Versions

From Version 4

Version 5.0.0 returns to three public functions. The 4.x format-specific functions (scraper_html, scraper_markdown, get_screenshot, scraper_json, scraper_multiple_page, scraper_sitemap, scrape_brand, and get_brand) and their option types are no longer exported. Use scraper_api with output flags instead:

Version 4Version 5
scraper_html(url, options)scraper_api(url, options)
scraper_markdown(url, options)scraper_api(url, { ...options, html: false, markdown: true })
get_screenshot(url, { full_page: false })scraper_api(url, { html: false, screenshot: true })
web_search(query, { region: "us" })web_search(query, { country: "us" })
ScraperOptions typeScraperApiOptions

A screenshot from scraper_api is a base64-encoded JPEG in data.screenshot, not a Buffer.

From Version 3

  • web_search now calls the MrScraper API with action: "websearch" and returns JSON. Its first argument is the search query, and it accepts country and num_results (10 to 100). The raw, format, region, language, page, and render_js options and search URL input were removed. Results are at result.data.data.
  • scraper_api sends a GET request with every option in the query string, and adds the wait_until option. At least one of html, markdown, or screenshot must be true. HTML-only requests return a string, and requests with Markdown or a screenshot return an object with one entry per output.
  • account is unchanged.

From Version 3.0 to 3.1

Version 3.1.0 adds the account function and the AccountOptions and AccountSummary types. No existing functions changed.

From Version 2

Version 3 renames the public functions and their option types. Request options and the { status_code, data, headers } response are unchanged.

Version 2Version 3
fetch_content(url, options)scraper_api(url, options)
FetchContentOptionsScraperApiOptions

The version 2 names are no longer exported.

From Version 1

fetchHtml and all AI scraper, manual scraper, and results functions (createAiScraper, rerunAiScraper, bulkRerunAiScraper, rerunManualScraper, bulkRerunManualScraper, getAllResults, getResultById) were removed in version 2.

  • Replace fetchHtml with scraper_api. It now returns a response wrapper instead of only the API payload, and options use snake_case (for example, geoCode is now geo_code).
  • For scrapers and results, call the REST API directly.

Migration from Firecrawl

If you're migrating from the Firecrawl Node SDK, use this mapping to find equivalent MrScraper functions.

Firecrawl MethodMrScraper Equivalent
Scrape a URL with HTML or Markdown outputscraper_api with html: true and/or markdown: true
Scrape a URL with a screenshotscraper_api with screenshot: true
Searchweb_search

On this page