{"record":{"id":"4dc81dd2aaaf9bae","repo":"mastra-ai/mastra","slug":"mastra-org-id-and-mastra-project-id-or-org-pr","errorCode":null,"errorMessage":"MASTRA_ORG_ID and MASTRA_PROJECT_ID (or --org/--project flags, or .mastra-project.json) are required when MASTRA_API_TOKEN is set","messagePattern":"MASTRA_ORG_ID and MASTRA_PROJECT_ID \\(or --org/--project flags, or \\.mastra-project\\.json\\) are required when MASTRA_API_TOKEN is set","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/server/deploy.ts","lineNumber":400,"sourceCode":"  const packageName = getPackageName(targetDir);\n\n  // Step 1: Auth\n  let token: string;\n  try {\n    token = await getToken();\n  } catch {\n    p.log.error(`Authentication failed. Run: mastra auth login`);\n    process.exit(1);\n  }\n\n  // Step 2: Load existing project config\n  const projectConfig = await loadProjectConfig(targetDir, opts.config);\n\n  // Step 3: Resolve org — flags and config are checked before requiring env vars\n  const hasOrg = Boolean(process.env.MASTRA_ORG_ID || opts.org || projectConfig?.organizationId);\n  const hasProject = Boolean(process.env.MASTRA_PROJECT_ID || opts.project || projectConfig?.projectId);\n  if (isHeadless && (!hasOrg || !hasProject)) {\n    throw new Error(\n      'MASTRA_ORG_ID and MASTRA_PROJECT_ID (or --org/--project flags, or .mastra-project.json) are required when MASTRA_API_TOKEN is set',\n    );\n  }\n\n  const { orgId, orgName } = await resolveOrg(token, projectConfig, opts.org);\n\n  // Step 4: Resolve project\n  const { projectId, projectName, projectSlug } = await resolveProject(\n    token,\n    orgId,\n    projectConfig,\n    opts.project,\n    packageName,\n    autoAccept,\n  );\n\n  // Step 5: Confirmation\n  const isAlreadyLinked = projectConfig?.projectId === projectId && projectConfig?.organizationId === orgId;","sourceCodeStart":382,"sourceCodeEnd":418,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/server/deploy.ts#L382-L418","documentation":"When MASTRA_API_TOKEN is set, deploy runs headless (no interactive prompts). Headless mode requires a fully specified target: an org id and a project id must come from MASTRA_ORG_ID/MASTRA_PROJECT_ID env vars, --org/--project flags, or .mastra-project.json. If either is missing the CLI throws rather than prompting.","triggerScenarios":"Exporting MASTRA_API_TOKEN (or running in CI with it set) and calling `mastra server deploy` with neither MASTRA_ORG_ID nor --org nor projectConfig.organizationId, OR with neither MASTRA_PROJECT_ID nor --project nor projectConfig.projectId — i.e. isHeadless && (!hasOrg || !hasProject).","commonSituations":"CI secret contains the API token but not org/project ids; developer exported MASTRA_API_TOKEN locally for testing then ran deploy expecting interactive prompts; .mastra-project.json deleted or only partially populated (org set, project missing).","solutions":["Set both MASTRA_ORG_ID and MASTRA_PROJECT_ID (e.g. as CI secrets)","Pass --org <id> and --project <id-or-slug> on the command line","Commit/restore .mastra-project.json with organizationId and projectId in the deploy directory","Unset MASTRA_API_TOKEN if you actually want the interactive flow"],"exampleFix":"// before (CI env)\nMASTRA_API_TOKEN=*** npx mastra server deploy --yes\n// after\nMASTRA_API_TOKEN=*** MASTRA_ORG_ID=org_123 MASTRA_PROJECT_ID=proj_456 npx mastra server deploy --yes","handlingStrategy":"validation","validationCode":"// Preflight before invoking headless deploy\nconst required = ['MASTRA_API_TOKEN', 'MASTRA_ORG_ID', 'MASTRA_PROJECT_ID'] as const;\nfor (const v of required) {\n  if (!process.env[v]) throw new Error(`${v} must be set for headless deploy`);\n}","typeGuard":"function hasHeadlessEnv(env: NodeJS.ProcessEnv): env is NodeJS.ProcessEnv &\n  { 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 deploy();\n} catch (err) {\n  if (err instanceof Error && err.message.includes('MASTRA_ORG_ID and MASTRA_PROJECT_ID')) {\n    console.error('Headless mode needs org+project: set MASTRA_ORG_ID/MASTRA_PROJECT_ID or pass --org/--project.');\n    process.exitCode = 2;\n  } else throw err;\n}","preventionTips":["Configure MASTRA_ORG_ID and MASTRA_PROJECT_ID as CI secrets alongside MASTRA_API_TOKEN","Keep .mastra-project.json present and current in the deploy directory","Don't export MASTRA_API_TOKEN in interactive shells unless headless flags are also provided","Add a preflight env check step at the start of the deploy pipeline"],"tags":["cli","ci","env-vars","headless"],"backgroundTag":"missing-env-var","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}