{"record":{"id":"dfcc0a47dc7db045","repo":"mastra-ai/mastra","slug":"failed-to-create-api-key-await-extracterror-re","errorCode":null,"errorMessage":"Failed to create API key — ${await extractError(res)}","messagePattern":"Failed to create API key — (.+?)","errorType":"http","errorClass":"PlatformApiError","httpStatus":null,"severity":"error","filePath":"mastracode/mastra-factory/src/platform.ts","lineNumber":109,"sourceCode":" * POST /v1/auth/tokens — mint an `sk_` WorkOS org API key.\n * Returns the plaintext secret; the platform never returns it again.\n */\nexport async function mintOrgApiKey({\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 PlatformApiError(res.status, `Failed to create API key — ${await extractError(res)}`);\n  }\n  const body = (await res.json()) as CreateTokenResponse;\n  return body.secret;\n}\n\n/**\n * POST /v1/server/projects/:id/databases — attach a Neon Postgres database.\n * Returns immediately with `status: 'provisioning'`; poll for `ready`.\n *\n * Note: this route requires `requireRole('admin')`. Non-admin org members\n * will get a 403.\n */\nexport async function attachNeonDatabase({\n  token,\n  orgId,\n  projectId,\n  name,\n  regionId,","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/mastra-factory/src/platform.ts#L91-L127","documentation":"mintOrgApiKey POSTs to /v1/auth/tokens with `{ name: keyName }` and org auth headers to mint a new org-scoped API key. Any non-ok response becomes a PlatformApiError with the upstream status and server error text, prefixed with 'Failed to create API key —'.","triggerScenarios":"POST /v1/auth/tokens returns 4xx/5xx: 401/403 for expired token or insufficient org permissions, 409/422 for a duplicate or invalid key name, 429 rate limiting, 5xx platform errors.","commonSituations":"Non-admin user trying to mint org keys; token expired mid-provisioning; key name colliding with an existing token; org hitting key-count limits; transient 502/503 from the platform.","solutions":["Check the message after the em-dash and address the stated cause (auth, name collision, permissions).","Re-authenticate to refresh an expired token, then rerun create-factory.","Ask an org admin to run the command or mint the key from the dashboard if your role lacks permission.","Retry after backoff if the status is 429 or 5xx."],"exampleFix":"// before\nconst secret = await mintOrgApiKey({ token, orgId, keyName: 'factory' });\n// after\nlet secret: string;\ntry {\n  secret = await mintOrgApiKey({ token, orgId, keyName: 'factory' });\n} catch (e) {\n  if (e instanceof PlatformApiError && e.status === 409) {\n    secret = await mintOrgApiKey({ token, orgId, keyName: `factory-${Date.now()}` });\n  } else throw e;\n}","handlingStrategy":"try-catch","validationCode":"if (!keyName || keyName.length > 100) throw new Error('API key name must be 1-100 chars');\n// optionally verify uniqueness if the platform lists existing tokens:\nconst tokens = await listOrgTokens({ token, orgId });\nif (tokens.some(t => t.name === keyName)) throw new Error(`Key name '${keyName}' already exists`);","typeGuard":"function isPlatformApiError(e) { return e instanceof PlatformApiError; }","tryCatchPattern":"try {\n  secret = await mintOrgApiKey({ token, orgId, keyName });\n} catch (err) {\n  if (err instanceof PlatformApiError && (err.status === 429 || err.status >= 500)) {\n    await sleep(2000); secret = await mintOrgApiKey({ token, orgId, keyName });\n  } else throw err;\n}","preventionTips":["Use unique key names (append timestamp/suffix) to avoid collisions.","Confirm admin role before attempting org-level key minting.","Refresh expired tokens before multi-step provisioning.","Back off and retry on 429/5xx."],"tags":["http-error","platform-api","api-key","auth"],"backgroundTag":"http-api-error","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}