Skip to content

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

fix(sdk): trim whitespace from API key to reject whitespace-only strings

Himadri M6eefd0f8225d
diff --git a/apps/js-sdk/firecrawl/src/__tests__/unit/v2/clientOptions.test.ts b/apps/js-sdk/firecrawl/src/__tests__/unit/v2/clientOptions.test.ts
index 16441a736..770e9df62 100644
--- a/apps/js-sdk/firecrawl/src/__tests__/unit/v2/clientOptions.test.ts
+++ b/apps/js-sdk/firecrawl/src/__tests__/unit/v2/clientOptions.test.ts
@@ -71,6 +71,10 @@ describe('Firecrawl v2 Client Options', () => {
expect(() => new Firecrawl('')).toThrow('API key is required');
});
+ it('should throw for whitespace-only string API key on cloud', () => {
+ expect(() => new Firecrawl(' ')).toThrow('API key is required');
+ });
+
it('should provide v1 accessor when constructed with string', () => {
const client = new Firecrawl('test-key');
diff --git a/apps/js-sdk/firecrawl/src/v2/client.ts b/apps/js-sdk/firecrawl/src/v2/client.ts
index 96b8db9cf..ecb64cf99 100644
--- a/apps/js-sdk/firecrawl/src/v2/client.ts
+++ b/apps/js-sdk/firecrawl/src/v2/client.ts
@@ -132,7 +132,7 @@ export class FirecrawlClient {
const opts: FirecrawlClientOptions =
typeof options === "string" ? { apiKey: options } : options;
- const apiKey = opts.apiKey ?? process.env.FIRECRAWL_API_KEY ?? "";
+ const apiKey = (opts.apiKey ?? process.env.FIRECRAWL_API_KEY ?? "").trim();
const apiUrl = (opts.apiUrl ?? process.env.FIRECRAWL_API_URL ?? "https://api.firecrawl.dev").replace(/\/$/, "");
if (this.isCloudService(apiUrl) && !apiKey) {