Typescript SDK

Typescript SDK is the easiest way to access Scrapfly API in Typescript, Javascript and NodeJS.

It provides a client that streamlines the scraping process by:

  • Handling common errors
  • Automatically encoding and decoding sensitive API parameters
  • Handling and simplifying concurrency
  • Implementing CSS selector engine for result HTML
For more on Typescript SDK use with Scrapfly, select "Typescript SDK" option in Scrapfly docs top bar.

Installation

Source code of Typescript SDK is available on Github scrapfly-sdk package is available through NPM.

The SDK is also compatible with other typescript runtimes like Bun.

Quick Use

Here's a quick preview of what Typescript SDK can do:

In short, we first create a ScrapflyClient object with our scrapfly key. Then, we can use the .scrape() method to issue our scraping commands which are defined by ScrapeConfig object.

The returned ScrapeResult object contains result data (like page HTML), request metadata and convenience extensions like CSS selector engine .selector() which can further parse the HTML result to specific details.

Configuring Scrape

The SDK supports all features of Scrapfly API, which can be configured through ScrapeConfig object:

For scraping websites protected against web scraping make sure to enable Anti Scraping Protection bypass using asp: true option.

For more on available options see API specification which is matched in the SDK where applicable.

Handling Result

The ScrapeResult object contains all data returned by Scrapfly API such as response data, api use information, scrape metadata and more:

Concurrent Scraping

The main scraping method .scrape() is asynchronous meaning it can be used in javascript idioms like Promise.all() and .then() callbacks. Additionally, the SDK provides .concurrentScrape() async generator that can be used to concurrently scrape at your scrapfly plan's concurrency limit:

Getting Account Details

To access Scrapfly account information the `.account()` method can be used:

Examples

Custom Headers

To provide additional headers, use headers option of ScrapeConfig. Note that when using asp=True Scrapfly can add additional headers automatically to prevent scraper blocking.

Post Form

To post FormData, use data option:

Post JSON

To post JSON data, use data option with a 'Content-Type':'application/json' header in ScrapeConfig:

Javascript Rendering

To render pages using headless browsers using Javascript Rendering feature use render_js=true option of ScrapeConfig:

Javascript Scenario

To execute Javascript Scenario use scenario option of ScrapeConfig:

Capturing Screenshots

To capture screenshots render_js=true and screenshots options of ScrapeConfig can be used:

Scraping Binary Data

Binary data can be scraped like any other page however it's returned b64 encoded. To decode it, the Buffer.from() method can be used:

Summary