{"record":{"id":"9c1b667ada150181","repo":"mastra-ai/mastra","slug":"failed-to-create-access-token-res-status","errorCode":null,"errorMessage":"Failed to create access token (${res.status})","messagePattern":"Failed to create access token \\((.+?)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/init/observability-provision.ts","lineNumber":216,"sourceCode":"  return body.project;\n}\n\nasync function mintOrgToken({\n  token,\n  orgId,\n  keyName,\n}: {\n  token: string;\n  orgId: string;\n  keyName: string;\n}): Promise<string> {\n  const res = await platformFetch(`${MASTRA_PLATFORM_API_URL}/v1/auth/tokens`, {\n    method: 'POST',\n    headers: { ...authHeaders(token, orgId), 'Content-Type': 'application/json' },\n    body: JSON.stringify({ name: keyName }),\n  });\n  if (!res.ok) {\n    throw new Error(`Failed to create access token (${res.status})`);\n  }\n  const body = (await res.json()) as CreateTokenResponse;\n  return body.secret;\n}\n\n/**\n * Derive a per-project spans endpoint matching the mobs-collector route\n * `POST /projects/:projectId/ai/spans/publish`. Only used when a non-default\n * platform URL is in play; production usage relies on the\n * MastraPlatformExporter's own default base.\n */\nfunction deriveTracesEndpoint(platformUrl: string, projectId: string): string {\n  // Strip a trailing /v1 (or any other path) — we want the host root.\n  const url = new URL(platformUrl);\n  return `${url.origin}/projects/${projectId}/ai/spans/publish`;\n}\n","sourceCodeStart":198,"sourceCodeEnd":233,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/init/observability-provision.ts#L198-L233","documentation":"After the observability project exists, the CLI mints an organization access token via POST /v1/auth/tokens and reads the secret from the response. This error is thrown when the request returns a non-2xx status.","triggerScenarios":"provisionObservabilityProject calls mintOrgToken and the platform rejects the token-creation request: 401/403 (insufficient permissions to create org tokens), 429 (rate limit), or 5xx.","commonSituations":"Authenticated user lacking org admin/token-creation rights, expired session token, org-level policy forbidding API tokens, or transient platform failures.","solutions":["On 401/403, re-authenticate (`mastra login`) and ensure your role allows creating organization tokens.","Check org settings/policies that may restrict token creation.","Retry on 429/5xx after a delay.","Verify network connectivity to the Mastra platform API."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const token = await getValidPlatformToken(); // must be an org token with token-creation scope\nif (!token) throw new Error('Re-authenticate before minting org tokens');","typeGuard":null,"tryCatchPattern":"try {\n  await provisionObservabilityProject(...);\n} catch (err) {\n  if (err.message.startsWith('Failed to create access token')) {\n    const status = err.message.match(/\\((\\d+)\\)/)?.[1];\n    if (status === '401' || status === '403') {\n      // check org role/permissions, then re-auth\n    } else if (status === '429' || status?.startsWith('5')) {\n      await retryWithBackoff();\n    }\n  }\n}","preventionTips":["Ensure your account role permits creating organization API tokens.","Check org policies restricting token creation.","Re-authenticate before running init."],"tags":["network","api","authentication","http"],"backgroundTag":"http-request-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}