{"record":{"id":"64dd6b8706b99fec","repo":"vercel/ai","slug":"description-api-key-is-missing-pass-it-using-t-64dd6b","errorCode":null,"errorMessage":"${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or the ${environmentVariableName} environment variable.","messagePattern":"(.+?) API key is missing\\. Pass it using the '(.+?)' parameter or the (.+?) environment variable\\.","errorType":"exception","errorClass":"LoadAPIKeyError","httpStatus":null,"severity":"error","filePath":"packages/provider-utils/src/load-api-key.ts","lineNumber":33,"sourceCode":"    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\n  return apiKey;\n}\n","sourceCodeStart":15,"sourceCodeEnd":46,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/provider-utils/src/load-api-key.ts#L15-L46","documentation":"loadApiKey found neither an apiKey parameter nor the configured environment variable (both null/undefined), so it throws LoadAPIKeyError naming the parameter and the expected environment variable. This is the standard 'credentials not configured' failure.","triggerScenarios":"Calling a provider factory without an apiKey option while process.env[environmentVariableName] (e.g. OPENAI_API_KEY) is unset at call time.","commonSituations":".env file not loaded (missing dotenv / Next.js env naming mismatch like NEXT_PUBLIC vs server-only); env var set in a different shell/session; CI pipeline missing the secret; variable defined but empty string treated as missing in some flows; deploying without configuring platform env vars.","solutions":["Set the indicated environment variable (e.g. export OPENAI_API_KEY=sk-...) or add it to .env","Or pass the key explicitly: createOpenAI({ apiKey: 'sk-...' })","Verify the exact variable name and casing the provider expects","In CI/deployment, add the secret to the platform's environment configuration and redeploy"],"exampleFix":"// before\nconst anthropic = createAnthropic();\n// after\nconst anthropic = createAnthropic({ apiKey: process.env.ANTHROPIC_API_KEY });","handlingStrategy":"validation","validationCode":"const apiKey = process.env.OPENAI_API_KEY;\nif (!apiKey) throw new Error('OPENAI_API_KEY is not set. Add it to .env or export it before starting.');\nconst provider = createOpenAI({ apiKey });","typeGuard":"function hasApiKey(value: unknown): value is string {\n  return typeof value === 'string' && value.length > 0;\n}","tryCatchPattern":"try {\n  const provider = createOpenAI();\n} catch (error) {\n  if (/API key is missing/.test(String(error.message))) {\n    console.error('Set OPENAI_API_KEY in your environment or pass it via createOpenAI({ apiKey })');\n    process.exit(1);\n  }\n  throw error;\n}","preventionTips":["Add a startup check that all required provider env vars are set","Keep .env files loaded (dotenv / framework auto-loading) and committed as .env.example","Match env var names exactly (including provider-specific prefixes like NEXT_PUBLIC_)","Configure secrets in CI and deployment platforms before first deploy"],"tags":["api-key","environment","configuration"],"backgroundTag":"missing-env-var","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}