mastra-ai/mastra · error

FirecrawlBrowser requires `apiKey` or FIRECRAWL_API_KEY

Error message

FirecrawlBrowser requires `apiKey` or FIRECRAWL_API_KEY

What it means

FirecrawlBrowser validates configuration at construction time: an API key must come either from the explicit `apiKey` config property or from the FIRECRAWL_API_KEY environment variable. The library throws immediately in the constructor rather than later at request time, so a missing key fails fast. This guards against constructing a browser that cannot authenticate any Firecrawl calls.

Source

Thrown at browser/firecrawl/src/firecrawl-browser.ts:38

/**
 * Mastra browser provider backed by [Firecrawl Browser Sandbox](https://docs.firecrawl.dev/features/browser):
 * provisions remote sessions via API and drives them with the same deterministic tools as {@link AgentBrowser}.
 */
export class FirecrawlBrowser extends AgentBrowser {
  override readonly name = 'FirecrawlBrowser';
  override readonly provider = 'firecrawl/browser-sandbox';

  /** Narrowed from base `MastraBrowser` (`unknown`) — same pattern as {@link AgentBrowser}. */
  declare protected sharedManager: BrowserManager | null;

  private readonly firecrawl: Firecrawl;
  private readonly sessionOpts: FirecrawlBrowserSessionOptions;
  private sharedFirecrawlSessionId?: string;

  constructor(config: FirecrawlBrowserConfig) {
    const apiKey = config.apiKey ?? process.env.FIRECRAWL_API_KEY;
    if (!apiKey) {
      throw new Error('FirecrawlBrowser requires `apiKey` or FIRECRAWL_API_KEY');
    }
    const fc = new Firecrawl({ apiKey, apiUrl: config.apiUrl });
    const sessionOpts = pickSessionOpts(config);

    super({
      ...toBaseConfig(config),
      createThreadManager: opts =>
        new FirecrawlAgentBrowserThreadManager({
          ...opts,
          firecrawl: fc,
          resolveWebSocketUrl: url => resolveCdpWebSocketUrl(url, opts.logger),
          sessionOptions: sessionOpts,
        }),
    });
    this.firecrawl = fc;
    this.sessionOpts = sessionOpts;
  }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Set the FIRECRAWL_API_KEY environment variable to a valid Firecrawl API key
  2. Pass apiKey explicitly in the FirecrawlBrowser config object
  3. Verify the env var is loaded (dotenv/config, Docker -e, CI secrets) before constructing
  4. Check that the value is non-empty and from the correct Firecrawl account

Example fix

// before
const browser = new FirecrawlBrowser({});
// after
const browser = new FirecrawlBrowser({ apiKey: process.env.FIRECRAWL_API_KEY });
Defensive patterns

Strategy: validation

Validate before calling

const apiKey = config.apiKey ?? process.env.FIRECRAWL_API_KEY;
if (!apiKey) throw new Error('Set FIRECRAWL_API_KEY or pass apiKey to FirecrawlBrowser');

Type guard

function hasFirecrawlKey(config: { apiKey?: string }): config is { apiKey: string } {
  return Boolean(config.apiKey ?? process.env.FIRECRAWL_API_KEY);
}

Try / catch

try {
  const browser = new FirecrawlBrowser(config);
} catch (e) {
  if (e instanceof Error && e.message.includes('apiKey')) {
    throw new Error('Firecrawl configuration error: missing apiKey / FIRECRAWL_API_KEY');
  }
  throw e;
}

Prevention

When it happens

Trigger: Constructing FirecrawlBrowser with a config object lacking `apiKey` while the FIRECRAWL_API_KEY environment variable is unset (or set to an empty string, which is falsy).

Common situations: Deploying to an environment where the env var was never set (CI, serverless, Docker without env passthrough); passing an empty-string apiKey; loading .env only in dev but not production; forgetting the config spread when wiring the browser into an agent.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/b18694c20997d88a. Report an issue: GitHub.