For web scraping with JavaScript, a 41 line Node.js script can scrape one page twice, once as markdown and once for a named field, then stream a crawl into a file with one line per page. It needs one package and starts no browser on your machine.
The measured run went through the Spider API on November 17, 2026. Its target was scrapethissite.com, a public site built for scraping practice. The crawl of 25 pages streamed into the file in 5.4 seconds, and the two scrapes took 1.5 and 1.4 seconds. The crawl cost $0.0019 on the pay as you go meter. Cost varies by site.
The script parses no HTML. The API returns each page as markdown in its content field, and for the crawl the script writes that field to pages.jsonl, with the page’s URL and status, as each page arrives. When request asks for a browser, the browser runs on the API’s side. The web scraping guide shows which kinds of page need one.
What the script returned
| Call | What came back | Time | Cost |
|---|---|---|---|
crawlUrl, streamed, limit: 25 | 25 lines in pages.jsonl | 5.4 s | $0.0019 |
scrapeUrl, markdown | The page as markdown, status 200 | 1.5 s | $0.000319 |
scrapeUrl with css_extraction_map | The title under css_extracted | 1.4 s | $0.000319 |
We started it at 10:34:51 UTC on Node.js 24.2.0 with client 0.2.5. The script sends the two scrapes first and the crawl last. Time is wall time at the client. Cost is costs.total_cost on the pay as you go meter, summed over the 25 pages for the crawl. The Unlimited plan is priced by requests at once and not by page, from $40 a month for 2 in http mode.
Install the client and run the script
The script is 41 lines and needs one package.
npm install @spider-cloud/spider-client@0.2.5
export SPIDER_API_KEY=your_keySave this as scrape.mjs and run node scrape.mjs.
import { Spider } from "@spider-cloud/spider-client";
import { appendFileSync } from "node:fs";
const spider = new Spider();
const [page] = await spider.scrapeUrl("https://www.scrapethissite.com/pages/simple/", {
return_format: "markdown",
request: "http",
});
console.log(page.status, page.content.slice(0, 200));
const fields = await spider.scrapeUrl("https://www.scrapethissite.com/pages/simple/", {
return_format: "markdown",
css_extraction_map: {
"/": [{ name: "title", selectors: ["h1"] }],
},
});
console.log(fields[0].css_extracted);
let saved = 0;
await spider.crawlUrl(
"https://www.scrapethissite.com/pages/",
{ limit: 25, return_format: "markdown" },
true,
(page) => {
try {
appendFileSync("pages.jsonl", JSON.stringify({ url: page.url, status: page.status, content: page.content }) + "\n");
saved += 1;
} catch (err) {
console.error("could not save", page.url, err);
}
},
);
if (saved === 0) {
throw new Error("The crawl streamed no pages. Check the key and your credits.");
}
console.log(`saved ${saved} pages`);The client calls the fetch built into Node, which is on without a flag from Node.js 18. We ran the script on Node.js 24.2.0 with version 0.2.5 of the client, the version on npm on November 17, 2026. The .mjs extension makes the file an ES module, where top level await works.
The script appends to pages.jsonl. Delete the file before a second run, or the new lines land after the old ones.
The copy we ran against the API had a few more lines. It printed the time of each call and the cost of each scrape, and it saved page.costs.total_cost with each line of the crawl. The times and costs above come from those lines.
Reading what scrapeUrl returns
In version 0.2.5 scrapeUrl(url, params) resolves with an array, one object per page, which is why the script destructures [page]. Each object carries url, status, content and a costs object whose total_cost field is what the page cost.
Check page.status before you trust content. The practice site answers its CSRF exercise, /pages/advanced/?gotcha=csrf, with status 400 and the text “Needs login”, and the crawl passed that status through with the page. The script saves the status with each line for that reason. As the API reference puts it, an attempt bills for the bandwidth and compute it used, whatever status the page returns.
The API reference describes the request values. "http" runs no JavaScript and is the fastest. "browser" renders the page first. The second scrape and the crawl leave the field out, so they get the default, "smart". For a page that builds itself with a script, set request: "browser", as step two of the how-to shows.
The script asks for markdown because it is the smaller answer. On ten public pages measured on November 5, 2026, markdown used 15 times fewer tokens than raw HTML at the median, counted in the token cost post.
Extracting fields with CSS selectors
css_extraction_map is keyed by URL path. Each value is a list of { name, selectors } pairs. The API reference says the key "/" matches every page, any other key matches only the page whose path is exactly that key, and a prefix such as "/products" currently matches nothing. Start with "/" unless you mean one exact page.
The values come back as arrays, because a selector can match more than once. Against the practice site the script printed { title: [ 'Countries of the World: A Simple Example 250 items' ] }. Take [0] when you want one string.
Streaming a crawl in Node.js
Pass true as the third argument and a function as the fourth. The client sends Content-Type: application/jsonl, reads the answer as it arrives, splits it on newlines and calls your function with each parsed page. In the run against the API, the function ran 25 times for limit: 25 and a fresh pages.jsonl held 25 lines.
Each page is written when it lands, so a crawl that stops partway leaves the pages that already arrived in the file. The await resolves with no value, so the pages exist only where your function put them, and the function is the place to save, count or queue them.
Without the third and fourth arguments, crawlUrl waits for the whole crawl and resolves with the full array. That suits 25 pages. For a crawl of thousands it means holding all of them in memory before the first one is written.
What the client does on a 429
The client wraps every POST in a backoff with five attempts in all. When the status is 429 it reads Retry-After, uses 1 second if the header is missing, and waits that long before the next attempt. It also records the RateLimit-Limit, RateLimit-Remaining and RateLimit-Reset headers on each answer.
On November 16, 2026 we ran the script against a local server that answers the way the API documents, with https://example.com as every URL, so we could send a 429 on purpose. The server answered the first request with a 429 and Retry-After: 1. The script ran to the end and saved 25 of 25 pages, with no retry code in it. Five attempts is the whole budget, so a run that hits the limit again and again will still fail.
Counting the pages a streamed crawl saved
In stream mode crawlUrl resolves with no value, so the count of saved pages is how the script knows the crawl ran. When saved is still zero after the call, the script throws and names the two things to check, the key and the credits. In our November 16 local test with a 402, that throw is how the failure showed up.
The client does not pass an error thrown inside your function back to the caller. In the local test with a function that throws on every page, no error reached the caller, so the try and catch in the function are where a failed write gets logged. Keep both habits in your own code. Catch inside the function, and count after the call.
Run the script on your own pages
The API run was one morning on one practice site, and the 429, 402 and throw tests ran on a local server that answers as the API documents. To run the script against the API, create an API key and export it as SPIDER_API_KEY. For one page with no key and no Node install, use the web scraper.
Frequently asked questions
Can I do web scraping with JavaScript without running a browser in Node?
Yes. The script on this page needs one package, @spider-cloud/spider-client 0.2.5, and starts no browser on your machine. The request field tells the API to use a plain HTTP fetch or a browser on its side. On November 17, 2026 we ran the script on Node.js 24.2.0 against the API, and it streamed 25 pages from a public practice site into a file in 5.4 seconds.
How do I stream a crawl in Node.js instead of waiting for the whole result?
Pass true and a function as the third and fourth arguments of crawlUrl. The client then asks for JSON lines and calls your function once for each page as its line arrives. In that mode crawlUrl resolves with no value, so save or count pages inside the function. In our run against the API on November 17, 2026 the function ran 25 times for a limit of 25.
What does the Node client do when it gets a 429?
Version 0.2.5 makes up to five attempts. On a 429 it waits the number of seconds in the Retry-After header, or 1 second when the header is missing, before the next attempt. In our test of November 16, 2026 a local test server answered the first request with a 429 and Retry-After: 1, and the script still saved 25 of 25 pages with no retry code of its own.
How do I count the pages a streamed crawl saved?
Count them inside your function. In stream mode crawlUrl resolves with no value, so the script on this page adds one to a counter for each page it writes, prints the total and throws if the count is still zero after the call. In our run against the API on November 17, 2026 it saved 25 pages for a limit of 25.
Can I use the Node client without an API key?
No. In version 0.2.5, new Spider() reads SPIDER_API_KEY from the environment and throws No API key provided when it is missing. Export the key before you run the script, as the install step on this page does.
Sources
- Measured run, 2026-11-17, 10:34:51 to 10:34:59 UTC: the script below on Node.js 24.2.0 with @spider-cloud/spider-client 0.2.5, sent once through the Spider API to www.scrapethissite.com. The copy we ran also printed the time of each call and the cost of each scrape, and saved each page's cost
- Local tests, 2026-11-16: the same script with example.com as every URL, on Node.js 24.2.0 with @spider-cloud/spider-client 0.2.5, against a local server that answers as the API documents. Four tests: success, one 429, a 402, and a function that throws on every page
- Spider JavaScript client source at commit 34a69f8: scrapeUrl, crawlUrl, the key check and the retry on 429
- Spider JavaScript client source at commit 34a69f8: the stream reader and the JSON lines reader that calls your function once per page
- Spider OpenAPI file, read 2026-11-16: the request values, css_extraction_map key matching, the css_extracted field and the note on billing for failed pages
- Spider pricing page, the Unlimited plan's price by requests at once
- Node.js documentation, the global fetch: no longer behind a flag from v18.0.0
- Node.js documentation, ECMAScript modules: the .mjs extension and top level await
- What a web page costs in tokens, measured on ten public pages, 2026-11-05