{"record":{"id":"4d304660ae03cd7b","repo":"mastra-ai/mastra","slug":"failed-to-initiate-xai-device-authorization-res","errorCode":null,"errorMessage":"Failed to initiate xAI device authorization: ${response.status}${text ? ` ${text}` : ''}","messagePattern":"Failed to initiate xAI device authorization: (.+?)(.+?)` : ''\\}","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/auth/providers/xai.ts","lineNumber":106,"sourceCode":"  /** RFC 8628 poll-loop state (interval growth, deadline, slow_down count). */\n  state: DeviceCodePollState;\n}\n\nexport type XAIDevicePollResult =\n  | { status: 'complete'; credentials: OAuthCredentials }\n  | { status: 'pending'; nextPollMs: number; pending: XAIDeviceLoginPending }\n  | { status: 'failed'; error: string };\n\n/**\n * Start an xAI device-code login: request a user code and return the\n * serializable pending state for subsequent polls.\n */\nexport async function startXAIDeviceLogin(options?: { signal?: AbortSignal }): Promise<XAIDeviceLoginPending> {\n  const response = await postForm(DEVICE_CODE_URL, { client_id: CLIENT_ID, scope: SCOPE }, options?.signal);\n\n  if (!response.ok) {\n    const text = await response.text().catch(() => '');\n    throw new Error(`Failed to initiate xAI device authorization: ${response.status}${text ? ` ${text}` : ''}`);\n  }\n\n  const data = (await response.json()) as {\n    device_code?: string;\n    user_code?: string;\n    verification_uri?: string;\n    verification_uri_complete?: string;\n    interval?: number;\n    expires_in?: number;\n  };\n\n  if (!data.device_code || !data.user_code || !data.verification_uri) {\n    throw new Error('xAI device authorization response missing required fields');\n  }\n\n  const url = validateVerificationUri(data.verification_uri_complete ?? data.verification_uri);\n\n  return {","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/auth/providers/xai.ts#L88-L124","documentation":"startXAIDeviceLogin POSTs the client_id and scope to the xAI device-authorization endpoint. If the HTTP response is not ok, it reads the body (best-effort) and throws with the status code and any text, surfacing why device authorization could not begin.","triggerScenarios":"Any call that starts an xAI device login where postForm(DEVICE_CODE_URL, ...) resolves with response.ok === false (4xx/5xx from the provider).","commonSituations":"Invalid or unregistered CLIENT_ID (401/403); invalid scope requested; xAI API outage (5xx); corporate proxy blocking the request; rate limiting (429).","solutions":["Read the status/text in the message: 401/403 → check client_id credentials; 429 → back off and retry; 5xx → provider issue.","Verify the CLIENT_ID and SCOPE match the values registered for the xAI app.","Check network/proxy/firewall access to the xAI API host.","Retry with backoff during provider outages."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// cheap reachability/config pre-check before starting device login\nif (!CLIENT_ID) throw new Error('xAI CLIENT_ID is not configured');\nif (!DEVICE_CODE_URL.startsWith('https://')) throw new Error('Device code URL must be https');","typeGuard":null,"tryCatchPattern":"try {\n  pending = await startXAIDeviceLogin({ signal });\n} catch (e) {\n  if (e instanceof Error && /status: 5\\d\\d|status: 429/.test(e.message)) {\n    await backoff(); // transient outage or rate limit\n    pending = await startXAIDeviceLogin({ signal });\n  } // 401/403 → fix CLIENT_ID/scope before retrying\n}","preventionTips":["Validate CLIENT_ID and SCOPE configuration at startup","Implement exponential backoff for 429/5xx responses","Ensure outbound network/proxy access to the xAI API host","Surface the status code (already in the message) to guide the fix"],"tags":["oauth","xai","device-flow","http"],"backgroundTag":"oauth-device-flow-initiation-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}