firecrawl/apps/js-sdk/example.js

76 lines
2.0 KiB
JavaScript
Raw Normal View History

2024-09-10 09:09:39 -03:00
import FirecrawlApp from 'firecrawl';
2024-11-14 15:51:27 -03:00
import { z } from 'zod';
2024-04-16 11:38:22 -03:00
2024-05-09 10:36:56 -03:00
const app = new FirecrawlApp({apiKey: "fc-YOUR_API_KEY"});
2024-04-16 11:38:22 -03:00
const main = async () => {
2024-08-08 11:41:13 -03:00
// Scrape a website:
const scrapeResult = await app.scrapeUrl('firecrawl.dev');
2024-04-16 11:38:22 -03:00
if (scrapeResult.success) {
console.log(scrapeResult.markdown)
}
2024-04-16 11:38:22 -03:00
// Crawl a website:
const crawlResult = await app.crawlUrl('mendable.ai', { excludePaths: ['blog/*'], limit: 5});
console.log(crawlResult);
// Asynchronously crawl a website:
const asyncCrawlResult = await app.asyncCrawlUrl('mendable.ai', { excludePaths: ['blog/*'], limit: 5});
if (asyncCrawlResult.success) {
const id = asyncCrawlResult.id;
console.log(id);
let checkStatus;
if (asyncCrawlResult.success) {
while (true) {
checkStatus = await app.checkCrawlStatus(id);
if (checkStatus.success && checkStatus.status === 'completed') {
break;
}
await new Promise(resolve => setTimeout(resolve, 1000)); // wait 1 second
}
if (checkStatus.success && checkStatus.data) {
console.log(checkStatus.data[0].markdown);
}
}
2024-04-16 11:38:22 -03:00
}
// Map a website:
const mapResult = await app.mapUrl('https://firecrawl.dev');
console.log(mapResult)
2024-11-14 15:51:27 -03:00
// Extract information from a website using LLM:
const extractSchema = z.object({
title: z.string(),
description: z.string(),
links: z.array(z.string())
});
const extractResult = await app.extractUrls(['https://firecrawl.dev'], {
prompt: "Extract the title, description, and links from the website",
schema: extractSchema
});
console.log(extractResult);
2024-05-09 10:36:56 -03:00
// Crawl a website with WebSockets:
const watch = await app.crawlUrlAndWatch('mendable.ai', { excludePaths: ['blog/*'], limit: 5});
2024-08-29 20:01:16 +02:00
watch.addEventListener("document", doc => {
console.log("DOC", doc.detail);
});
2024-08-29 20:01:16 +02:00
watch.addEventListener("error", err => {
console.error("ERR", err.detail.error);
});
2024-08-29 20:01:16 +02:00
watch.addEventListener("done", state => {
console.log("DONE", state.detail.status);
});
}
2024-08-29 20:01:16 +02:00
main()