firecrawl 2.4.1

Official Rust SDK for Firecrawl API v2.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! Crawl endpoint for Firecrawl API v2.

use serde::{Deserialize, Serialize};

use crate::client::Client;
use crate::scrape::ScrapeOptions;
use crate::types::{CrawlErrorsResponse, Document, JobStatus, SitemapMode, WebhookConfig};
use crate::FirecrawlError;

/// Options for crawling a website.
#[serde_with::skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CrawlOptions {
    /// Natural language prompt to guide crawl behavior.
    pub prompt: Option<String>,

    /// URL path patterns to exclude from crawling.
    pub exclude_paths: Option<Vec<String>>,

    /// URL path patterns to include in crawling.
    pub include_paths: Option<Vec<String>>,

    /// Maximum depth of links to follow from the initial URL.
    pub max_discovery_depth: Option<u32>,

    /// How to handle the sitemap.
    pub sitemap: Option<SitemapMode>,

    /// Ignore query parameters when deduplicating URLs.
    pub ignore_query_parameters: Option<bool>,

    /// Maximum number of pages to crawl.
    pub limit: Option<u32>,

    /// Crawl the entire domain regardless of path structure.
    pub crawl_entire_domain: Option<bool>,

    /// Allow following links to external domains.
    pub allow_external_links: Option<bool>,

    /// Allow following links to subdomains.
    pub allow_subdomains: Option<bool>,

    /// Delay between requests in seconds.
    pub delay: Option<u32>,

    /// Maximum concurrent requests.
    pub max_concurrency: Option<u32>,

    /// Webhook configuration for job notifications.
    pub webhook: Option<WebhookConfig>,

    /// Scrape options to apply to each page.
    pub scrape_options: Option<ScrapeOptions>,

    /// Enable zero data retention mode.
    pub zero_data_retention: Option<bool>,

    /// Integration identifier for tracking.
    pub integration: Option<String>,

    /// Idempotency key for the request.
    #[serde(skip)]
    pub idempotency_key: Option<String>,

    /// Poll interval for synchronous crawl (milliseconds).
    #[serde(skip)]
    pub poll_interval: Option<u64>,
}

/// Request body for crawl endpoint.
#[derive(Deserialize, Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
struct CrawlRequest {
    url: String,
    #[serde(flatten)]
    options: CrawlOptions,
}

/// Response from starting a crawl job.
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CrawlResponse {
    /// Whether the request was successful.
    pub success: bool,
    /// The crawl job ID.
    pub id: String,
    /// URL to check the crawl status.
    pub url: String,
}

/// Status of a crawl job.
#[serde_with::skip_serializing_none]
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CrawlJob {
    /// Current status of the crawl job.
    pub status: JobStatus,
    /// Total number of pages to crawl.
    pub total: u32,
    /// Number of pages completed.
    pub completed: u32,
    /// Credits used by the crawl.
    pub credits_used: Option<u32>,
    /// Expiry time of the crawl data.
    pub expires_at: Option<String>,
    /// URL for the next page of results.
    pub next: Option<String>,
    /// Crawled documents.
    pub data: Vec<Document>,
}

/// Response from canceling a crawl.
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CancelCrawlResponse {
    /// Status of the cancellation.
    pub status: String,
}

