Node
Firecrawl Node SDK is a wrapper around the Firecrawl API to help you easily turn websites into markdown.
Installation
To install the Firecrawl Node SDK, you can use npm:
Usage
- Get an API key from firecrawl.dev
- Set the API key as an environment variable named
FIRECRAWL_API_KEY
or pass it as a parameter to theFirecrawlApp
class.
Here’s an example of how to use the SDK with error handling:
Scraping a URL
To scrape a single URL with error handling, use the scrapeUrl
method. It takes the URL as a parameter and returns the scraped data as a dictionary.
Crawling a Website
To crawl a website with error handling, use the crawlUrl
method. It takes the starting URL and optional parameters as arguments. The params
argument allows you to specify additional options for the crawl job, such as the maximum number of pages to crawl, allowed domains, and the output format.
Asynchronous Crawling
To crawl a website asynchronously, use the crawlUrlAsync
method. It returns the crawl ID
which you can use to check the status of the crawl job. It takes the starting URL and optional parameters as arguments. The params
argument allows you to specify additional options for the crawl job, such as the maximum number of pages to crawl, allowed domains, and the output format.
Checking Crawl Status
To check the status of a crawl job with error handling, use the checkCrawlStatus
method. It takes the ID
as a parameter and returns the current status of the crawl job.
Cancelling a Crawl
To cancel an asynchronous crawl job, use the cancelCrawl
method. It takes the job ID of the asynchronous crawl as a parameter and returns the cancellation status.
Mapping a Website
To map a website with error handling, use the mapUrl
method. It takes the starting URL as a parameter and returns the mapped data as a dictionary.
Crawling a Website with WebSockets
To crawl a website with WebSockets, use the crawlUrlAndWatch
method. It takes the starting URL and optional parameters as arguments. The params
argument allows you to specify additional options for the crawl job, such as the maximum number of pages to crawl, allowed domains, and the output format.
Error Handling
The SDK handles errors returned by the Firecrawl API and raises appropriate exceptions. If an error occurs during a request, an exception will be raised with a descriptive error message. The examples above demonstrate how to handle these errors using try/catch
blocks.