{"record":{"id":"94e2fb8341450fad","repo":"vercel/ai","slug":"description-api-key-is-missing-pass-it-using-t-94e2fb","errorCode":null,"errorMessage":"${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables are not supported in this environment.","messagePattern":"(.+?) API key is missing\\. Pass it using the '(.+?)' parameter\\. Environment variables are not supported in this environment\\.","errorType":"exception","errorClass":"LoadAPIKeyError","httpStatus":null,"severity":"error","filePath":"packages/provider-utils/src/load-api-key.ts","lineNumber":25,"sourceCode":"  description,\n}: {\n  apiKey: string | undefined;\n  environmentVariableName: string;\n  apiKeyParameterName?: string;\n  description: string;\n}): string {\n  if (typeof apiKey === 'string') {\n    return apiKey;\n  }\n\n  if (apiKey != null) {\n    throw new LoadAPIKeyError({\n      message: `${description} API key must be a string.`,\n    });\n  }\n\n  if (typeof process === 'undefined') {\n    throw new LoadAPIKeyError({\n      message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables are not supported in this environment.`,\n    });\n  }\n\n  apiKey = process.env[environmentVariableName];\n\n  if (apiKey == null) {\n    throw new LoadAPIKeyError({\n      message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.`,\n    });\n  }\n\n  if (typeof apiKey !== 'string') {\n    throw new LoadAPIKeyError({\n      message: `${description} API key must be a string. The value of the ${environmentVariableName} environment variable is not a string.`,\n    });\n  }\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/provider-utils/src/load-api-key.ts#L7-L43","documentation":"loadApiKey could not find the API key: no apiKey parameter was provided and `process` is undefined (edge/browser/serverless runtimes without Node's process global), so environment variables cannot be consulted. It throws LoadAPIKeyError telling you to pass the key explicitly via the named parameter.","triggerScenarios":"Creating a provider instance (e.g. createOpenAI()) with no apiKey in an environment where `typeof process === 'undefined'` — such as Edge Runtime, Vercel Edge Functions configured without Node compat, browsers, or workers — and no key was passed in options.","commonSituations":"Deploying to Edge runtime after developing locally with env vars; forgetting that .env loading doesn't exist in the browser; Cloudflare Workers without env binding wired to the provider factory.","solutions":["Pass the key explicitly: createOpenAI({ apiKey: env.MY_API_KEY })","In Next.js, expose the variable server-side and pass it in a route handler/server action instead of relying on process.env in edge/browser code","Enable Node.js runtime/compat if the deployment platform supports it","Ensure the value you read from the platform's env bindings (e.g. worker env object) is actually forwarded to the factory"],"exampleFix":"// before (edge runtime)\nconst openai = createOpenAI();\n// after\nconst openai = createOpenAI({ apiKey: env.OPENAI_API_KEY });","handlingStrategy":"validation","validationCode":"if (typeof process === 'undefined') {\n  if (!env.OPENAI_API_KEY) throw new Error('Pass apiKey explicitly: process.env is unavailable in this runtime');\n}\nconst provider = createOpenAI({ apiKey: env.OPENAI_API_KEY });","typeGuard":"function canUseProcessEnv(): boolean {\n  return typeof process !== 'undefined' && typeof process.env === 'object';\n}","tryCatchPattern":"try {\n  const provider = createOpenAI();\n} catch (error) {\n  if (/API key is missing/.test(String(error.message))) {\n    throw new Error('Set OPENAI_API_KEY or pass createOpenAI({ apiKey }) — env vars unsupported here');\n  }\n  throw error;\n}","preventionTips":["In edge/browser code, always pass the key explicitly from platform env bindings","Never rely on process.env in Edge Runtime, Workers, or client bundles","Fail fast at boot with your own env check for required keys"],"tags":["api-key","environment","edge-runtime","configuration"],"backgroundTag":"missing-env-var","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}