{"record":{"id":"fa2030d8efac9693","repo":"DayuanJiang/next-ai-draw-io","slug":"either-anthropic-api-key-or-anthropic-auth-token-e","errorCode":null,"errorMessage":"Either ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN environment variable is required for anthropic provider. Please set one in your .env.local file.","messagePattern":"Either ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN environment variable is required for anthropic provider\\. Please set one in your \\.env\\.local file\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"lib/ai-providers.ts","lineNumber":657,"sourceCode":"    // Handle array of env var names - at least one must be set\n    if (Array.isArray(customApiKeyEnv)) {\n        const hasAnyKey = customApiKeyEnv.some((envVar) => process.env[envVar])\n        if (!hasAnyKey) {\n            throw new Error(\n                `At least one of [${customApiKeyEnv.join(\", \")}] environment variables is required for ${provider} provider. ` +\n                    `Please set at least one in your .env.local file.`,\n            )\n        }\n        return\n    }\n\n    // Anthropic accepts ANTHROPIC_AUTH_TOKEN (Bearer auth) as alternative to ANTHROPIC_API_KEY\n    if (provider === \"anthropic\" && !customApiKeyEnv) {\n        const hasCredential = !!(\n            process.env.ANTHROPIC_API_KEY || process.env.ANTHROPIC_AUTH_TOKEN\n        )\n        if (!hasCredential) {\n            throw new Error(\n                `Either ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN environment variable is required for anthropic provider. ` +\n                    `Please set one in your .env.local file.`,\n            )\n        }\n    } else {\n        // Use custom env var name if provided, otherwise use default\n        const requiredVar = customApiKeyEnv || PROVIDER_ENV_VARS[provider]\n        if (requiredVar && !process.env[requiredVar]) {\n            throw new Error(\n                `${requiredVar} environment variable is required for ${provider} provider. ` +\n                    `Please set it in your .env.local file.`,\n            )\n        }\n    }\n\n    // Azure requires either AZURE_BASE_URL or AZURE_RESOURCE_NAME in addition to API key\n    if (provider === \"azure\") {\n        const hasBaseUrl = !!process.env.AZURE_BASE_URL","sourceCodeStart":639,"sourceCodeEnd":675,"githubUrl":"https://github.com/DayuanJiang/next-ai-draw-io/blob/155ef4f7acd29c9d46fb6fc35c92e6e6955a6ce1/lib/ai-providers.ts#L639-L675","documentation":"For the anthropic provider, either ANTHROPIC_API_KEY (x-api-key) or ANTHROPIC_AUTH_TOKEN (Bearer) must be set; absent both, getAIModel throws at validation time.","triggerScenarios":"Selecting/configuring provider anthropic with neither env var set in .env.local or the process environment.","commonSituations":"Fresh setup without .env.local, CI/production missing secrets, or a typo such as ANTHROPIC_APIKEY.","solutions":["Set ANTHROPIC_API_KEY=sk-ant-... (or ANTHROPIC_AUTH_TOKEN for bearer auth) in .env.local","Restart the server after adding the variable","Verify the variable name spelling exactly matches"],"exampleFix":"# before\n# .env.local\nAI_PROVIDER=anthropic\n\n# after\n# .env.local\nAI_PROVIDER=anthropic\nANTHROPIC_API_KEY=sk-ant-...","handlingStrategy":"validation","validationCode":"const hasAnthropicCred = !!(process.env.ANTHROPIC_API_KEY || process.env.ANTHROPIC_AUTH_TOKEN)\nif (provider === 'anthropic' && !hasAnthropicCred) failFast('Set ANTHROPIC_API_KEY')","typeGuard":null,"tryCatchPattern":"try { getAIModel() } catch (e) { if ((e as Error).message.includes('ANTHROPIC_API_KEY')) promptApiKeySetup('anthropic') }","preventionTips":["Keep .env.example listing both accepted Anthropic vars","Add a preflight env check on app startup","Use ANTHROPIC_AUTH_TOKEN only for bearer-gateway setups"],"tags":["env-var","anthropic","credentials","configuration"],"backgroundTag":"missing-api-key","analyzedSha":"155ef4f7acd29c9d46fb6fc35c92e6e6955a6ce1","analyzedAt":"2026-08-27T11:40:38.297Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}