firecrawl 4.2.0 → 4.3.1
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/dist/{chunk-MKEZ44MR.js → chunk-VFP5PS5Z.js} +1 -1
- package/dist/index.cjs +150 -5
- package/dist/index.d.cts +85 -1
- package/dist/index.d.ts +85 -1
- package/dist/index.js +150 -5
- package/dist/{package-7YDUGTZ6.js → package-ZQ5CJHCA.js} +1 -1
- package/package.json +1 -1
- package/src/v1/index.ts +138 -0
- package/src/v2/client.ts +11 -1
- package/src/v2/methods/usage.ts +40 -5
- package/src/v2/types.ts +30 -0
- package/src/v2/utils/validation.ts +15 -2
|
@@ -8,7 +8,7 @@ var require_package = __commonJS({
|
|
|
8
8
|
"package.json"(exports, module) {
|
|
9
9
|
module.exports = {
|
|
10
10
|
name: "@mendable/firecrawl-js",
|
|
11
|
-
version: "4.
|
|
11
|
+
version: "4.3.1",
|
|
12
12
|
description: "JavaScript SDK for Firecrawl API",
|
|
13
13
|
main: "dist/index.js",
|
|
14
14
|
types: "dist/index.d.ts",
|
package/dist/index.cjs
CHANGED
|
@@ -35,7 +35,7 @@ var require_package = __commonJS({
|
|
|
35
35
|
"package.json"(exports2, module2) {
|
|
36
36
|
module2.exports = {
|
|
37
37
|
name: "@mendable/firecrawl-js",
|
|
38
|
-
version: "4.
|
|
38
|
+
version: "4.3.1",
|
|
39
39
|
description: "JavaScript SDK for Firecrawl API",
|
|
40
40
|
main: "dist/index.js",
|
|
41
41
|
types: "dist/index.d.ts",
|
|
@@ -227,7 +227,7 @@ var SdkError = class extends Error {
|
|
|
227
227
|
};
|
|
228
228
|
|
|
229
229
|
// src/v2/utils/validation.ts
|
|
230
|
-
var import_zod_to_json_schema =
|
|
230
|
+
var import_zod_to_json_schema = require("zod-to-json-schema");
|
|
231
231
|
function ensureValidFormats(formats) {
|
|
232
232
|
if (!formats) return;
|
|
233
233
|
for (const fmt of formats) {
|
|
@@ -246,7 +246,19 @@ function ensureValidFormats(formats) {
|
|
|
246
246
|
const isZod = !!maybeSchema && (typeof maybeSchema.safeParse === "function" || typeof maybeSchema.parse === "function") && !!maybeSchema._def;
|
|
247
247
|
if (isZod) {
|
|
248
248
|
try {
|
|
249
|
-
j.schema = (0, import_zod_to_json_schema.
|
|
249
|
+
j.schema = (0, import_zod_to_json_schema.zodToJsonSchema)(maybeSchema);
|
|
250
|
+
} catch {
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
if (fmt.type === "changeTracking") {
|
|
256
|
+
const ct = fmt;
|
|
257
|
+
const maybeSchema = ct.schema;
|
|
258
|
+
const isZod = !!maybeSchema && (typeof maybeSchema.safeParse === "function" || typeof maybeSchema.parse === "function") && !!maybeSchema._def;
|
|
259
|
+
if (isZod) {
|
|
260
|
+
try {
|
|
261
|
+
ct.schema = (0, import_zod_to_json_schema.zodToJsonSchema)(maybeSchema);
|
|
250
262
|
} catch {
|
|
251
263
|
}
|
|
252
264
|
}
|
|
@@ -740,7 +752,12 @@ async function getCreditUsage(http) {
|
|
|
740
752
|
const res = await http.get("/v2/team/credit-usage");
|
|
741
753
|
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get credit usage");
|
|
742
754
|
const d = res.data.data || res.data;
|
|
743
|
-
return {
|
|
755
|
+
return {
|
|
756
|
+
remainingCredits: d.remainingCredits ?? d.remaining_credits ?? 0,
|
|
757
|
+
planCredits: d.planCredits ?? d.plan_credits,
|
|
758
|
+
billingPeriodStart: d.billingPeriodStart ?? d.billing_period_start ?? null,
|
|
759
|
+
billingPeriodEnd: d.billingPeriodEnd ?? d.billing_period_end ?? null
|
|
760
|
+
};
|
|
744
761
|
} catch (err) {
|
|
745
762
|
if (err?.isAxiosError) return normalizeAxiosError(err, "get credit usage");
|
|
746
763
|
throw err;
|
|
@@ -750,7 +767,13 @@ async function getTokenUsage(http) {
|
|
|
750
767
|
try {
|
|
751
768
|
const res = await http.get("/v2/team/token-usage");
|
|
752
769
|
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get token usage");
|
|
753
|
-
|
|
770
|
+
const d = res.data.data || res.data;
|
|
771
|
+
return {
|
|
772
|
+
remainingTokens: d.remainingTokens ?? d.remaining_tokens ?? 0,
|
|
773
|
+
planTokens: d.planTokens ?? d.plan_tokens,
|
|
774
|
+
billingPeriodStart: d.billingPeriodStart ?? d.billing_period_start ?? null,
|
|
775
|
+
billingPeriodEnd: d.billingPeriodEnd ?? d.billing_period_end ?? null
|
|
776
|
+
};
|
|
754
777
|
} catch (err) {
|
|
755
778
|
if (err?.isAxiosError) return normalizeAxiosError(err, "get token usage");
|
|
756
779
|
throw err;
|
|
@@ -766,6 +789,28 @@ async function getQueueStatus(http) {
|
|
|
766
789
|
throw err;
|
|
767
790
|
}
|
|
768
791
|
}
|
|
792
|
+
async function getCreditUsageHistorical(http, byApiKey) {
|
|
793
|
+
try {
|
|
794
|
+
const query = byApiKey ? "?byApiKey=true" : "";
|
|
795
|
+
const res = await http.get(`/v2/team/credit-usage/historical${query}`);
|
|
796
|
+
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get credit usage historical");
|
|
797
|
+
return res.data;
|
|
798
|
+
} catch (err) {
|
|
799
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "get credit usage historical");
|
|
800
|
+
throw err;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
async function getTokenUsageHistorical(http, byApiKey) {
|
|
804
|
+
try {
|
|
805
|
+
const query = byApiKey ? "?byApiKey=true" : "";
|
|
806
|
+
const res = await http.get(`/v2/team/token-usage/historical${query}`);
|
|
807
|
+
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get token usage historical");
|
|
808
|
+
return res.data;
|
|
809
|
+
} catch (err) {
|
|
810
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "get token usage historical");
|
|
811
|
+
throw err;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
769
814
|
|
|
770
815
|
// src/v2/watcher.ts
|
|
771
816
|
var import_events = require("events");
|
|
@@ -1073,6 +1118,14 @@ var FirecrawlClient = class {
|
|
|
1073
1118
|
async getTokenUsage() {
|
|
1074
1119
|
return getTokenUsage(this.http);
|
|
1075
1120
|
}
|
|
1121
|
+
/** Historical credit usage by month; set byApiKey to true to break down by API key. */
|
|
1122
|
+
async getCreditUsageHistorical(byApiKey) {
|
|
1123
|
+
return getCreditUsageHistorical(this.http, byApiKey);
|
|
1124
|
+
}
|
|
1125
|
+
/** Historical token usage by month; set byApiKey to true to break down by API key. */
|
|
1126
|
+
async getTokenUsageHistorical(byApiKey) {
|
|
1127
|
+
return getTokenUsageHistorical(this.http, byApiKey);
|
|
1128
|
+
}
|
|
1076
1129
|
/** Metrics about the team's scrape queue. */
|
|
1077
1130
|
async getQueueStatus() {
|
|
1078
1131
|
return getQueueStatus(this.http);
|
|
@@ -2348,6 +2401,98 @@ var FirecrawlApp = class {
|
|
|
2348
2401
|
}
|
|
2349
2402
|
return { success: false, error: "Internal server error." };
|
|
2350
2403
|
}
|
|
2404
|
+
/**
|
|
2405
|
+
* Gets current credit usage and billing period for the team (v1).
|
|
2406
|
+
*/
|
|
2407
|
+
async getCreditUsage() {
|
|
2408
|
+
const headers = this.prepareHeaders();
|
|
2409
|
+
try {
|
|
2410
|
+
const response = await this.getRequest(
|
|
2411
|
+
`${this.apiUrl}/v1/team/credit-usage`,
|
|
2412
|
+
headers
|
|
2413
|
+
);
|
|
2414
|
+
if (response.status === 200) {
|
|
2415
|
+
return response.data;
|
|
2416
|
+
} else {
|
|
2417
|
+
this.handleError(response, "get credit usage");
|
|
2418
|
+
}
|
|
2419
|
+
} catch (error) {
|
|
2420
|
+
if (error.response?.data?.error) {
|
|
2421
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
2422
|
+
} else {
|
|
2423
|
+
throw new FirecrawlError(error.message, 500);
|
|
2424
|
+
}
|
|
2425
|
+
}
|
|
2426
|
+
return { success: false, error: "Internal server error." };
|
|
2427
|
+
}
|
|
2428
|
+
/**
|
|
2429
|
+
* Gets current token usage and billing period for the team (v1).
|
|
2430
|
+
*/
|
|
2431
|
+
async getTokenUsage() {
|
|
2432
|
+
const headers = this.prepareHeaders();
|
|
2433
|
+
try {
|
|
2434
|
+
const response = await this.getRequest(
|
|
2435
|
+
`${this.apiUrl}/v1/team/token-usage`,
|
|
2436
|
+
headers
|
|
2437
|
+
);
|
|
2438
|
+
if (response.status === 200) {
|
|
2439
|
+
return response.data;
|
|
2440
|
+
} else {
|
|
2441
|
+
this.handleError(response, "get token usage");
|
|
2442
|
+
}
|
|
2443
|
+
} catch (error) {
|
|
2444
|
+
if (error.response?.data?.error) {
|
|
2445
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
2446
|
+
} else {
|
|
2447
|
+
throw new FirecrawlError(error.message, 500);
|
|
2448
|
+
}
|
|
2449
|
+
}
|
|
2450
|
+
return { success: false, error: "Internal server error." };
|
|
2451
|
+
}
|
|
2452
|
+
/**
|
|
2453
|
+
* Gets historical credit usage. Pass byApiKey=true to break down by API key.
|
|
2454
|
+
*/
|
|
2455
|
+
async getCreditUsageHistorical(byApiKey) {
|
|
2456
|
+
const headers = this.prepareHeaders();
|
|
2457
|
+
try {
|
|
2458
|
+
const url = `${this.apiUrl}/v1/team/credit-usage/historical${byApiKey ? "?byApiKey=true" : ""}`;
|
|
2459
|
+
const response = await this.getRequest(url, headers);
|
|
2460
|
+
if (response.status === 200) {
|
|
2461
|
+
return response.data;
|
|
2462
|
+
} else {
|
|
2463
|
+
this.handleError(response, "get credit usage historical");
|
|
2464
|
+
}
|
|
2465
|
+
} catch (error) {
|
|
2466
|
+
if (error.response?.data?.error) {
|
|
2467
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
2468
|
+
} else {
|
|
2469
|
+
throw new FirecrawlError(error.message, 500);
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
return { success: false, error: "Internal server error." };
|
|
2473
|
+
}
|
|
2474
|
+
/**
|
|
2475
|
+
* Gets historical token usage. Pass byApiKey=true to break down by API key.
|
|
2476
|
+
*/
|
|
2477
|
+
async getTokenUsageHistorical(byApiKey) {
|
|
2478
|
+
const headers = this.prepareHeaders();
|
|
2479
|
+
try {
|
|
2480
|
+
const url = `${this.apiUrl}/v1/team/token-usage/historical${byApiKey ? "?byApiKey=true" : ""}`;
|
|
2481
|
+
const response = await this.getRequest(url, headers);
|
|
2482
|
+
if (response.status === 200) {
|
|
2483
|
+
return response.data;
|
|
2484
|
+
} else {
|
|
2485
|
+
this.handleError(response, "get token usage historical");
|
|
2486
|
+
}
|
|
2487
|
+
} catch (error) {
|
|
2488
|
+
if (error.response?.data?.error) {
|
|
2489
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
2490
|
+
} else {
|
|
2491
|
+
throw new FirecrawlError(error.message, 500);
|
|
2492
|
+
}
|
|
2493
|
+
}
|
|
2494
|
+
return { success: false, error: "Internal server error." };
|
|
2495
|
+
}
|
|
2351
2496
|
};
|
|
2352
2497
|
var CrawlWatcher = class extends e {
|
|
2353
2498
|
ws;
|
package/dist/index.d.cts
CHANGED
|
@@ -288,9 +288,35 @@ interface ConcurrencyCheck {
|
|
|
288
288
|
}
|
|
289
289
|
interface CreditUsage {
|
|
290
290
|
remainingCredits: number;
|
|
291
|
+
planCredits?: number;
|
|
292
|
+
billingPeriodStart?: string | null;
|
|
293
|
+
billingPeriodEnd?: string | null;
|
|
291
294
|
}
|
|
292
295
|
interface TokenUsage {
|
|
293
296
|
remainingTokens: number;
|
|
297
|
+
planTokens?: number;
|
|
298
|
+
billingPeriodStart?: string | null;
|
|
299
|
+
billingPeriodEnd?: string | null;
|
|
300
|
+
}
|
|
301
|
+
interface CreditUsageHistoricalPeriod {
|
|
302
|
+
startDate: string | null;
|
|
303
|
+
endDate: string | null;
|
|
304
|
+
apiKey?: string;
|
|
305
|
+
creditsUsed: number;
|
|
306
|
+
}
|
|
307
|
+
interface CreditUsageHistoricalResponse {
|
|
308
|
+
success: boolean;
|
|
309
|
+
periods: CreditUsageHistoricalPeriod[];
|
|
310
|
+
}
|
|
311
|
+
interface TokenUsageHistoricalPeriod {
|
|
312
|
+
startDate: string | null;
|
|
313
|
+
endDate: string | null;
|
|
314
|
+
apiKey?: string;
|
|
315
|
+
tokensUsed: number;
|
|
316
|
+
}
|
|
317
|
+
interface TokenUsageHistoricalResponse {
|
|
318
|
+
success: boolean;
|
|
319
|
+
periods: TokenUsageHistoricalPeriod[];
|
|
294
320
|
}
|
|
295
321
|
interface CrawlErrorsResponse$1 {
|
|
296
322
|
errors: {
|
|
@@ -552,6 +578,10 @@ declare class FirecrawlClient {
|
|
|
552
578
|
getCreditUsage(): Promise<CreditUsage>;
|
|
553
579
|
/** Recent token usage. */
|
|
554
580
|
getTokenUsage(): Promise<TokenUsage>;
|
|
581
|
+
/** Historical credit usage by month; set byApiKey to true to break down by API key. */
|
|
582
|
+
getCreditUsageHistorical(byApiKey?: boolean): Promise<CreditUsageHistoricalResponse>;
|
|
583
|
+
/** Historical token usage by month; set byApiKey to true to break down by API key. */
|
|
584
|
+
getTokenUsageHistorical(byApiKey?: boolean): Promise<TokenUsageHistoricalResponse>;
|
|
555
585
|
/** Metrics about the team's scrape queue. */
|
|
556
586
|
getQueueStatus(): Promise<QueueStatusResponse$1>;
|
|
557
587
|
/**
|
|
@@ -1092,6 +1122,44 @@ interface QueueStatusResponse {
|
|
|
1092
1122
|
*/
|
|
1093
1123
|
mostRecentSuccess: string | null;
|
|
1094
1124
|
}
|
|
1125
|
+
/** Credit usage for v1 API (snake_case fields as returned by API). */
|
|
1126
|
+
interface CreditUsageResponseV1 {
|
|
1127
|
+
success: boolean;
|
|
1128
|
+
data: {
|
|
1129
|
+
remaining_credits: number;
|
|
1130
|
+
plan_credits: number;
|
|
1131
|
+
billing_period_start: string | null;
|
|
1132
|
+
billing_period_end: string | null;
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
/** Token usage for v1 API (snake_case fields as returned by API). */
|
|
1136
|
+
interface TokenUsageResponseV1 {
|
|
1137
|
+
success: boolean;
|
|
1138
|
+
data: {
|
|
1139
|
+
remaining_tokens: number;
|
|
1140
|
+
plan_tokens: number;
|
|
1141
|
+
billing_period_start: string | null;
|
|
1142
|
+
billing_period_end: string | null;
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
interface CreditUsageHistoricalResponseV1 {
|
|
1146
|
+
success: boolean;
|
|
1147
|
+
periods: {
|
|
1148
|
+
startDate: string | null;
|
|
1149
|
+
endDate: string | null;
|
|
1150
|
+
apiKey?: string;
|
|
1151
|
+
creditsUsed: number;
|
|
1152
|
+
}[];
|
|
1153
|
+
}
|
|
1154
|
+
interface TokenUsageHistoricalResponseV1 {
|
|
1155
|
+
success: boolean;
|
|
1156
|
+
periods: {
|
|
1157
|
+
startDate: string | null;
|
|
1158
|
+
endDate: string | null;
|
|
1159
|
+
apiKey?: string;
|
|
1160
|
+
tokensUsed: number;
|
|
1161
|
+
}[];
|
|
1162
|
+
}
|
|
1095
1163
|
/**
|
|
1096
1164
|
* Main class for interacting with the Firecrawl API.
|
|
1097
1165
|
* Provides methods for scraping, searching, crawling, and mapping web content.
|
|
@@ -1362,6 +1430,22 @@ declare class FirecrawlApp {
|
|
|
1362
1430
|
* @returns The current queue status.
|
|
1363
1431
|
*/
|
|
1364
1432
|
getQueueStatus(): Promise<QueueStatusResponse | ErrorResponse>;
|
|
1433
|
+
/**
|
|
1434
|
+
* Gets current credit usage and billing period for the team (v1).
|
|
1435
|
+
*/
|
|
1436
|
+
getCreditUsage(): Promise<CreditUsageResponseV1 | ErrorResponse>;
|
|
1437
|
+
/**
|
|
1438
|
+
* Gets current token usage and billing period for the team (v1).
|
|
1439
|
+
*/
|
|
1440
|
+
getTokenUsage(): Promise<TokenUsageResponseV1 | ErrorResponse>;
|
|
1441
|
+
/**
|
|
1442
|
+
* Gets historical credit usage. Pass byApiKey=true to break down by API key.
|
|
1443
|
+
*/
|
|
1444
|
+
getCreditUsageHistorical(byApiKey?: boolean): Promise<CreditUsageHistoricalResponseV1 | ErrorResponse>;
|
|
1445
|
+
/**
|
|
1446
|
+
* Gets historical token usage. Pass byApiKey=true to break down by API key.
|
|
1447
|
+
*/
|
|
1448
|
+
getTokenUsageHistorical(byApiKey?: boolean): Promise<TokenUsageHistoricalResponseV1 | ErrorResponse>;
|
|
1365
1449
|
}
|
|
1366
1450
|
interface CrawlWatcherEvents {
|
|
1367
1451
|
document: CustomEvent<FirecrawlDocument<undefined>>;
|
|
@@ -1403,4 +1487,4 @@ declare class Firecrawl extends FirecrawlClient {
|
|
|
1403
1487
|
get v1(): FirecrawlApp;
|
|
1404
1488
|
}
|
|
1405
1489
|
|
|
1406
|
-
export { type ActionOption, type ActiveCrawl, type ActiveCrawlsResponse, type AttributesFormat, type BatchScrapeJob, type BatchScrapeOptions, type BatchScrapeResponse$1 as BatchScrapeResponse, type CategoryOption, type ChangeTrackingFormat, type ClickAction, type ConcurrencyCheck, type CrawlErrorsResponse$1 as CrawlErrorsResponse, type CrawlJob, type CrawlOptions, type CrawlResponse$1 as CrawlResponse, type CreditUsage, type Document, type DocumentMetadata, type ErrorDetails, type ExecuteJavascriptAction, type ExtractResponse$1 as ExtractResponse, Firecrawl, FirecrawlApp as FirecrawlAppV1, FirecrawlClient, type FirecrawlClientOptions, type Format, type FormatOption, type FormatString, type JsonFormat, type LocationConfig, type MapData, type MapOptions, type PDFAction, type PaginationConfig, type PressAction, type QueueStatusResponse$1 as QueueStatusResponse, type ScrapeAction, type ScrapeOptions, type ScreenshotAction, type ScreenshotFormat, type ScrollAction, SdkError, type SearchData, type SearchRequest, type SearchResultImages, type SearchResultNews, type SearchResultWeb, type TokenUsage, type Viewport, type WaitAction, type WebhookConfig, type WriteAction, Firecrawl as default };
|
|
1490
|
+
export { type ActionOption, type ActiveCrawl, type ActiveCrawlsResponse, type AttributesFormat, type BatchScrapeJob, type BatchScrapeOptions, type BatchScrapeResponse$1 as BatchScrapeResponse, type CategoryOption, type ChangeTrackingFormat, type ClickAction, type ConcurrencyCheck, type CrawlErrorsResponse$1 as CrawlErrorsResponse, type CrawlJob, type CrawlOptions, type CrawlResponse$1 as CrawlResponse, type CreditUsage, type CreditUsageHistoricalPeriod, type CreditUsageHistoricalResponse, type Document, type DocumentMetadata, type ErrorDetails, type ExecuteJavascriptAction, type ExtractResponse$1 as ExtractResponse, Firecrawl, FirecrawlApp as FirecrawlAppV1, FirecrawlClient, type FirecrawlClientOptions, type Format, type FormatOption, type FormatString, type JsonFormat, type LocationConfig, type MapData, type MapOptions, type PDFAction, type PaginationConfig, type PressAction, type QueueStatusResponse$1 as QueueStatusResponse, type ScrapeAction, type ScrapeOptions, type ScreenshotAction, type ScreenshotFormat, type ScrollAction, SdkError, type SearchData, type SearchRequest, type SearchResultImages, type SearchResultNews, type SearchResultWeb, type TokenUsage, type TokenUsageHistoricalPeriod, type TokenUsageHistoricalResponse, type Viewport, type WaitAction, type WebhookConfig, type WriteAction, Firecrawl as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -288,9 +288,35 @@ interface ConcurrencyCheck {
|
|
|
288
288
|
}
|
|
289
289
|
interface CreditUsage {
|
|
290
290
|
remainingCredits: number;
|
|
291
|
+
planCredits?: number;
|
|
292
|
+
billingPeriodStart?: string | null;
|
|
293
|
+
billingPeriodEnd?: string | null;
|
|
291
294
|
}
|
|
292
295
|
interface TokenUsage {
|
|
293
296
|
remainingTokens: number;
|
|
297
|
+
planTokens?: number;
|
|
298
|
+
billingPeriodStart?: string | null;
|
|
299
|
+
billingPeriodEnd?: string | null;
|
|
300
|
+
}
|
|
301
|
+
interface CreditUsageHistoricalPeriod {
|
|
302
|
+
startDate: string | null;
|
|
303
|
+
endDate: string | null;
|
|
304
|
+
apiKey?: string;
|
|
305
|
+
creditsUsed: number;
|
|
306
|
+
}
|
|
307
|
+
interface CreditUsageHistoricalResponse {
|
|
308
|
+
success: boolean;
|
|
309
|
+
periods: CreditUsageHistoricalPeriod[];
|
|
310
|
+
}
|
|
311
|
+
interface TokenUsageHistoricalPeriod {
|
|
312
|
+
startDate: string | null;
|
|
313
|
+
endDate: string | null;
|
|
314
|
+
apiKey?: string;
|
|
315
|
+
tokensUsed: number;
|
|
316
|
+
}
|
|
317
|
+
interface TokenUsageHistoricalResponse {
|
|
318
|
+
success: boolean;
|
|
319
|
+
periods: TokenUsageHistoricalPeriod[];
|
|
294
320
|
}
|
|
295
321
|
interface CrawlErrorsResponse$1 {
|
|
296
322
|
errors: {
|
|
@@ -552,6 +578,10 @@ declare class FirecrawlClient {
|
|
|
552
578
|
getCreditUsage(): Promise<CreditUsage>;
|
|
553
579
|
/** Recent token usage. */
|
|
554
580
|
getTokenUsage(): Promise<TokenUsage>;
|
|
581
|
+
/** Historical credit usage by month; set byApiKey to true to break down by API key. */
|
|
582
|
+
getCreditUsageHistorical(byApiKey?: boolean): Promise<CreditUsageHistoricalResponse>;
|
|
583
|
+
/** Historical token usage by month; set byApiKey to true to break down by API key. */
|
|
584
|
+
getTokenUsageHistorical(byApiKey?: boolean): Promise<TokenUsageHistoricalResponse>;
|
|
555
585
|
/** Metrics about the team's scrape queue. */
|
|
556
586
|
getQueueStatus(): Promise<QueueStatusResponse$1>;
|
|
557
587
|
/**
|
|
@@ -1092,6 +1122,44 @@ interface QueueStatusResponse {
|
|
|
1092
1122
|
*/
|
|
1093
1123
|
mostRecentSuccess: string | null;
|
|
1094
1124
|
}
|
|
1125
|
+
/** Credit usage for v1 API (snake_case fields as returned by API). */
|
|
1126
|
+
interface CreditUsageResponseV1 {
|
|
1127
|
+
success: boolean;
|
|
1128
|
+
data: {
|
|
1129
|
+
remaining_credits: number;
|
|
1130
|
+
plan_credits: number;
|
|
1131
|
+
billing_period_start: string | null;
|
|
1132
|
+
billing_period_end: string | null;
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
/** Token usage for v1 API (snake_case fields as returned by API). */
|
|
1136
|
+
interface TokenUsageResponseV1 {
|
|
1137
|
+
success: boolean;
|
|
1138
|
+
data: {
|
|
1139
|
+
remaining_tokens: number;
|
|
1140
|
+
plan_tokens: number;
|
|
1141
|
+
billing_period_start: string | null;
|
|
1142
|
+
billing_period_end: string | null;
|
|
1143
|
+
};
|
|
1144
|
+
}
|
|
1145
|
+
interface CreditUsageHistoricalResponseV1 {
|
|
1146
|
+
success: boolean;
|
|
1147
|
+
periods: {
|
|
1148
|
+
startDate: string | null;
|
|
1149
|
+
endDate: string | null;
|
|
1150
|
+
apiKey?: string;
|
|
1151
|
+
creditsUsed: number;
|
|
1152
|
+
}[];
|
|
1153
|
+
}
|
|
1154
|
+
interface TokenUsageHistoricalResponseV1 {
|
|
1155
|
+
success: boolean;
|
|
1156
|
+
periods: {
|
|
1157
|
+
startDate: string | null;
|
|
1158
|
+
endDate: string | null;
|
|
1159
|
+
apiKey?: string;
|
|
1160
|
+
tokensUsed: number;
|
|
1161
|
+
}[];
|
|
1162
|
+
}
|
|
1095
1163
|
/**
|
|
1096
1164
|
* Main class for interacting with the Firecrawl API.
|
|
1097
1165
|
* Provides methods for scraping, searching, crawling, and mapping web content.
|
|
@@ -1362,6 +1430,22 @@ declare class FirecrawlApp {
|
|
|
1362
1430
|
* @returns The current queue status.
|
|
1363
1431
|
*/
|
|
1364
1432
|
getQueueStatus(): Promise<QueueStatusResponse | ErrorResponse>;
|
|
1433
|
+
/**
|
|
1434
|
+
* Gets current credit usage and billing period for the team (v1).
|
|
1435
|
+
*/
|
|
1436
|
+
getCreditUsage(): Promise<CreditUsageResponseV1 | ErrorResponse>;
|
|
1437
|
+
/**
|
|
1438
|
+
* Gets current token usage and billing period for the team (v1).
|
|
1439
|
+
*/
|
|
1440
|
+
getTokenUsage(): Promise<TokenUsageResponseV1 | ErrorResponse>;
|
|
1441
|
+
/**
|
|
1442
|
+
* Gets historical credit usage. Pass byApiKey=true to break down by API key.
|
|
1443
|
+
*/
|
|
1444
|
+
getCreditUsageHistorical(byApiKey?: boolean): Promise<CreditUsageHistoricalResponseV1 | ErrorResponse>;
|
|
1445
|
+
/**
|
|
1446
|
+
* Gets historical token usage. Pass byApiKey=true to break down by API key.
|
|
1447
|
+
*/
|
|
1448
|
+
getTokenUsageHistorical(byApiKey?: boolean): Promise<TokenUsageHistoricalResponseV1 | ErrorResponse>;
|
|
1365
1449
|
}
|
|
1366
1450
|
interface CrawlWatcherEvents {
|
|
1367
1451
|
document: CustomEvent<FirecrawlDocument<undefined>>;
|
|
@@ -1403,4 +1487,4 @@ declare class Firecrawl extends FirecrawlClient {
|
|
|
1403
1487
|
get v1(): FirecrawlApp;
|
|
1404
1488
|
}
|
|
1405
1489
|
|
|
1406
|
-
export { type ActionOption, type ActiveCrawl, type ActiveCrawlsResponse, type AttributesFormat, type BatchScrapeJob, type BatchScrapeOptions, type BatchScrapeResponse$1 as BatchScrapeResponse, type CategoryOption, type ChangeTrackingFormat, type ClickAction, type ConcurrencyCheck, type CrawlErrorsResponse$1 as CrawlErrorsResponse, type CrawlJob, type CrawlOptions, type CrawlResponse$1 as CrawlResponse, type CreditUsage, type Document, type DocumentMetadata, type ErrorDetails, type ExecuteJavascriptAction, type ExtractResponse$1 as ExtractResponse, Firecrawl, FirecrawlApp as FirecrawlAppV1, FirecrawlClient, type FirecrawlClientOptions, type Format, type FormatOption, type FormatString, type JsonFormat, type LocationConfig, type MapData, type MapOptions, type PDFAction, type PaginationConfig, type PressAction, type QueueStatusResponse$1 as QueueStatusResponse, type ScrapeAction, type ScrapeOptions, type ScreenshotAction, type ScreenshotFormat, type ScrollAction, SdkError, type SearchData, type SearchRequest, type SearchResultImages, type SearchResultNews, type SearchResultWeb, type TokenUsage, type Viewport, type WaitAction, type WebhookConfig, type WriteAction, Firecrawl as default };
|
|
1490
|
+
export { type ActionOption, type ActiveCrawl, type ActiveCrawlsResponse, type AttributesFormat, type BatchScrapeJob, type BatchScrapeOptions, type BatchScrapeResponse$1 as BatchScrapeResponse, type CategoryOption, type ChangeTrackingFormat, type ClickAction, type ConcurrencyCheck, type CrawlErrorsResponse$1 as CrawlErrorsResponse, type CrawlJob, type CrawlOptions, type CrawlResponse$1 as CrawlResponse, type CreditUsage, type CreditUsageHistoricalPeriod, type CreditUsageHistoricalResponse, type Document, type DocumentMetadata, type ErrorDetails, type ExecuteJavascriptAction, type ExtractResponse$1 as ExtractResponse, Firecrawl, FirecrawlApp as FirecrawlAppV1, FirecrawlClient, type FirecrawlClientOptions, type Format, type FormatOption, type FormatString, type JsonFormat, type LocationConfig, type MapData, type MapOptions, type PDFAction, type PaginationConfig, type PressAction, type QueueStatusResponse$1 as QueueStatusResponse, type ScrapeAction, type ScrapeOptions, type ScreenshotAction, type ScreenshotFormat, type ScrollAction, SdkError, type SearchData, type SearchRequest, type SearchResultImages, type SearchResultNews, type SearchResultWeb, type TokenUsage, type TokenUsageHistoricalPeriod, type TokenUsageHistoricalResponse, type Viewport, type WaitAction, type WebhookConfig, type WriteAction, Firecrawl as default };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
require_package
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-VFP5PS5Z.js";
|
|
4
4
|
|
|
5
5
|
// src/v2/utils/httpClient.ts
|
|
6
6
|
import axios from "axios";
|
|
@@ -111,7 +111,7 @@ var SdkError = class extends Error {
|
|
|
111
111
|
};
|
|
112
112
|
|
|
113
113
|
// src/v2/utils/validation.ts
|
|
114
|
-
import zodToJsonSchema from "zod-to-json-schema";
|
|
114
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
115
115
|
function ensureValidFormats(formats) {
|
|
116
116
|
if (!formats) return;
|
|
117
117
|
for (const fmt of formats) {
|
|
@@ -136,6 +136,18 @@ function ensureValidFormats(formats) {
|
|
|
136
136
|
}
|
|
137
137
|
continue;
|
|
138
138
|
}
|
|
139
|
+
if (fmt.type === "changeTracking") {
|
|
140
|
+
const ct = fmt;
|
|
141
|
+
const maybeSchema = ct.schema;
|
|
142
|
+
const isZod = !!maybeSchema && (typeof maybeSchema.safeParse === "function" || typeof maybeSchema.parse === "function") && !!maybeSchema._def;
|
|
143
|
+
if (isZod) {
|
|
144
|
+
try {
|
|
145
|
+
ct.schema = zodToJsonSchema(maybeSchema);
|
|
146
|
+
} catch {
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
139
151
|
if (fmt.type === "screenshot") {
|
|
140
152
|
const s = fmt;
|
|
141
153
|
if (s.quality != null && (typeof s.quality !== "number" || s.quality < 0)) {
|
|
@@ -624,7 +636,12 @@ async function getCreditUsage(http) {
|
|
|
624
636
|
const res = await http.get("/v2/team/credit-usage");
|
|
625
637
|
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get credit usage");
|
|
626
638
|
const d = res.data.data || res.data;
|
|
627
|
-
return {
|
|
639
|
+
return {
|
|
640
|
+
remainingCredits: d.remainingCredits ?? d.remaining_credits ?? 0,
|
|
641
|
+
planCredits: d.planCredits ?? d.plan_credits,
|
|
642
|
+
billingPeriodStart: d.billingPeriodStart ?? d.billing_period_start ?? null,
|
|
643
|
+
billingPeriodEnd: d.billingPeriodEnd ?? d.billing_period_end ?? null
|
|
644
|
+
};
|
|
628
645
|
} catch (err) {
|
|
629
646
|
if (err?.isAxiosError) return normalizeAxiosError(err, "get credit usage");
|
|
630
647
|
throw err;
|
|
@@ -634,7 +651,13 @@ async function getTokenUsage(http) {
|
|
|
634
651
|
try {
|
|
635
652
|
const res = await http.get("/v2/team/token-usage");
|
|
636
653
|
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get token usage");
|
|
637
|
-
|
|
654
|
+
const d = res.data.data || res.data;
|
|
655
|
+
return {
|
|
656
|
+
remainingTokens: d.remainingTokens ?? d.remaining_tokens ?? 0,
|
|
657
|
+
planTokens: d.planTokens ?? d.plan_tokens,
|
|
658
|
+
billingPeriodStart: d.billingPeriodStart ?? d.billing_period_start ?? null,
|
|
659
|
+
billingPeriodEnd: d.billingPeriodEnd ?? d.billing_period_end ?? null
|
|
660
|
+
};
|
|
638
661
|
} catch (err) {
|
|
639
662
|
if (err?.isAxiosError) return normalizeAxiosError(err, "get token usage");
|
|
640
663
|
throw err;
|
|
@@ -650,6 +673,28 @@ async function getQueueStatus(http) {
|
|
|
650
673
|
throw err;
|
|
651
674
|
}
|
|
652
675
|
}
|
|
676
|
+
async function getCreditUsageHistorical(http, byApiKey) {
|
|
677
|
+
try {
|
|
678
|
+
const query = byApiKey ? "?byApiKey=true" : "";
|
|
679
|
+
const res = await http.get(`/v2/team/credit-usage/historical${query}`);
|
|
680
|
+
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get credit usage historical");
|
|
681
|
+
return res.data;
|
|
682
|
+
} catch (err) {
|
|
683
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "get credit usage historical");
|
|
684
|
+
throw err;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
async function getTokenUsageHistorical(http, byApiKey) {
|
|
688
|
+
try {
|
|
689
|
+
const query = byApiKey ? "?byApiKey=true" : "";
|
|
690
|
+
const res = await http.get(`/v2/team/token-usage/historical${query}`);
|
|
691
|
+
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get token usage historical");
|
|
692
|
+
return res.data;
|
|
693
|
+
} catch (err) {
|
|
694
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "get token usage historical");
|
|
695
|
+
throw err;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
653
698
|
|
|
654
699
|
// src/v2/watcher.ts
|
|
655
700
|
import { EventEmitter } from "events";
|
|
@@ -957,6 +1002,14 @@ var FirecrawlClient = class {
|
|
|
957
1002
|
async getTokenUsage() {
|
|
958
1003
|
return getTokenUsage(this.http);
|
|
959
1004
|
}
|
|
1005
|
+
/** Historical credit usage by month; set byApiKey to true to break down by API key. */
|
|
1006
|
+
async getCreditUsageHistorical(byApiKey) {
|
|
1007
|
+
return getCreditUsageHistorical(this.http, byApiKey);
|
|
1008
|
+
}
|
|
1009
|
+
/** Historical token usage by month; set byApiKey to true to break down by API key. */
|
|
1010
|
+
async getTokenUsageHistorical(byApiKey) {
|
|
1011
|
+
return getTokenUsageHistorical(this.http, byApiKey);
|
|
1012
|
+
}
|
|
960
1013
|
/** Metrics about the team's scrape queue. */
|
|
961
1014
|
async getQueueStatus() {
|
|
962
1015
|
return getQueueStatus(this.http);
|
|
@@ -1006,7 +1059,7 @@ var FirecrawlApp = class {
|
|
|
1006
1059
|
if (typeof process !== "undefined" && process.env && process.env.npm_package_version) {
|
|
1007
1060
|
return process.env.npm_package_version;
|
|
1008
1061
|
}
|
|
1009
|
-
const packageJson = await import("./package-
|
|
1062
|
+
const packageJson = await import("./package-ZQ5CJHCA.js");
|
|
1010
1063
|
return packageJson.default.version;
|
|
1011
1064
|
} catch (error) {
|
|
1012
1065
|
const isTest = typeof process !== "undefined" && (process.env.JEST_WORKER_ID != null || false);
|
|
@@ -2232,6 +2285,98 @@ var FirecrawlApp = class {
|
|
|
2232
2285
|
}
|
|
2233
2286
|
return { success: false, error: "Internal server error." };
|
|
2234
2287
|
}
|
|
2288
|
+
/**
|
|
2289
|
+
* Gets current credit usage and billing period for the team (v1).
|
|
2290
|
+
*/
|
|
2291
|
+
async getCreditUsage() {
|
|
2292
|
+
const headers = this.prepareHeaders();
|
|
2293
|
+
try {
|
|
2294
|
+
const response = await this.getRequest(
|
|
2295
|
+
`${this.apiUrl}/v1/team/credit-usage`,
|
|
2296
|
+
headers
|
|
2297
|
+
);
|
|
2298
|
+
if (response.status === 200) {
|
|
2299
|
+
return response.data;
|
|
2300
|
+
} else {
|
|
2301
|
+
this.handleError(response, "get credit usage");
|
|
2302
|
+
}
|
|
2303
|
+
} catch (error) {
|
|
2304
|
+
if (error.response?.data?.error) {
|
|
2305
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
2306
|
+
} else {
|
|
2307
|
+
throw new FirecrawlError(error.message, 500);
|
|
2308
|
+
}
|
|
2309
|
+
}
|
|
2310
|
+
return { success: false, error: "Internal server error." };
|
|
2311
|
+
}
|
|
2312
|
+
/**
|
|
2313
|
+
* Gets current token usage and billing period for the team (v1).
|
|
2314
|
+
*/
|
|
2315
|
+
async getTokenUsage() {
|
|
2316
|
+
const headers = this.prepareHeaders();
|
|
2317
|
+
try {
|
|
2318
|
+
const response = await this.getRequest(
|
|
2319
|
+
`${this.apiUrl}/v1/team/token-usage`,
|
|
2320
|
+
headers
|
|
2321
|
+
);
|
|
2322
|
+
if (response.status === 200) {
|
|
2323
|
+
return response.data;
|
|
2324
|
+
} else {
|
|
2325
|
+
this.handleError(response, "get token usage");
|
|
2326
|
+
}
|
|
2327
|
+
} catch (error) {
|
|
2328
|
+
if (error.response?.data?.error) {
|
|
2329
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
2330
|
+
} else {
|
|
2331
|
+
throw new FirecrawlError(error.message, 500);
|
|
2332
|
+
}
|
|
2333
|
+
}
|
|
2334
|
+
return { success: false, error: "Internal server error." };
|
|
2335
|
+
}
|
|
2336
|
+
/**
|
|
2337
|
+
* Gets historical credit usage. Pass byApiKey=true to break down by API key.
|
|
2338
|
+
*/
|
|
2339
|
+
async getCreditUsageHistorical(byApiKey) {
|
|
2340
|
+
const headers = this.prepareHeaders();
|
|
2341
|
+
try {
|
|
2342
|
+
const url = `${this.apiUrl}/v1/team/credit-usage/historical${byApiKey ? "?byApiKey=true" : ""}`;
|
|
2343
|
+
const response = await this.getRequest(url, headers);
|
|
2344
|
+
if (response.status === 200) {
|
|
2345
|
+
return response.data;
|
|
2346
|
+
} else {
|
|
2347
|
+
this.handleError(response, "get credit usage historical");
|
|
2348
|
+
}
|
|
2349
|
+
} catch (error) {
|
|
2350
|
+
if (error.response?.data?.error) {
|
|
2351
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
2352
|
+
} else {
|
|
2353
|
+
throw new FirecrawlError(error.message, 500);
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
return { success: false, error: "Internal server error." };
|
|
2357
|
+
}
|
|
2358
|
+
/**
|
|
2359
|
+
* Gets historical token usage. Pass byApiKey=true to break down by API key.
|
|
2360
|
+
*/
|
|
2361
|
+
async getTokenUsageHistorical(byApiKey) {
|
|
2362
|
+
const headers = this.prepareHeaders();
|
|
2363
|
+
try {
|
|
2364
|
+
const url = `${this.apiUrl}/v1/team/token-usage/historical${byApiKey ? "?byApiKey=true" : ""}`;
|
|
2365
|
+
const response = await this.getRequest(url, headers);
|
|
2366
|
+
if (response.status === 200) {
|
|
2367
|
+
return response.data;
|
|
2368
|
+
} else {
|
|
2369
|
+
this.handleError(response, "get token usage historical");
|
|
2370
|
+
}
|
|
2371
|
+
} catch (error) {
|
|
2372
|
+
if (error.response?.data?.error) {
|
|
2373
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ""}`, error.response.status);
|
|
2374
|
+
} else {
|
|
2375
|
+
throw new FirecrawlError(error.message, 500);
|
|
2376
|
+
}
|
|
2377
|
+
}
|
|
2378
|
+
return { success: false, error: "Internal server error." };
|
|
2379
|
+
}
|
|
2235
2380
|
};
|
|
2236
2381
|
var CrawlWatcher = class extends e {
|
|
2237
2382
|
ws;
|
package/package.json
CHANGED
package/src/v1/index.ts
CHANGED
|
@@ -584,6 +584,48 @@ export interface QueueStatusResponse {
|
|
|
584
584
|
mostRecentSuccess: string | null;
|
|
585
585
|
}
|
|
586
586
|
|
|
587
|
+
/** Credit usage for v1 API (snake_case fields as returned by API). */
|
|
588
|
+
export interface CreditUsageResponseV1 {
|
|
589
|
+
success: boolean;
|
|
590
|
+
data: {
|
|
591
|
+
remaining_credits: number;
|
|
592
|
+
plan_credits: number;
|
|
593
|
+
billing_period_start: string | null;
|
|
594
|
+
billing_period_end: string | null;
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** Token usage for v1 API (snake_case fields as returned by API). */
|
|
599
|
+
export interface TokenUsageResponseV1 {
|
|
600
|
+
success: boolean;
|
|
601
|
+
data: {
|
|
602
|
+
remaining_tokens: number;
|
|
603
|
+
plan_tokens: number;
|
|
604
|
+
billing_period_start: string | null;
|
|
605
|
+
billing_period_end: string | null;
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
export interface CreditUsageHistoricalResponseV1 {
|
|
610
|
+
success: boolean;
|
|
611
|
+
periods: {
|
|
612
|
+
startDate: string | null;
|
|
613
|
+
endDate: string | null;
|
|
614
|
+
apiKey?: string;
|
|
615
|
+
creditsUsed: number;
|
|
616
|
+
}[];
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
export interface TokenUsageHistoricalResponseV1 {
|
|
620
|
+
success: boolean;
|
|
621
|
+
periods: {
|
|
622
|
+
startDate: string | null;
|
|
623
|
+
endDate: string | null;
|
|
624
|
+
apiKey?: string;
|
|
625
|
+
tokensUsed: number;
|
|
626
|
+
}[];
|
|
627
|
+
}
|
|
628
|
+
|
|
587
629
|
/**
|
|
588
630
|
* Main class for interacting with the Firecrawl API.
|
|
589
631
|
* Provides methods for scraping, searching, crawling, and mapping web content.
|
|
@@ -2070,6 +2112,102 @@ export default class FirecrawlApp {
|
|
|
2070
2112
|
}
|
|
2071
2113
|
return { success: false, error: "Internal server error." };
|
|
2072
2114
|
}
|
|
2115
|
+
|
|
2116
|
+
/**
|
|
2117
|
+
* Gets current credit usage and billing period for the team (v1).
|
|
2118
|
+
*/
|
|
2119
|
+
async getCreditUsage(): Promise<CreditUsageResponseV1 | ErrorResponse> {
|
|
2120
|
+
const headers = this.prepareHeaders();
|
|
2121
|
+
try {
|
|
2122
|
+
const response: AxiosResponse = await this.getRequest(
|
|
2123
|
+
`${this.apiUrl}/v1/team/credit-usage`,
|
|
2124
|
+
headers
|
|
2125
|
+
);
|
|
2126
|
+
if (response.status === 200) {
|
|
2127
|
+
return response.data as CreditUsageResponseV1;
|
|
2128
|
+
} else {
|
|
2129
|
+
this.handleError(response, "get credit usage");
|
|
2130
|
+
}
|
|
2131
|
+
} catch (error: any) {
|
|
2132
|
+
if (error.response?.data?.error) {
|
|
2133
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
|
|
2134
|
+
} else {
|
|
2135
|
+
throw new FirecrawlError(error.message, 500);
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
return { success: false, error: "Internal server error." };
|
|
2139
|
+
}
|
|
2140
|
+
|
|
2141
|
+
/**
|
|
2142
|
+
* Gets current token usage and billing period for the team (v1).
|
|
2143
|
+
*/
|
|
2144
|
+
async getTokenUsage(): Promise<TokenUsageResponseV1 | ErrorResponse> {
|
|
2145
|
+
const headers = this.prepareHeaders();
|
|
2146
|
+
try {
|
|
2147
|
+
const response: AxiosResponse = await this.getRequest(
|
|
2148
|
+
`${this.apiUrl}/v1/team/token-usage`,
|
|
2149
|
+
headers
|
|
2150
|
+
);
|
|
2151
|
+
if (response.status === 200) {
|
|
2152
|
+
return response.data as TokenUsageResponseV1;
|
|
2153
|
+
} else {
|
|
2154
|
+
this.handleError(response, "get token usage");
|
|
2155
|
+
}
|
|
2156
|
+
} catch (error: any) {
|
|
2157
|
+
if (error.response?.data?.error) {
|
|
2158
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
|
|
2159
|
+
} else {
|
|
2160
|
+
throw new FirecrawlError(error.message, 500);
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
return { success: false, error: "Internal server error." };
|
|
2164
|
+
}
|
|
2165
|
+
|
|
2166
|
+
/**
|
|
2167
|
+
* Gets historical credit usage. Pass byApiKey=true to break down by API key.
|
|
2168
|
+
*/
|
|
2169
|
+
async getCreditUsageHistorical(byApiKey?: boolean): Promise<CreditUsageHistoricalResponseV1 | ErrorResponse> {
|
|
2170
|
+
const headers = this.prepareHeaders();
|
|
2171
|
+
try {
|
|
2172
|
+
const url = `${this.apiUrl}/v1/team/credit-usage/historical${byApiKey ? "?byApiKey=true" : ""}`;
|
|
2173
|
+
const response: AxiosResponse = await this.getRequest(url, headers);
|
|
2174
|
+
if (response.status === 200) {
|
|
2175
|
+
return response.data as CreditUsageHistoricalResponseV1;
|
|
2176
|
+
} else {
|
|
2177
|
+
this.handleError(response, "get credit usage historical");
|
|
2178
|
+
}
|
|
2179
|
+
} catch (error: any) {
|
|
2180
|
+
if (error.response?.data?.error) {
|
|
2181
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
|
|
2182
|
+
} else {
|
|
2183
|
+
throw new FirecrawlError(error.message, 500);
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
return { success: false, error: "Internal server error." };
|
|
2187
|
+
}
|
|
2188
|
+
|
|
2189
|
+
/**
|
|
2190
|
+
* Gets historical token usage. Pass byApiKey=true to break down by API key.
|
|
2191
|
+
*/
|
|
2192
|
+
async getTokenUsageHistorical(byApiKey?: boolean): Promise<TokenUsageHistoricalResponseV1 | ErrorResponse> {
|
|
2193
|
+
const headers = this.prepareHeaders();
|
|
2194
|
+
try {
|
|
2195
|
+
const url = `${this.apiUrl}/v1/team/token-usage/historical${byApiKey ? "?byApiKey=true" : ""}`;
|
|
2196
|
+
const response: AxiosResponse = await this.getRequest(url, headers);
|
|
2197
|
+
if (response.status === 200) {
|
|
2198
|
+
return response.data as TokenUsageHistoricalResponseV1;
|
|
2199
|
+
} else {
|
|
2200
|
+
this.handleError(response, "get token usage historical");
|
|
2201
|
+
}
|
|
2202
|
+
} catch (error: any) {
|
|
2203
|
+
if (error.response?.data?.error) {
|
|
2204
|
+
throw new FirecrawlError(`Request failed with status code ${error.response.status}. Error: ${error.response.data.error} ${error.response.data.details ? ` - ${JSON.stringify(error.response.data.details)}` : ''}`, error.response.status);
|
|
2205
|
+
} else {
|
|
2206
|
+
throw new FirecrawlError(error.message, 500);
|
|
2207
|
+
}
|
|
2208
|
+
}
|
|
2209
|
+
return { success: false, error: "Internal server error." };
|
|
2210
|
+
}
|
|
2073
2211
|
}
|
|
2074
2212
|
|
|
2075
2213
|
interface CrawlWatcherEvents {
|
package/src/v2/client.ts
CHANGED
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
batchScrape as batchWaiter,
|
|
20
20
|
} from "./methods/batch";
|
|
21
21
|
import { startExtract, getExtractStatus, extract as extractWaiter } from "./methods/extract";
|
|
22
|
-
import { getConcurrency, getCreditUsage, getQueueStatus, getTokenUsage } from "./methods/usage";
|
|
22
|
+
import { getConcurrency, getCreditUsage, getQueueStatus, getTokenUsage, getCreditUsageHistorical, getTokenUsageHistorical } from "./methods/usage";
|
|
23
23
|
import type {
|
|
24
24
|
Document,
|
|
25
25
|
ScrapeOptions,
|
|
@@ -269,6 +269,16 @@ export class FirecrawlClient {
|
|
|
269
269
|
return getTokenUsage(this.http);
|
|
270
270
|
}
|
|
271
271
|
|
|
272
|
+
/** Historical credit usage by month; set byApiKey to true to break down by API key. */
|
|
273
|
+
async getCreditUsageHistorical(byApiKey?: boolean) {
|
|
274
|
+
return getCreditUsageHistorical(this.http, byApiKey);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Historical token usage by month; set byApiKey to true to break down by API key. */
|
|
278
|
+
async getTokenUsageHistorical(byApiKey?: boolean) {
|
|
279
|
+
return getTokenUsageHistorical(this.http, byApiKey);
|
|
280
|
+
}
|
|
281
|
+
|
|
272
282
|
/** Metrics about the team's scrape queue. */
|
|
273
283
|
async getQueueStatus() {
|
|
274
284
|
return getQueueStatus(this.http);
|
package/src/v2/methods/usage.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ConcurrencyCheck, CreditUsage, QueueStatusResponse, TokenUsage } from "../types";
|
|
1
|
+
import type { ConcurrencyCheck, CreditUsage, QueueStatusResponse, TokenUsage, CreditUsageHistoricalResponse, TokenUsageHistoricalResponse } from "../types";
|
|
2
2
|
import { HttpClient } from "../utils/httpClient";
|
|
3
3
|
import { normalizeAxiosError, throwForBadResponse } from "../utils/errorHandler";
|
|
4
4
|
|
|
@@ -16,10 +16,15 @@ export async function getConcurrency(http: HttpClient): Promise<ConcurrencyCheck
|
|
|
16
16
|
|
|
17
17
|
export async function getCreditUsage(http: HttpClient): Promise<CreditUsage> {
|
|
18
18
|
try {
|
|
19
|
-
const res = await http.get<{ success: boolean; data?: { remainingCredits?: number; remaining_credits?: number } }>("/v2/team/credit-usage");
|
|
19
|
+
const res = await http.get<{ success: boolean; data?: { remainingCredits?: number; remaining_credits?: number; planCredits?: number; plan_credits?: number; billingPeriodStart?: string | null; billing_period_start?: string | null; billingPeriodEnd?: string | null; billing_period_end?: string | null } }>("/v2/team/credit-usage");
|
|
20
20
|
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get credit usage");
|
|
21
21
|
const d = res.data.data || (res.data as any);
|
|
22
|
-
return {
|
|
22
|
+
return {
|
|
23
|
+
remainingCredits: d.remainingCredits ?? d.remaining_credits ?? 0,
|
|
24
|
+
planCredits: d.planCredits ?? d.plan_credits,
|
|
25
|
+
billingPeriodStart: d.billingPeriodStart ?? d.billing_period_start ?? null,
|
|
26
|
+
billingPeriodEnd: d.billingPeriodEnd ?? d.billing_period_end ?? null,
|
|
27
|
+
};
|
|
23
28
|
} catch (err: any) {
|
|
24
29
|
if (err?.isAxiosError) return normalizeAxiosError(err, "get credit usage");
|
|
25
30
|
throw err;
|
|
@@ -28,9 +33,15 @@ export async function getCreditUsage(http: HttpClient): Promise<CreditUsage> {
|
|
|
28
33
|
|
|
29
34
|
export async function getTokenUsage(http: HttpClient): Promise<TokenUsage> {
|
|
30
35
|
try {
|
|
31
|
-
const res = await http.get<{ success: boolean; data?:
|
|
36
|
+
const res = await http.get<{ success: boolean; data?: { remainingTokens?: number; planTokens?: number; billingPeriodStart?: string | null; billingPeriodEnd?: string | null; remaining_tokens?: number; plan_tokens?: number; billing_period_start?: string | null; billing_period_end?: string | null } }>("/v2/team/token-usage");
|
|
32
37
|
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get token usage");
|
|
33
|
-
|
|
38
|
+
const d = res.data.data || (res.data as any);
|
|
39
|
+
return {
|
|
40
|
+
remainingTokens: d.remainingTokens ?? d.remaining_tokens ?? 0,
|
|
41
|
+
planTokens: d.planTokens ?? d.plan_tokens,
|
|
42
|
+
billingPeriodStart: d.billingPeriodStart ?? d.billing_period_start ?? null,
|
|
43
|
+
billingPeriodEnd: d.billingPeriodEnd ?? d.billing_period_end ?? null,
|
|
44
|
+
};
|
|
34
45
|
} catch (err: any) {
|
|
35
46
|
if (err?.isAxiosError) return normalizeAxiosError(err, "get token usage");
|
|
36
47
|
throw err;
|
|
@@ -47,3 +58,27 @@ export async function getQueueStatus(http: HttpClient): Promise<QueueStatusRespo
|
|
|
47
58
|
throw err;
|
|
48
59
|
}
|
|
49
60
|
}
|
|
61
|
+
|
|
62
|
+
export async function getCreditUsageHistorical(http: HttpClient, byApiKey?: boolean): Promise<CreditUsageHistoricalResponse> {
|
|
63
|
+
try {
|
|
64
|
+
const query = byApiKey ? "?byApiKey=true" : "";
|
|
65
|
+
const res = await http.get<CreditUsageHistoricalResponse>(`/v2/team/credit-usage/historical${query}`);
|
|
66
|
+
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get credit usage historical");
|
|
67
|
+
return res.data;
|
|
68
|
+
} catch (err: any) {
|
|
69
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "get credit usage historical");
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function getTokenUsageHistorical(http: HttpClient, byApiKey?: boolean): Promise<TokenUsageHistoricalResponse> {
|
|
75
|
+
try {
|
|
76
|
+
const query = byApiKey ? "?byApiKey=true" : "";
|
|
77
|
+
const res = await http.get<TokenUsageHistoricalResponse>(`/v2/team/token-usage/historical${query}`);
|
|
78
|
+
if (res.status !== 200 || !res.data?.success) throwForBadResponse(res, "get token usage historical");
|
|
79
|
+
return res.data;
|
|
80
|
+
} catch (err: any) {
|
|
81
|
+
if (err?.isAxiosError) return normalizeAxiosError(err, "get token usage historical");
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/v2/types.ts
CHANGED
|
@@ -339,10 +339,40 @@ export interface ConcurrencyCheck {
|
|
|
339
339
|
|
|
340
340
|
export interface CreditUsage {
|
|
341
341
|
remainingCredits: number;
|
|
342
|
+
planCredits?: number;
|
|
343
|
+
billingPeriodStart?: string | null;
|
|
344
|
+
billingPeriodEnd?: string | null;
|
|
342
345
|
}
|
|
343
346
|
|
|
344
347
|
export interface TokenUsage {
|
|
345
348
|
remainingTokens: number;
|
|
349
|
+
planTokens?: number;
|
|
350
|
+
billingPeriodStart?: string | null;
|
|
351
|
+
billingPeriodEnd?: string | null;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export interface CreditUsageHistoricalPeriod {
|
|
355
|
+
startDate: string | null;
|
|
356
|
+
endDate: string | null;
|
|
357
|
+
apiKey?: string;
|
|
358
|
+
creditsUsed: number;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export interface CreditUsageHistoricalResponse {
|
|
362
|
+
success: boolean;
|
|
363
|
+
periods: CreditUsageHistoricalPeriod[];
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export interface TokenUsageHistoricalPeriod {
|
|
367
|
+
startDate: string | null;
|
|
368
|
+
endDate: string | null;
|
|
369
|
+
apiKey?: string;
|
|
370
|
+
tokensUsed: number;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export interface TokenUsageHistoricalResponse {
|
|
374
|
+
success: boolean;
|
|
375
|
+
periods: TokenUsageHistoricalPeriod[];
|
|
346
376
|
}
|
|
347
377
|
|
|
348
378
|
export interface CrawlErrorsResponse {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type FormatOption, type JsonFormat, type ScrapeOptions, type ScreenshotFormat } from "../types";
|
|
2
|
-
import zodToJsonSchema from "zod-to-json-schema";
|
|
1
|
+
import { type FormatOption, type JsonFormat, type ScrapeOptions, type ScreenshotFormat, type ChangeTrackingFormat } from "../types";
|
|
2
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
3
3
|
|
|
4
4
|
export function ensureValidFormats(formats?: FormatOption[]): void {
|
|
5
5
|
if (!formats) return;
|
|
@@ -27,6 +27,19 @@ export function ensureValidFormats(formats?: FormatOption[]): void {
|
|
|
27
27
|
}
|
|
28
28
|
continue;
|
|
29
29
|
}
|
|
30
|
+
if ((fmt as ChangeTrackingFormat).type === "changeTracking") {
|
|
31
|
+
const ct = fmt as ChangeTrackingFormat;
|
|
32
|
+
const maybeSchema: any = ct.schema as any;
|
|
33
|
+
const isZod = !!maybeSchema && (typeof maybeSchema.safeParse === "function" || typeof maybeSchema.parse === "function") && !!maybeSchema._def;
|
|
34
|
+
if (isZod) {
|
|
35
|
+
try {
|
|
36
|
+
(ct as any).schema = zodToJsonSchema(maybeSchema);
|
|
37
|
+
} catch {
|
|
38
|
+
// Best-effort conversion; if it fails, leave original value
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
30
43
|
if ((fmt as ScreenshotFormat).type === "screenshot") {
|
|
31
44
|
// no-op; already camelCase; validate numeric fields if present
|
|
32
45
|
const s = fmt as ScreenshotFormat;
|