impl Client {
    /// Starts a crawl job asynchronously.
    ///
    /// Returns immediately with a job ID that can be used to check status.
    ///
    /// # Arguments
    ///
    /// * `url` - The URL to start crawling from.
    /// * `options` - Optional crawl configuration.
    ///
    /// # Returns
    ///
    /// A `CrawlResponse` containing the job ID.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use firecrawl::{Client, CrawlOptions};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new("your-api-key")?;
    ///
    ///     let response = client.start_crawl("https://example.com", None).await?;
    ///     println!("Crawl job started: {}", response.id);
    ///
    ///     // Check status later
    ///     let status = client.get_crawl_status(&response.id).await?;
    ///     println!("Status: {:?}, Completed: {}/{}", status.status, status.completed, status.total);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn start_crawl(
        &self,
        url: impl AsRef<str>,
        options: impl Into<Option<CrawlOptions>>,
    ) -> Result<CrawlResponse, FirecrawlError> {
        let options = options.into().unwrap_or_default();
        let body = CrawlRequest {
            url: url.as_ref().to_string(),
            options: options.clone(),
        };

        let headers = self.prepare_headers(options.idempotency_key.as_ref());

        let response = self
            .client
            .post(self.url("/crawl"))
            .headers(headers)
            .json(&body)
            .send()
            .await
            .map_err(|e| {
                FirecrawlError::HttpError(format!("Starting crawl of {:?}", url.as_ref()), e)
            })?;

        self.handle_response(response, "start crawl").await
    }

    /// Gets the status of a crawl job.
    ///
    /// If the job is completed, this will automatically fetch all pages of results.
    ///
    /// # Arguments
    ///
    /// * `id` - The crawl job ID.
    ///
    /// # Returns
    ///
    /// A `CrawlJob` containing the current status and any available documents.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use firecrawl::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new("your-api-key")?;
    ///
    ///     let status = client.get_crawl_status("job-id").await?;
    ///     println!("Status: {:?}", status.status);
    ///     println!("Completed: {}/{}", status.completed, status.total);
    ///     println!("Documents: {}", status.data.len());
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_crawl_status(&self, id: impl AsRef<str>) -> Result<CrawlJob, FirecrawlError> {
        let response = self
            .client
            .get(self.url(&format!("/crawl/{}", id.as_ref())))
            .headers(self.prepare_headers(None))
            .send()
            .await
            .map_err(|e| {
                FirecrawlError::HttpError(format!("Checking crawl status {}", id.as_ref()), e)
            })?;

        let mut status: CrawlJob = self
            .handle_response(response, format!("crawl status {}", id.as_ref()))
            .await?;

        // Auto-paginate if completed
        if status.status == JobStatus::Completed {
            while let Some(next) = status.next.take() {
                let next_status = self.get_crawl_status_next(&next).await?;
                status.data.extend(next_status.data);
                status.next = next_status.next;
            }
        }

        Ok(status)
    }

    /// Fetches the next page of crawl results.
    async fn get_crawl_status_next(&self, next: &str) -> Result<CrawlJob, FirecrawlError> {
        let response = self
            .client
            .get(next)
            .headers(self.prepare_headers(None))
            .send()
            .await
            .map_err(|e| FirecrawlError::HttpError(format!("Paginating crawl at {}", next), e))?;

        self.handle_response(response, "crawl pagination").await
    }

    /// Crawls a website and waits for completion.
    ///
    /// This method starts a crawl and polls until it completes or fails.
    ///
    /// # Arguments
    ///
    /// * `url` - The URL to start crawling from.
    /// * `options` - Optional crawl configuration.
    ///
    /// # Returns
    ///
    /// A `CrawlJob` containing all crawled documents.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use firecrawl::{Client, CrawlOptions, SitemapMode};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new("your-api-key")?;
    ///
    ///     let options = CrawlOptions {
    ///         sitemap: Some(SitemapMode::Include),
    ///         limit: Some(100),
    ///         poll_interval: Some(5000), // Check every 5 seconds
    ///         ..Default::default()
    ///     };
    ///
    ///     let result = client.crawl("https://example.com", options).await?;
    ///     println!("Crawled {} pages", result.data.len());
    ///
    ///     for doc in result.data {
    ///         println!("URL: {:?}", doc.metadata.and_then(|m| m.source_url));
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn crawl(
        &self,
        url: impl AsRef<str>,
        options: impl Into<Option<CrawlOptions>>,
    ) -> Result<CrawlJob, FirecrawlError> {
        let options = options.into().unwrap_or_default();
        let poll_interval = options.poll_interval.unwrap_or(2000);

        let response = self.start_crawl(url, options).await?;
        self.wait_for_crawl(&response.id, poll_interval).await
    }

    /// Waits for a crawl job to complete.
    async fn wait_for_crawl(
        &self,
        id: &str,
        poll_interval: u64,
    ) -> Result<CrawlJob, FirecrawlError> {
        loop {
            let status = self.get_crawl_status(id).await?;

            match status.status {
                JobStatus::Completed => return Ok(status),
                JobStatus::Scraping => {
                    tokio::time::sleep(tokio::time::Duration::from_millis(poll_interval)).await;
                }
                JobStatus::Failed => {
                    return Err(FirecrawlError::JobFailed(
                        "Crawl job failed".to_string(),
                        JobStatus::Failed,
                    ));
                }
                JobStatus::Cancelled => {
                    return Err(FirecrawlError::JobFailed(
                        "Crawl job was cancelled".to_string(),
                        JobStatus::Cancelled,
                    ));
                }
            }
        }
    }

    /// Cancels a running crawl job.
    ///
    /// # Arguments
    ///
    /// * `id` - The crawl job ID to cancel.
    ///
    /// # Returns
    ///
    /// A `CancelCrawlResponse` indicating the cancellation status.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use firecrawl::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new("your-api-key")?;
    ///
    ///     let response = client.cancel_crawl("job-id").await?;
    ///     println!("Cancellation status: {}", response.status);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn cancel_crawl(
        &self,
        id: impl AsRef<str>,
    ) -> Result<CancelCrawlResponse, FirecrawlError> {
        let response = self
            .client
            .delete(self.url(&format!("/crawl/{}", id.as_ref())))
            .headers(self.prepare_headers(None))
            .send()
            .await
            .map_err(|e| {
                FirecrawlError::HttpError(format!("Cancelling crawl {}", id.as_ref()), e)
            })?;

        self.handle_response(response, "cancel crawl").await
    }

    /// Gets errors from a crawl job.
    ///
    /// # Arguments
    ///
    /// * `id` - The crawl job ID.
    ///
    /// # Returns
    ///
    /// A `CrawlErrorsResponse` containing error details.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use firecrawl::Client;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = Client::new("your-api-key")?;
    ///
    ///     let errors = client.get_crawl_errors("job-id").await?;
    ///     for error in errors.errors {
    ///         println!("Error on {}: {}", error.url, error.error);
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn get_crawl_errors(
        &self,
        id: impl AsRef<str>,
    ) -> Result<CrawlErrorsResponse, FirecrawlError> {
        let response = self
            .client
            .get(self.url(&format!("/crawl/{}/errors", id.as_ref())))
            .headers(self.prepare_headers(None))
            .send()
            .await
            .map_err(|e| {
                FirecrawlError::HttpError(format!("Getting crawl errors {}", id.as_ref()), e)
            })?;

        self.handle_response(response, "crawl errors").await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[tokio::test]
    async fn test_start_crawl_with_mock() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("POST", "/v2/crawl")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "id": "crawl-123",
                    "url": "https://api.firecrawl.dev/v2/crawl/crawl-123"
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let response = client
            .start_crawl("https://example.com", None)
            .await
            .unwrap();

        assert!(response.success);
        assert_eq!(response.id, "crawl-123");
        mock.assert();
    }

    #[tokio::test]
    async fn test_get_crawl_status_with_mock() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("GET", "/v2/crawl/crawl-123")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "status": "completed",
                    "total": 5,
                    "completed": 5,
                    "creditsUsed": 5,
                    "expiresAt": "2024-12-31T23:59:59Z",
                    "data": [
                        {
                            "markdown": "# Page 1",
                            "metadata": {
                                "sourceURL": "https://example.com/page1",
                                "statusCode": 200
                            }
                        },
                        {
                            "markdown": "# Page 2",
                            "metadata": {
                                "sourceURL": "https://example.com/page2",
                                "statusCode": 200
                            }
                        }
                    ]
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let status = client.get_crawl_status("crawl-123").await.unwrap();

        assert_eq!(status.status, JobStatus::Completed);
        assert_eq!(status.total, 5);
        assert_eq!(status.completed, 5);
        assert_eq!(status.data.len(), 2);
        mock.assert();
    }

    #[tokio::test]
    async fn test_cancel_crawl_with_mock() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("DELETE", "/v2/crawl/crawl-123")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "status": "cancelled"
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let response = client.cancel_crawl("crawl-123").await.unwrap();

        assert_eq!(response.status, "cancelled");
        mock.assert();
    }

    #[tokio::test]
    async fn test_get_crawl_errors_with_mock() {
        let mut server = mockito::Server::new_async().await;

        let mock = server
            .mock("GET", "/v2/crawl/crawl-123/errors")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "errors": [
                        {
                            "id": "error-1",
                            "timestamp": "2024-01-01T00:00:00Z",
                            "url": "https://example.com/broken",
                            "error": "404 Not Found"
                        }
                    ],
                    "robotsBlocked": [
                        "https://example.com/admin"
                    ]
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let errors = client.get_crawl_errors("crawl-123").await.unwrap();

        assert_eq!(errors.errors.len(), 1);
        assert_eq!(errors.errors[0].url, "https://example.com/broken");
        assert_eq!(errors.robots_blocked.len(), 1);
        mock.assert();
    }

    #[tokio::test]
    async fn test_crawl_with_options() {
        let mut server = mockito::Server::new_async().await;

        // Mock the start endpoint
        let start_mock = server
            .mock("POST", "/v2/crawl")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "success": true,
                    "id": "crawl-456",
                    "url": "https://api.firecrawl.dev/v2/crawl/crawl-456"
                })
                .to_string(),
            )
            .create();

        // Mock the status endpoint (completed immediately)
        let status_mock = server
            .mock("GET", "/v2/crawl/crawl-456")
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(
                json!({
                    "status": "completed",
                    "total": 2,
                    "completed": 2,
                    "data": [
                        {
                            "markdown": "# Page 1",
                            "metadata": { "sourceURL": "https://example.com/1", "statusCode": 200 }
                        }
                    ]
                })
                .to_string(),
            )
            .create();

        let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();
        let options = CrawlOptions {
            limit: Some(10),
            sitemap: Some(SitemapMode::Include),
            ..Default::default()
        };

        let result = client.crawl("https://example.com", options).await.unwrap();

        assert_eq!(result.status, JobStatus::Completed);
        assert_eq!(result.data.len(), 1);
        start_mock.assert();
        status_mock.assert();
    }
}