DayuanJiang/next-ai-draw-io · critical · Error
Either ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN environment
Error message
Either ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN environment variable is required for anthropic provider. Please set one in your .env.local file.
What it means
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.
Source
Thrown at lib/ai-providers.ts:657
// Handle array of env var names - at least one must be set
if (Array.isArray(customApiKeyEnv)) {
const hasAnyKey = customApiKeyEnv.some((envVar) => process.env[envVar])
if (!hasAnyKey) {
throw new Error(
`At least one of [${customApiKeyEnv.join(", ")}] environment variables is required for ${provider} provider. ` +
`Please set at least one in your .env.local file.`,
)
}
return
}
// Anthropic accepts ANTHROPIC_AUTH_TOKEN (Bearer auth) as alternative to ANTHROPIC_API_KEY
if (provider === "anthropic" && !customApiKeyEnv) {
const hasCredential = !!(
process.env.ANTHROPIC_API_KEY || process.env.ANTHROPIC_AUTH_TOKEN
)
if (!hasCredential) {
throw new Error(
`Either ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN environment variable is required for anthropic provider. ` +
`Please set one in your .env.local file.`,
)
}
} else {
// Use custom env var name if provided, otherwise use default
const requiredVar = customApiKeyEnv || PROVIDER_ENV_VARS[provider]
if (requiredVar && !process.env[requiredVar]) {
throw new Error(
`${requiredVar} environment variable is required for ${provider} provider. ` +
`Please set it in your .env.local file.`,
)
}
}
// Azure requires either AZURE_BASE_URL or AZURE_RESOURCE_NAME in addition to API key
if (provider === "azure") {
const hasBaseUrl = !!process.env.AZURE_BASE_URLView on GitHub (pinned to 155ef4f7ac)
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
Example fix
# before # .env.local AI_PROVIDER=anthropic # after # .env.local AI_PROVIDER=anthropic ANTHROPIC_API_KEY=sk-ant-...
Defensive patterns
Strategy: validation
Validate before calling
const hasAnthropicCred = !!(process.env.ANTHROPIC_API_KEY || process.env.ANTHROPIC_AUTH_TOKEN)
if (provider === 'anthropic' && !hasAnthropicCred) failFast('Set ANTHROPIC_API_KEY') Try / catch
try { getAIModel() } catch (e) { if ((e as Error).message.includes('ANTHROPIC_API_KEY')) promptApiKeySetup('anthropic') } Prevention
- 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
When it happens
Trigger: Selecting/configuring provider anthropic with neither env var set in .env.local or the process environment.
Common situations: Fresh setup without .env.local, CI/production missing secrets, or a typo such as ANTHROPIC_APIKEY.
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
- At least one of [${customApiKeyEnv.join(", ")}] environment
- ${requiredVar} environment variable is required for ${provid
- Azure requires either AZURE_BASE_URL or AZURE_RESOURCE_NAME
- ${varName} must be a valid integer, got: ${value}
- ${varName} must be >= ${min}, got: ${parsed}
AI-assisted analysis of DayuanJiang/next-ai-draw-io@155ef4f7ac (2026-08-27).
Data as JSON: /api/errors/fa2030d8efac9693.
Report an issue: GitHub.