Installation

To install the Firecrawl Rust SDK, add the following to your Cargo.toml:

Rust
# Add this to your Cargo.toml
[dependencies]
firecrawl = "^1.0"
tokio = { version = "^1", features = ["full"] }
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
uuid = { version = "^1.10", features = ["v4"] }

[build-dependencies]
tokio = { version = "1", features = ["full"] }

Usage

  1. Get an API key from firecrawl.dev
  2. Set the API key as a parameter to the FirecrawlApp struct.
  3. Set the API URL and/or pass it as a parameter to the FirecrawlApp struct. Defaults to https://api.firecrawl.dev.
  4. Set the version and/or pass it as a parameter to the FirecrawlApp struct. Defaults to v1.

Here’s an example of how to use the SDK in Rust:

Rust
use firecrawl::FirecrawlApp;

#[tokio::main]
async fn main() {
    // Initialize the FirecrawlApp with the API key
    let apiKey = "fc-YOUR_API_KEY";
    let apiURL = "https://api.firecrawl.dev";
    let version = "v1";
    let app = FirecrawlApp::new(apiKey, apiURL, version).expect("Failed to initialize FirecrawlApp");

    // Scrape a website
    let scrapeResponse = app.scrape_url("https://firecrawl.dev", Some(json!({
        "formats": ["markdown", "html"]
    })));
    println!("Scrape Result:\n{}", scrapeResponse);

    // Crawl a website
    let crawlParams = json!({
        "limit": 100,
        "scrapeOptions": {
            "formats": ["markdown", "html"]
        }
    });
    let crawlResult = app
        .CrawlURL(
            "https://firecrawl.dev",
            Some(crawlParams),
            true,
            2,
            None,
        )
        .await;

    match crawlResult {
        Ok(data) => println!("Crawl Result:\n{}", data),
        Err(e) => eprintln!("Crawl failed: {}", e),
    }
}

Scraping a URL

To scrape a single URL with error handling, use the scrape_url method. It takes the URL as a parameter and returns the scraped data as a serde_json::Value.

Rust
let scrapeParams = json!({
    "formats": ["markdown", "html"]
});

let scrapeResult = app.ScrapeURL("https://firecrawl.dev", Some(scrapeParams)).await;
match scrapeResult {
    Ok(data) => println!("Scrape Result:\n{}", data),
    Err(e) => eprintln!("Map failed: {}", e),
}

Crawling a Website

To crawl a website, use the crawl_url 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.

Rust
let crawlParams = json!({
    "limit": 100,
    "scrapeOptions": {
        "formats": ["markdown", "html"]
    }
});
let crawlResult = app
    .CrawlURL(
        "https://firecrawl.dev",
        Some(crawlParams),
        true,
        2,
        None,
    )
    .await;

match crawlResult {
    Ok(data) => println!("Crawl Result:\n{}", data),
    Err(e) => eprintln!("Crawl failed: {}", e),
}

Checking Crawl Status

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

Rust
  let crawlStatus = app.CheckCrawlStatus("<crawl_id>").await;

  match crawlStatus {
      Ok(data) => println!("Crawl Status:\n{}", data),
      Err(e) => eprintln!("Check crawl status failed: {}", e),
  }

Mapping a Website

To map a website, use the map_url method. It takes the starting URL as a parameter and returns the mapped data as a serde_json::Value.

Rust
let mapResult = app.MapURL("https://firecrawl.dev", None).await;
match mapResult {
    Ok(data) => println!("Map Result:\n{}", data["links"]),
    Err(e) => eprintln!("Map failed: {}", e),
}

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.