{"record":{"id":"1a1bc698f7312242","repo":"mastra-ai/mastra","slug":"need-moonshot-api-key","errorCode":null,"errorMessage":"Need MOONSHOT_API_KEY","messagePattern":"Need MOONSHOT_API_KEY","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/agents/mastracode-gateway.ts","lineNumber":490,"sourceCode":"      });\n      return provider.chatModel(args.modelId) as unknown as GatewayLanguageModel;\n    }\n\n    if (args.providerId === 'github-copilot') {\n      return githubCopilotProvider(args.modelId, {\n        headers: args.headers,\n        authStorage: this.#credentials,\n      }) as unknown as GatewayLanguageModel;\n    }\n\n    if (args.providerId === 'moonshotai') {\n      // Prefer the gateway-resolved key (stored credentials), then the canonical\n      // registry env var. Keep the legacy typo name as a fallback for anyone who\n      // set it because of the previous error message.\n      const apiKey =\n        args.apiKey?.trim() || process.env.MOONSHOT_API_KEY?.trim() || process.env.MOONSHOT_AI_API_KEY?.trim();\n      if (!apiKey) {\n        throw new Error('Need MOONSHOT_API_KEY');\n      }\n      return createAnthropic({\n        apiKey,\n        baseURL: 'https://api.moonshot.ai/anthropic/v1',\n        name: 'moonshotai.anthropicv1',\n        headers: args.headers,\n      })(args.modelId) as unknown as GatewayLanguageModel;\n    }\n\n    if (args.providerId === 'anthropic') {\n      return this.#resolveAnthropicModel(args);\n    }\n\n    if (args.providerId === 'openai') {\n      const openaiModel = this.#resolveOpenAIModel(args);\n      if (openaiModel) return openaiModel;\n    }\n","sourceCodeStart":472,"sourceCodeEnd":508,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/agents/mastracode-gateway.ts#L472-L508","documentation":"resolveLanguageModel for Moonshot (Anthropic-compatible endpoint) requires an API key and throws 'Need MOONSHOT_API_KEY' when none is found. It checks args.apiKey, then MOONSHOT_API_KEY, then the legacy typo MOONSHOT_AI_API_KEY.","triggerScenarios":"A Moonshot gateway model is resolved with `args.apiKey` empty/whitespace and neither MOONSHOT_API_KEY nor MOONSHOT_AI_API_KEY is set in the environment.","commonSituations":"Deploying without the env var configured, key set under a differently named variable, running in an environment where .env is not loaded, or relying on the old typo'd MOONSHOT_AI_API_KEY in a context where it was dropped.","solutions":["Set MOONSHOT_API_KEY in the environment with a valid Moonshot API key","Pass the key explicitly via args.apiKey when constructing/resolving the model","Ensure your .env file is actually loaded by the process","Rename legacy MOONSHOT_AI_API_KEY to MOONSHOT_API_KEY to standardize","Verify the key works: `curl https://api.moonshot.ai/...` with the key"],"exampleFix":"// before\nconst model = gatewayModel('moonshotai/kimi-k2'); // no key anywhere\n// after\nprocess.env.MOONSHOT_API_KEY = 'sk-...';\nconst model = gatewayModel('moonshotai/kimi-k2');","handlingStrategy":"validation","validationCode":"const key = process.env.MOONSHOT_API_KEY?.trim();\nif (!key) throw new Error('Set MOONSHOT_API_KEY before resolving moonshot models');","typeGuard":"const hasMoonshotKey = (): boolean =>\n  Boolean(process.env.MOONSHOT_API_KEY?.trim() || process.env.MOONSHOT_AI_API_KEY?.trim());","tryCatchPattern":"try {\n  const model = resolveModel('moonshotai/kimi-k2');\n} catch (err) {\n  if ((err as Error).message === 'Need MOONSHOT_API_KEY') {\n    console.error('Set MOONSHOT_API_KEY in your environment or pass apiKey explicitly');\n  } else throw err;\n}","preventionTips":["Set MOONSHOT_API_KEY in .env and ensure it is loaded","Pass apiKey explicitly in deployment configs rather than relying on ambient env","Audit env var names when upgrading (legacy typo name may be removed)"],"tags":["configuration","env-var","api-key","authentication"],"backgroundTag":"missing-env-var","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}