{"record":{"id":"f4368d87a3ef4e4a","repo":"mastra-ai/mastra","slug":"kimi-for-coding-device-authorization-failed-res","errorCode":null,"errorMessage":"Kimi For Coding device authorization failed: ${response.status}${text ? ` ${text}` : ''}","messagePattern":"Kimi For Coding device authorization failed: (.+?)(.+?)` : ''\\}","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/auth/providers/kimi-coding.ts","lineNumber":124,"sourceCode":"\nexport async function startKimiCodingDeviceLogin(options?: {\n  signal?: AbortSignal;\n}): Promise<KimiCodingDeviceLoginPending> {\n  const clientId = CLIENT_ID;\n  const deviceId = createKimiCodingDeviceId();\n  const response = await fetch(`${OAUTH_HOST}/api/oauth/device_authorization`, {\n    method: 'POST',\n    headers: {\n      ...getKimiCodingDeviceHeaders(deviceId),\n      'Content-Type': 'application/x-www-form-urlencoded',\n      Accept: 'application/json',\n    },\n    body: new URLSearchParams({ client_id: clientId }).toString(),\n    signal: requestSignal(options?.signal),\n  });\n  if (!response.ok) {\n    const text = await response.text().catch(() => '');\n    throw new Error(`Kimi For Coding device authorization failed: ${response.status}${text ? ` ${text}` : ''}`);\n  }\n\n  const data = await readJson(response);\n  const deviceCode = data?.device_code;\n  const userCode = data?.user_code;\n  const verificationUri = trustedHttpUrl(data?.verification_uri);\n  const verificationUriComplete = trustedHttpUrl(data?.verification_uri_complete);\n  if (\n    typeof deviceCode !== 'string' ||\n    !deviceCode ||\n    typeof userCode !== 'string' ||\n    !userCode ||\n    !verificationUri ||\n    !verificationUriComplete\n  ) {\n    throw new Error('Invalid Kimi For Coding device authorization response');\n  }\n","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/auth/providers/kimi-coding.ts#L106-L142","documentation":"startKimiCodingDeviceLogin POSTs the device-authorization request to Kimi's OAuth endpoint. On a non-ok HTTP status it throws with the status code and any response body text, signaling the device flow could not even be started (before any user code exists).","triggerScenarios":"The device authorization endpoint returns non-2xx: invalid/expired client_id (401/400), server outage (5xx), rate limiting (429), or a network middlebox returning an error page. Called via the 'pending' login path for Kimi For Coding.","commonSituations":"Embedded client_id no longer accepted after a Kimi API update; corporate proxy/firewall blocking the auth endpoint; transient 5xx or 429 during heavy use; wrong region/base URL configuration.","solutions":["Read the embedded status/body: 400/401 usually means the client_id is rejected — update the SDK or check Kimi's API status","Retry with backoff if the status is 429/5xx","Verify network access to the Kimi authorization endpoint (bypass proxies/VPN to test)","Re-run the login flow; device authorization is a fresh request each time"],"exampleFix":"// before\nconst pending = await startKimiCodingDeviceLogin(); // 429\n// after\nawait new Promise(r => setTimeout(r, 5000));\nconst pending = await startKimiCodingDeviceLogin(); // retry once after backoff","handlingStrategy":"retry","validationCode":"// Pre-flight: confirm the auth endpoint is reachable before starting the device flow\nconst ping = await fetch(authBaseUrl, { method: 'HEAD' }).catch(() => null);\nif (!ping) throw new Error('Kimi auth endpoint unreachable — check network/proxy before login');","typeGuard":"function isDeviceAuthHttpError(err: unknown): err is Error & { status?: number } {\n  const m = err instanceof Error ? err.message.match(/device authorization failed: (\\d{3})/) : null;\n  return m !== null;\n}","tryCatchPattern":"try {\n  pending = await startKimiCodingDeviceLogin();\n} catch (err) {\n  const m = err instanceof Error && err.message.match(/device authorization failed: (\\d{3})/);\n  if (m && (m[1].startsWith('5') || m[1] === '429')) {\n    await backoff(); pending = await startKimiCodingDeviceLogin(); // retry transient\n  } else throw err; // 400/401: client_id or config problem, don't retry\n}","preventionTips":["Use exponential backoff for 429/5xx on the authorization request","Verify outbound network access to the Kimi auth endpoint (corporate proxies often block it)","Keep the SDK updated in case the embedded client_id changes","Fail fast on 4xx — retrying invalid-client errors never helps"],"tags":["network","http","oauth","device-flow","kimi-coding"],"backgroundTag":"http-request-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}