@mendable/firecrawl-js 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +72 -131
- package/package.json +1 -1
- package/src/index.ts +7 -9
- package/types/index.d.ts +7 -9
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Sideguide Technologies Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# Firecrawl
|
|
1
|
+
# Firecrawl Node SDK
|
|
2
2
|
|
|
3
|
-
The Firecrawl
|
|
3
|
+
The Firecrawl Node 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.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
-
To install the Firecrawl
|
|
7
|
+
To install the Firecrawl Node SDK, you can use npm:
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
npm install @mendable/firecrawl-js
|
|
@@ -15,44 +15,33 @@ npm install @mendable/firecrawl-js
|
|
|
15
15
|
1. Get an API key from [firecrawl.dev](https://firecrawl.dev)
|
|
16
16
|
2. Set the API key as an environment variable named `FIRECRAWL_API_KEY` or pass it as a parameter to the `FirecrawlApp` class.
|
|
17
17
|
|
|
18
|
-
|
|
19
18
|
Here's an example of how to use the SDK with error handling:
|
|
20
19
|
|
|
21
20
|
```js
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
limit: 1000,
|
|
41
|
-
},
|
|
42
|
-
pageOptions: {
|
|
43
|
-
onlyMainContent: true
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const crawlResult = await app.crawlUrl(crawlUrl, params);
|
|
48
|
-
console.log(crawlResult);
|
|
49
|
-
|
|
50
|
-
} catch (error) {
|
|
51
|
-
console.error('An error occurred:', error.message);
|
|
52
|
-
}
|
|
21
|
+
import FirecrawlApp, { CrawlParams, CrawlStatusResponse } from '@mendable/firecrawl-js';
|
|
22
|
+
|
|
23
|
+
const app = new FirecrawlApp({apiKey: "fc-YOUR_API_KEY"});
|
|
24
|
+
|
|
25
|
+
// Scrape a website
|
|
26
|
+
const scrapeResponse = await app.scrapeUrl('https://firecrawl.dev', {
|
|
27
|
+
formats: ['markdown', 'html'],
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (scrapeResponse) {
|
|
31
|
+
console.log(scrapeResponse)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Crawl a website
|
|
35
|
+
const crawlResponse = await app.crawlUrl('https://firecrawl.dev', {
|
|
36
|
+
limit: 100,
|
|
37
|
+
scrapeOptions: {
|
|
38
|
+
formats: ['markdown', 'html'],
|
|
53
39
|
}
|
|
40
|
+
} as CrawlParams, true, 30) as CrawlStatusResponse;
|
|
54
41
|
|
|
55
|
-
|
|
42
|
+
if (crawlResponse) {
|
|
43
|
+
console.log(crawlResponse)
|
|
44
|
+
}
|
|
56
45
|
```
|
|
57
46
|
|
|
58
47
|
### Scraping a URL
|
|
@@ -60,31 +49,49 @@ Here's an example of how to use the SDK with error handling:
|
|
|
60
49
|
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.
|
|
61
50
|
|
|
62
51
|
```js
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
52
|
+
const url = "https://example.com";
|
|
53
|
+
const scrapedData = await app.scrapeUrl(url);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Crawling a Website
|
|
57
|
+
|
|
58
|
+
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.
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
const crawlResponse = await app.crawlUrl('https://firecrawl.dev', {
|
|
62
|
+
limit: 100,
|
|
63
|
+
scrapeOptions: {
|
|
64
|
+
formats: ['markdown', 'html'],
|
|
75
65
|
}
|
|
76
|
-
|
|
77
|
-
|
|
66
|
+
} as CrawlParams, true, 30) as CrawlStatusResponse;
|
|
67
|
+
|
|
68
|
+
if (crawlResponse) {
|
|
69
|
+
console.log(crawlResponse)
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Checking Crawl Status
|
|
74
|
+
|
|
75
|
+
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.
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
const status = await app.checkCrawlStatus(id);
|
|
78
79
|
```
|
|
79
80
|
|
|
80
81
|
### Extracting structured data from a URL
|
|
81
82
|
|
|
82
|
-
With LLM extraction, you can easily extract structured data from any URL. We support zod
|
|
83
|
+
With LLM extraction, you can easily extract structured data from any URL. We support zod schema to make it easier for you too. Here is how you to use it:
|
|
83
84
|
|
|
84
85
|
```js
|
|
86
|
+
import FirecrawlApp from "@mendable/firecrawl-js";
|
|
85
87
|
import { z } from "zod";
|
|
86
88
|
|
|
87
|
-
const
|
|
89
|
+
const app = new FirecrawlApp({
|
|
90
|
+
apiKey: "fc-YOUR_API_KEY",
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Define schema to extract contents into
|
|
94
|
+
const schema = z.object({
|
|
88
95
|
top: z
|
|
89
96
|
.array(
|
|
90
97
|
z.object({
|
|
@@ -98,98 +105,32 @@ const zodSchema = z.object({
|
|
|
98
105
|
.describe("Top 5 stories on Hacker News"),
|
|
99
106
|
});
|
|
100
107
|
|
|
101
|
-
|
|
102
|
-
extractorOptions: { extractionSchema:
|
|
108
|
+
const scrapeResult = await app.scrapeUrl("https://firecrawl.dev", {
|
|
109
|
+
extractorOptions: { extractionSchema: schema },
|
|
103
110
|
});
|
|
104
111
|
|
|
105
|
-
console.log(
|
|
112
|
+
console.log(scrapeResult.data["llm_extraction"]);
|
|
106
113
|
```
|
|
107
114
|
|
|
108
|
-
###
|
|
115
|
+
### Map a Website
|
|
109
116
|
|
|
110
|
-
|
|
117
|
+
Use `map_url` to generate a list of URLs from a website. The `params` argument let you customize the mapping process, including options to exclude subdomains or to utilize the sitemap.
|
|
111
118
|
|
|
112
119
|
```js
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
### Crawling a Website
|
|
118
|
-
|
|
119
|
-
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.
|
|
120
|
-
|
|
121
|
-
```js
|
|
122
|
-
async function crawlExample() {
|
|
123
|
-
try {
|
|
124
|
-
const crawlUrl = 'https://example.com';
|
|
125
|
-
const params = {
|
|
126
|
-
crawlerOptions: {
|
|
127
|
-
excludes: ['blog/'],
|
|
128
|
-
includes: [], // leave empty for all pages
|
|
129
|
-
limit: 1000,
|
|
130
|
-
},
|
|
131
|
-
pageOptions: {
|
|
132
|
-
onlyMainContent: true
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
const waitUntilDone = true;
|
|
136
|
-
const timeout = 5;
|
|
137
|
-
const crawlResult = await app.crawlUrl(
|
|
138
|
-
crawlUrl,
|
|
139
|
-
params,
|
|
140
|
-
waitUntilDone,
|
|
141
|
-
timeout
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
console.log(crawlResult);
|
|
145
|
-
|
|
146
|
-
} catch (error) {
|
|
147
|
-
console.error(
|
|
148
|
-
'Error occurred while crawling:',
|
|
149
|
-
error.message
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
crawlExample();
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
### Checking Crawl Status
|
|
159
|
-
|
|
160
|
-
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.
|
|
161
|
-
|
|
162
|
-
```js
|
|
163
|
-
async function checkStatusExample(jobId) {
|
|
164
|
-
try {
|
|
165
|
-
const status = await app.checkCrawlStatus(jobId);
|
|
166
|
-
console.log(status);
|
|
167
|
-
|
|
168
|
-
} catch (error) {
|
|
169
|
-
console.error(
|
|
170
|
-
'Error occurred while checking crawl status:',
|
|
171
|
-
error.message
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
// Example usage, assuming you have a jobId
|
|
176
|
-
checkStatusExample('your_job_id_here');
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
## Running Locally
|
|
180
|
-
To use the SDK when running Firecrawl locally, you can change the initial Firecrawl app instance to:
|
|
181
|
-
```js
|
|
182
|
-
const app = new FirecrawlApp({ apiKey: "YOUR_API_KEY", apiUrl: "http://localhost:3002" });
|
|
120
|
+
const mapResult = await app.mapUrl('https://example.com') as MapResponse;
|
|
121
|
+
console.log(mapResult)
|
|
183
122
|
```
|
|
184
123
|
|
|
185
124
|
## Error Handling
|
|
186
125
|
|
|
187
126
|
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.
|
|
188
127
|
|
|
189
|
-
##
|
|
128
|
+
## License
|
|
190
129
|
|
|
191
|
-
|
|
130
|
+
The Firecrawl Node SDK is licensed under the MIT License. This means you are free to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the SDK, subject to the following conditions:
|
|
192
131
|
|
|
193
|
-
|
|
132
|
+
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
133
|
+
|
|
134
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
194
135
|
|
|
195
|
-
|
|
136
|
+
Please note that while this SDK is MIT licensed, it is part of a larger project which may be under different licensing terms. Always refer to the license information in the root directory of the main project for overall licensing details.
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -165,16 +165,14 @@ export interface ScrapeResponseV0 {
|
|
|
165
165
|
* Includes options for both scraping and mapping during a crawl.
|
|
166
166
|
*/
|
|
167
167
|
export interface CrawlParams {
|
|
168
|
+
includePaths?: string[];
|
|
169
|
+
excludePaths?: string[];
|
|
170
|
+
maxDepth?: number;
|
|
171
|
+
limit?: number;
|
|
172
|
+
allowBackwardLinks?: boolean;
|
|
173
|
+
allowExternalLinks?: boolean;
|
|
174
|
+
ignoreSitemap?: boolean;
|
|
168
175
|
scrapeOptions?: ScrapeParams;
|
|
169
|
-
crawlerOptions?: {
|
|
170
|
-
includePaths?: string[]
|
|
171
|
-
excludePaths?: string[]
|
|
172
|
-
maxDepth?: number
|
|
173
|
-
limit?: number
|
|
174
|
-
allowBackwardLinks?: boolean
|
|
175
|
-
allowExternalLinks?: boolean
|
|
176
|
-
ignoreSitemap?: boolean
|
|
177
|
-
};
|
|
178
176
|
}
|
|
179
177
|
|
|
180
178
|
/**
|
package/types/index.d.ts
CHANGED
|
@@ -153,16 +153,14 @@ export interface ScrapeResponseV0 {
|
|
|
153
153
|
* Includes options for both scraping and mapping during a crawl.
|
|
154
154
|
*/
|
|
155
155
|
export interface CrawlParams {
|
|
156
|
+
includePaths?: string[];
|
|
157
|
+
excludePaths?: string[];
|
|
158
|
+
maxDepth?: number;
|
|
159
|
+
limit?: number;
|
|
160
|
+
allowBackwardLinks?: boolean;
|
|
161
|
+
allowExternalLinks?: boolean;
|
|
162
|
+
ignoreSitemap?: boolean;
|
|
156
163
|
scrapeOptions?: ScrapeParams;
|
|
157
|
-
crawlerOptions?: {
|
|
158
|
-
includePaths?: string[];
|
|
159
|
-
excludePaths?: string[];
|
|
160
|
-
maxDepth?: number;
|
|
161
|
-
limit?: number;
|
|
162
|
-
allowBackwardLinks?: boolean;
|
|
163
|
-
allowExternalLinks?: boolean;
|
|
164
|
-
ignoreSitemap?: boolean;
|
|
165
|
-
};
|
|
166
164
|
}
|
|
167
165
|
/**
|
|
168
166
|
* Parameters for crawling operations on v0.
|