{"record":{"id":"007c5c0286eadb40","repo":"mastra-ai/mastra","slug":"mastra-org-id-and-mastra-project-id-are-required-w-007c5c","errorCode":null,"errorMessage":"MASTRA_ORG_ID and MASTRA_PROJECT_ID are required when MASTRA_API_TOKEN is set","messagePattern":"MASTRA_ORG_ID and MASTRA_PROJECT_ID are required when MASTRA_API_TOKEN is set","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/cli/src/commands/studio/deploy.ts","lineNumber":410,"sourceCode":"      hasEnvFile: Boolean(opts.envFile),\n      hasConfig: Boolean(opts.config),\n      debug: Boolean(opts.debug),\n      headless: Boolean(process.env.MASTRA_API_TOKEN),\n      targetApi: bucketApiHost(MASTRA_PLATFORM_API_URL),\n    },\n    execution: () => runStudioDeploy(dir, opts),\n    origin: process.env.MASTRA_ANALYTICS_ORIGIN as CLI_ORIGIN | undefined,\n  });\n}\n\nasync function runStudioDeploy(dir: string | undefined, opts: StudioDeployOptions) {\n  const targetDir = resolve(dir || process.cwd());\n  // Seed MASTRA_PROJECT_ID / MASTRA_ORG_ID from the project's .env so deploys\n  // auto-link to the project that `mastra init --observability` provisioned.\n  loadDeployEnvFromDotenv(targetDir);\n  const isHeadless = Boolean(process.env.MASTRA_API_TOKEN);\n  if (isHeadless && (!process.env.MASTRA_ORG_ID || !process.env.MASTRA_PROJECT_ID)) {\n    throw new Error('MASTRA_ORG_ID and MASTRA_PROJECT_ID are required when MASTRA_API_TOKEN is set');\n  }\n  const autoAccept = opts.yes ?? isHeadless;\n  const skipPreflight = opts.skipPreflight || process.env.MASTRA_SKIP_PREFLIGHT === '1';\n\n  p.intro('mastra studio deploy');\n\n  // Gather context\n  const packageName = getPackageName(targetDir);\n  const gitBranch = getGitBranch(targetDir);\n  const mastraVersion = getMastraVersion(targetDir);\n\n  // Step 1: Auth\n  const token = await getToken();\n\n  // Step 2: Load existing project config\n  const projectConfig = await loadProjectConfig(targetDir, opts.config);\n\n  // Step 3: Resolve org","sourceCodeStart":392,"sourceCodeEnd":428,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/studio/deploy.ts#L392-L428","documentation":"When MASTRA_API_TOKEN is present the deploy runs headless (no interactive prompts), so it requires MASTRA_ORG_ID and MASTRA_PROJECT_ID to know where to deploy. runStudioDeploy checks both after loading the project's .env and throws if either is unset.","triggerScenarios":"Setting MASTRA_API_TOKEN in the environment (CI, scripts) without exporting MASTRA_ORG_ID and/or MASTRA_PROJECT_ID, and the project's .env file does not seed them (no prior `mastra init --observability`).","commonSituations":"CI pipelines that provision only the API token secret; running deploy from a directory whose .env lacks MASTRA_ORG_ID/MASTRA_PROJECT_ID; forgetting that the values can also come from .env, not just shell env.","solutions":["Export both variables alongside the token: MASTRA_ORG_ID and MASTRA_PROJECT_ID","Run `mastra init --observability` in the project so its .env provisions MASTRA_ORG_ID/MASTRA_PROJECT_ID for auto-linking","Add the two variables to your CI secret store next to MASTRA_API_TOKEN","Or drop MASTRA_API_TOKEN and run interactively to pick org/project via prompts"],"exampleFix":"// before (CI env)\nMASTRA_API_TOKEN=tok_xxx\nmastra studio deploy --yes\n// Error: MASTRA_ORG_ID and MASTRA_PROJECT_ID are required when MASTRA_API_TOKEN is set\n// after\nMASTRA_API_TOKEN=tok_xxx\nMASTRA_ORG_ID=org_acme\nMASTRA_PROJECT_ID=prj_001\nmastra studio deploy --yes","handlingStrategy":"validation","validationCode":"if (process.env.MASTRA_API_TOKEN && !(process.env.MASTRA_ORG_ID && process.env.MASTRA_PROJECT_ID)) {\n  throw new Error('Headless deploy requires MASTRA_ORG_ID and MASTRA_PROJECT_ID alongside MASTRA_API_TOKEN');\n}","typeGuard":"function isHeadlessEnv(env: NodeJS.ProcessEnv): env is NodeJS.ProcessEnv & { MASTRA_API_TOKEN: string; MASTRA_ORG_ID: string; MASTRA_PROJECT_ID: string } {\n  return Boolean(env.MASTRA_API_TOKEN && env.MASTRA_ORG_ID && env.MASTRA_PROJECT_ID);\n}","tryCatchPattern":"try {\n  await $`mastra studio deploy --yes`;\n} catch (e) {\n  if (String(e).includes('MASTRA_ORG_ID and MASTRA_PROJECT_ID are required')) {\n    console.error('Add MASTRA_ORG_ID/MASTRA_PROJECT_ID to CI secrets or run `mastra init --observability`.');\n    process.exitCode = 2;\n  } else throw e;\n}","preventionTips":["Store MASTRA_ORG_ID and MASTRA_PROJECT_ID as CI secrets next to MASTRA_API_TOKEN","Run `mastra init --observability` once so the project .env seeds both ids","Add a pre-deploy env assertion step in CI","Never set MASTRA_API_TOKEN without the two ids in headless environments"],"tags":["cli","env-vars","headless","ci"],"backgroundTag":"missing-env-var","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}