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.

Step by Step Introduction

For a hands-on introduction and example projects see our Scrapfly SDK introduction page!

Discover Now

Installation

Source code of Typescript SDK is available on Github and the scrapfly-sdk package is available in all Javascript and Typescript runtimes:

Deno is a modern and secure runtime for JavaScript and TypeScript that uses V8 and is built in Rust. It's incredibly easy to use and runs Typescript natively as well as being backwards compatible with NodeJS. This makes Deno a great option for web-scraping related development.

To setup Scrapfly SDK with Deno, first install the SDK through jsr.io package index:

Try out the following code snippet for Web Scraping API to get started:

Bun is a modern runtime for JavaScript and TypeScript that is fully interchangeable with NodeJS. It's incredibly easy to use and runs Typescript natively which makes it a great option for web-scraping related development.

To setup Scrapfly SDK with Bun, first install the SDK through jsr.io package index:

Try out the following code snippet for Web Scraping API to get started:

NodeJS is the classic Javascript server runtime and is supported by the SDK through both CommonJS and ESM modules.

To setup Scrapfly SDK with Node, first install the SDK through NPM package index:

Try out the following code snippet for Web Scraping API to get started:

Serverless platforms like Cloudflare Workers, AWS Lambda etc. are also supported by Scrapfly SDK.

Most serverless platforms can run full NodeJS, Python or other runtimes though there are a few exceptions and differences in runtime implementations.

For the best experience see our recommended use through Denoflare 👇

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