npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mendable/firecrawl-js

v0.0.21

Published

JavaScript SDK for Firecrawl API

Downloads

8,249

Readme

Firecrawl JavaScript SDK

The Firecrawl JavaScript SDK is a library that allows you to easily scrape and crawl websites, and output the data in a format ready for use with language models (LLMs). It provides a simple and intuitive interface for interacting with the Firecrawl API.

Installation

To install the Firecrawl JavaScript SDK, you can use npm:

npm install @mendable/firecrawl-js

Usage

  1. Get an API key from firecrawl.dev
  2. Set the API key as an environment variable named FIRECRAWL_API_KEY or pass it as a parameter to the FirecrawlApp class.

Here's an example of how to use the SDK with error handling:

  import FirecrawlApp from '@mendable/firecrawl-js';

  async function main() {
    try {
      // Initialize the FirecrawlApp with your API key
      const app = new FirecrawlApp({ apiKey: "YOUR_API_KEY" });

      // Scrape a single URL
      const url = 'https://mendable.ai';
      const scrapedData = await app.scrapeUrl(url);
      console.log(scrapedData);
      
      // Crawl a website
      const crawlUrl = 'https://mendable.ai';
      const params = {
      crawlerOptions: {
        excludes: ['blog/'],
        includes: [], // leave empty for all pages 
        limit: 1000,
        },
        pageOptions: {
            onlyMainContent: true
        }
      };

      const crawlResult = await app.crawlUrl(crawlUrl, params);
      console.log(crawlResult);

    } catch (error) {
      console.error('An error occurred:', error.message);
    }
  }

  main();

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.

  async function scrapeExample() {
    try {
      const url = 'https://example.com';
      const scrapedData = await app.scrapeUrl(url);
      console.log(scrapedData);

    } catch (error) {
      console.error(
        'Error occurred while scraping:',
        error.message
      );
    }
  }
  
  scrapeExample();

Extracting structured data from a URL

With LLM extraction, you can easily extract structured data from any URL. We support zod schemas to make it easier for you too. Here is how you to use it:

import { z } from "zod";

const zodSchema = z.object({
  top: z
    .array(
      z.object({
        title: z.string(),
        points: z.number(),
        by: z.string(),
        commentsURL: z.string(),
      })
    )
    .length(5)
    .describe("Top 5 stories on Hacker News"),
});

let llmExtractionResult = await app.scrapeUrl("https://news.ycombinator.com", {
  extractorOptions: { extractionSchema: zodSchema },
});

console.log(llmExtractionResult.data.llm_extraction);

Search for a query

Used to search the web, get the most relevant results, scrap each page and return the markdown.

query = 'what is mendable?'
searchResult = app.search(query)

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.

async function crawlExample() {
  try {
    const crawlUrl = 'https://example.com';
    const params = {
      crawlerOptions: {
        excludes: ['blog/'],
        includes: [], // leave empty for all pages
        limit: 1000,
      },
      pageOptions: {
        onlyMainContent: true
      }
    };
    const waitUntilDone = true;
    const timeout = 5;
    const crawlResult = await app.crawlUrl(
      crawlUrl,
      params,
      waitUntilDone,
      timeout
    );

    console.log(crawlResult);

  } catch (error) {
    console.error(
      'Error occurred while crawling:',
      error.message
    );
  }
}

crawlExample();

Checking Crawl Status

To check the status of a crawl job with error handling, use the checkCrawlStatus method. It takes the job ID as a parameter and returns the current status of the crawl job.

async function checkStatusExample(jobId) {
  try {
    const status = await app.checkCrawlStatus(jobId);
    console.log(status);

  } catch (error) {
    console.error(
      'Error occurred while checking crawl status:',
      error.message
    );
  }
}
// Example usage, assuming you have a jobId
checkStatusExample('your_job_id_here');

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.

Contributing

Contributions to the Firecrawl JavaScript SDK are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License

The Firecrawl JavaScript SDK is open-source and released under the MIT License.