vercel/ai · error

${description} API key is missing. Pass it using the 'apiKey

Error message

${description} API key is missing. Pass it using the 'apiKey' parameter. Environment variables are not supported in this environment.

What it means

loadFalApiKey throws this when no apiKey parameter was given and `typeof process === 'undefined'`, meaning environment variables are unavailable (edge runtimes, browsers). The fal provider cannot resolve a key in such environments, so it fails fast with a targeted message.

Source

Thrown at packages/fal/src/fal-provider.ts:104

const defaultBaseURL = 'https://fal.run';

function loadFalApiKey({
  apiKey,
  description = 'fal.ai',
}: {
  apiKey: string | undefined;
  description?: string;
}): string {
  if (typeof apiKey === 'string') {
    return apiKey;
  }

  if (apiKey != null) {
    throw new Error(`${description} API key must be a string.`);
  }

  if (typeof process === 'undefined') {
    throw new Error(
      `${description} API key is missing. Pass it using the 'apiKey' parameter. Environment variables are not supported in this environment.`,
    );
  }

  let envApiKey = process.env.FAL_API_KEY;
  if (envApiKey == null) {
    envApiKey = process.env.FAL_KEY;
  }

  if (envApiKey == null) {
    throw new Error(
      `${description} API key is missing. Pass it using the 'apiKey' parameter or set either the FAL_API_KEY or FAL_KEY environment variable.`,
    );
  }

  if (typeof envApiKey !== 'string') {
    throw new Error(
      `${description} API key must be a string. The value of the environment variable is not a string.`,

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Pass the key explicitly: createFal({ apiKey: 'your-fal-key' }).
  2. In edge runtimes, read your platform env binding and pass it as apiKey.
  3. Ensure the bundler polyfills `process.env` if you must rely on env vars.

Example fix

// before (edge runtime)
const fal = createFal();
// after
const fal = createFal({ apiKey: env.FAL_KEY });
Defensive patterns

Strategy: validation

Validate before calling

function resolveFalKey(envBinding?: string): string {
  const key = envBinding ?? (typeof process !== 'undefined' ? process.env.FAL_API_KEY ?? process.env.FAL_KEY : undefined);
  if (!key) throw new Error('fal API key required: pass apiKey or set FAL_API_KEY/FAL_KEY');
  return key;
}
const fal = createFal({ apiKey: resolveFalKey(edgeEnv.FAL_KEY) });

Try / catch

let fal;
try {
  fal = createFal();
} catch (e) {
  if ((e as Error).message.includes('API key is missing')) {
    fal = createFal({ apiKey: runtimeEnv.FAL_KEY });
  } else throw e;
}

Prevention

When it happens

Trigger: Calling createFal() with no apiKey in an edge runtime (Vercel Edge, Cloudflare Workers) or browser bundle where `process` is not defined.

Common situations: Deploying fal-based routes to edge functions without supplying the key via parameter or runtime env binding.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/d93e6acb5a431306. Report an issue: GitHub.