{"record":{"id":"6592268ecf8a1e16","repo":"mastra-ai/mastra","slug":"mastra-gateway-no-api-key","errorCode":"MASTRA_GATEWAY_NO_API_KEY","errorMessage":"Missing MASTRA_GATEWAY_API_KEY environment variable","messagePattern":"Missing MASTRA_GATEWAY_API_KEY environment variable","errorType":"error_code","errorClass":"MastraError","httpStatus":null,"severity":"error","filePath":"packages/core/src/llm/model/gateways/mastra.ts","lineNumber":77,"sourceCode":"        apiKeyHeader: 'Authorization',\n        name: 'Gateway',\n        gateway: 'mastra',\n        models: [...models],\n        docUrl: 'https://mastra.ai/docs/gateway',\n      },\n    };\n\n    return providers;\n  }\n\n  async buildUrl(_modelId: string): Promise<string> {\n    return `${this.getBaseUrl()}/v1`;\n  }\n\n  async getApiKey(): Promise<string> {\n    const apiKey = this.config?.apiKey ?? process.env['MASTRA_GATEWAY_API_KEY'];\n    if (!apiKey) {\n      throw new MastraError({\n        id: 'MASTRA_GATEWAY_NO_API_KEY',\n        domain: 'LLM',\n        category: 'UNKNOWN',\n        text: 'Missing MASTRA_GATEWAY_API_KEY environment variable',\n      });\n    }\n    return apiKey;\n  }\n\n  resolveLanguageModel({\n    modelId,\n    providerId,\n    apiKey,\n    headers,\n  }: {\n    modelId: string;\n    providerId: string;\n    apiKey: string;","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/llm/model/gateways/mastra.ts#L59-L95","documentation":"The Mastra gateway requires an API key to authenticate requests, and none was found. getApiKey checks config.apiKey first, then the MASTRA_GATEWAY_API_KEY environment variable; if both are empty the gateway cannot authenticate and throws. This MastraError indicates missing credentials configuration.","triggerScenarios":"Calling getApiKey() (during model resolution/requests) when the MastraGateway was constructed without config.apiKey and process.env.MASTRA_GATEWAY_API_KEY is unset.","commonSituations":"Deploying to an environment where the env var wasn't set; .env file not loaded; variable renamed or typo'd; constructing gateway with an empty config object.","solutions":["Set MASTRA_GATEWAY_API_KEY in the environment before making requests.","Pass the key explicitly: new MastraGateway({ apiKey: process.env.MASTRA_GATEWAY_API_KEY }).","Verify .env loading (dotenv/Next.js env handling) and variable spelling in your deployment config.","Add a startup validation that fails fast if the env var is missing."],"exampleFix":"// before\nconst gw = new MastraGateway({}); // no key anywhere\n// after\nconst gw = new MastraGateway({ apiKey: process.env.MASTRA_GATEWAY_API_KEY });\n// and/or in shell: export MASTRA_GATEWAY_API_KEY=...","handlingStrategy":"validation","validationCode":"if (!process.env.MASTRA_GATEWAY_API_KEY) {\n  throw new Error('MASTRA_GATEWAY_API_KEY must be set before creating/using the Mastra gateway');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set MASTRA_GATEWAY_API_KEY in all environments (CI, prod, local .env).","Fail fast at app startup if the variable is missing.","Prefer explicit config.apiKey over env lookup when constructing the gateway.","Verify secret loading in your deployment platform (env files, secret managers)."],"tags":["api-key","environment","configuration","gateway"],"backgroundTag":"missing-env-var","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}