Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(js-sdk): make v2 requests work on Cloudflare Workers (#5243)

Erik Engervall086cf2c63647
diff --git a/apps/js-sdk/firecrawl/src/v2/utils/httpClient.ts b/apps/js-sdk/firecrawl/src/v2/utils/httpClient.ts
index 495565e85..21602bd38 100644
--- a/apps/js-sdk/firecrawl/src/v2/utils/httpClient.ts
+++ b/apps/js-sdk/firecrawl/src/v2/utils/httpClient.ts
@@ -6,6 +6,18 @@ import axios, {
import { getVersion } from "./getVersion";
import { pinToApiOrigin } from "../../utils/apiOrigin";
+// Runtimes such as Cloudflare Workers reject the Request `cache: "default"` that
+// axios's fetch adapter always sets; there axios calls fetch(url, init) without it.
+const rejectsDefaultCacheMode = (): boolean => {
+ if (typeof Request !== "function") return false;
+ try {
+ new Request("https://api.firecrawl.dev", { cache: "default" });
+ return false;
+ } catch {
+ return true;
+ }
+};
+
export interface HttpClientOptions {
apiKey: string;
apiUrl: string;
@@ -44,6 +56,7 @@ export class HttpClient {
...(this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {}),
},
transitional: { clarifyTimeoutError: true },
+ ...(rejectsDefaultCacheMode() ? { adapter: "fetch", env: { Request: null as any } } : {}),
});
}