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
{
"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/sdkAuthentication
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:
| Property | Type | Description |
|---|---|---|
status_code | number | HTTP status code of the API response |
data | T (default unknown) | Parsed JSON for JSON responses, a string for HTML and other text, or a Node.js Buffer for binary responses |
headers | Record<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.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
url | string | Yes | - | Target page URL (first argument) |
token | string | No | - | Per-request token override |
html | boolean | No | true | Return HTML content |
markdown | boolean | No | false | Return Markdown content |
timeout | number | No | 120 | Page timeout in seconds (1-300) |
geo_code | string | No | "US" | Two-letter country code for geolocation |
proxy_country | string | null | No | - | Optional proxy country |
browser_rendering | boolean | null | No | - | true forces browser rendering, false disables it, unset tries without a browser first and falls back automatically |
screenshot | boolean | No | false | Capture a screenshot. Also forces browser rendering |
super | boolean | No | false | Enable real device routing |
use_proxy | boolean | No | true | Allow the service to use a proxy |
proxy | string | null | No | - | Custom proxy in username:password@host:port format |
home_page | boolean | No | false | Visit the site's root page before the target URL |
wait_for_selector | string | null | No | - | CSS selector to wait for before returning content |
wait_until | string | null | No | - | Page lifecycle event to wait for, such as "domcontentloaded" |
block_resources | boolean | null | No | false | Block images, CSS, and fonts to speed up the request |
bypass_proxy | boolean | No | false | Load static assets such as images, fonts and stylesheets outside the residential proxy to save bandwidth |
cookie_jar | string | null | No | - | Cookie configuration, e.g. "domain=example.com&country=us&max_used=100&max_age=50m" |
headers | object | string | null | No | - | Headers forwarded to the target site, as an object or JSON string |
max_retries | number | No | 3 | Maximum number of retries, a nonnegative integer |
token_cap | number | null | No | - | Optional token budget for retries |
stream | boolean | null | No | - | Request a streamed response. The SDK buffers it into the normal return value |
discard_token_secret | string | No | "" | 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.
Web Search
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:
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
query | string | Yes | - | Search query (first argument). Must not be empty |
token | string | No | - | Per-request token override |
country | string | null | No | - | Country code for localized results, such as "us". Omitted to use the service default |
num_results | number | No | 10 | Number of results, an integer from 10 to 100. Sent as numResults |
timeout | number | No | 600 | Request timeout in seconds |
Response (result.data, typed as WebSearchResponse):
| Property | Type | Description |
|---|---|---|
success | boolean | Whether the search succeeded |
message | string | Status message |
data | WebSearchResult[] | 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:
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
token | string | No | - | Per-request token override |
timeout | number | No | 30 | HTTP request timeout in seconds. Must be positive |
Account summary (result.data, typed as AccountSummary):
| Property | Type | Description |
|---|---|---|
subscription_status | string | null | Subscription status, such as "active" |
enterprise | boolean | Whether the account is on an enterprise plan |
token_usage | number | Tokens used in the current period |
token_limit | number | Token limit for the current period |
token_remaining | number | Remaining tokens, never below 0 |
usage_percent | number | Token usage as a percentage of the limit |
rate_limit | number | Request rate limit |
rate_ttl | number | Rate-limit window |
auto_renew | boolean | Whether the subscription renews automatically |
ends_at | string | null | Subscription end date |
user | object | name, 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
| 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 { 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 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
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 4 | Version 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 type | ScraperApiOptions |
A screenshot from scraper_api is a base64-encoded JPEG in data.screenshot, not a Buffer.
From Version 3
web_searchnow calls the MrScraper API withaction: "websearch"and returns JSON. Its first argument is the search query, and it acceptscountryandnum_results(10 to 100). Theraw,format,region,language,page, andrender_jsoptions and search URL input were removed. Results are atresult.data.data.scraper_apisends aGETrequest with every option in the query string, and adds thewait_untiloption. At least one ofhtml,markdown, orscreenshotmust betrue. HTML-only requests return a string, and requests with Markdown or a screenshot return an object with one entry per output.accountis 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 2 | Version 3 |
|---|---|
fetch_content(url, options) | scraper_api(url, options) |
FetchContentOptions | ScraperApiOptions |
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
fetchHtmlwithscraper_api. It now returns a response wrapper instead of only the API payload, and options usesnake_case(for example,geoCodeis nowgeo_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 Method | MrScraper Equivalent |
|---|---|
| Scrape a URL with HTML or Markdown output | scraper_api with html: true and/or markdown: true |
| Scrape a URL with a screenshot | scraper_api with screenshot: true |
| Search | web_search |
Python SDK
Install and use mrscraper-sdk, the typed async Python client for the MrScraper API, to fetch page content with the Scraper API and get real-time web search results with asyncio.
LangChain SDK
Use MrScraper as LangChain tools via the langchain-mrscraper package to fetch web pages, search the web, and check account usage from agents.