{"record":{"id":"763931d21fc052f9","repo":"vercel/ai","slug":"xai-realtime-client-secret-request-failed-respo","errorCode":null,"errorMessage":"xAI realtime client secret request failed: ${response.status} ${text}","messagePattern":"xAI realtime client secret request failed: (.+?) (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/xai/src/realtime/xai-realtime-model.ts","lineNumber":58,"sourceCode":"    const url = `${this.config.baseURL}/realtime/client_secrets`;\n\n    const body: Record<string, unknown> = {};\n    if (options.expiresAfterSeconds != null) {\n      body.expires_after = { seconds: options.expiresAfterSeconds };\n    }\n\n    const response = await fetchFn(url, {\n      method: 'POST',\n      headers: {\n        ...this.config.headers(),\n        'Content-Type': 'application/json',\n      },\n      body: JSON.stringify(body),\n    });\n\n    if (!response.ok) {\n      const text = await response.text();\n      throw new Error(\n        `xAI realtime client secret request failed: ${response.status} ${text}`,\n      );\n    }\n\n    const data = (await response.json()) as {\n      value: string;\n      expires_at?: number;\n    };\n\n    return {\n      token: data.value,\n      // xAI selects the voice model from the `model` query parameter on the\n      // WebSocket URL. Without it the model choice is silently ignored and the\n      // server falls back to its default voice model.\n      url: `wss://${new URL(this.config.baseURL).host}/v1/realtime?model=${encodeURIComponent(this.modelId)}`,\n      expiresAt: data.expires_at,\n    };\n  }","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/xai/src/realtime/xai-realtime-model.ts#L40-L76","documentation":"XaiRealtimeModel.doCreateClientSecret POSTs to {baseURL}/realtime/client_secrets to mint an ephemeral client secret for realtime WebSocket sessions. When the HTTP response is not ok, the raw status and response body are wrapped in a plain Error. This is an API-level failure: auth, wrong model, quota, or endpoint availability.","triggerScenarios":"Calling createClientSecret on an xai realtime model when the xAI API returns 401 (invalid API key), 403, 404 (realtime endpoint not enabled for the account/model), or 429/5xx responses.","commonSituations":"Missing or revoked XAI_API_KEY; using a model id without realtime access; corporate proxy returning non-2xx; base URL misconfigured to a non-xAI gateway.","solutions":["Check the status/body in the message: fix 401 by setting a valid XAI_API_KEY, fix 404 by confirming the model supports realtime and the account has access","Verify baseURL is correct (default https://api.x.ai/v1) and reachable from your network","Retry on 429/5xx with backoff; if persistent, check xAI status or support"],"exampleFix":"// before\nconst secret = await model.doCreateClientSecret({});\n// after\ntry {\n  const secret = await model.doCreateClientSecret({});\n} catch (e) {\n  console.error('client secret failed:', (e as Error).message); // inspect status/text\n}","handlingStrategy":"retry","validationCode":"if (!process.env.XAI_API_KEY) throw new Error('XAI_API_KEY missing before requesting realtime client secret');","typeGuard":"null","tryCatchPattern":"try {\n  const secret = await model.doCreateClientSecret({ expiresAfterSeconds: 3600 });\n} catch (e) {\n  const msg = (e as Error).message;\n  if (msg.includes(' 401 ') || msg.includes(' 403 ')) {\n    // fix credentials\n  } else if (/ 429 | 5\\d\\d /.test(msg)) {\n    // retry with backoff\n  }\n}","preventionTips":["Verify XAI_API_KEY validity and realtime model access before deploying","Use the default baseURL unless deliberately pointing at a gateway","Apply exponential backoff for 429/5xx responses"],"tags":["xai","realtime","http-error","client-secret"],"backgroundTag":"http-request-failed","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}