Open History to see the last commit
1import { getApiKeyFromEnv } from '@tanstack/ai-utils'
2import type { ClientOptions } from 'openai'
3
4export interface GrokClientConfig extends Omit<ClientOptions, 'apiKey'> {
5 apiKey: string
6}
7
8/**
9 * Gets Grok API key from environment variables
10 * @throws Error if XAI_API_KEY is not found
11 */
12export function getGrokApiKeyFromEnv(): string {
13 try {
14 return getApiKeyFromEnv('XAI_API_KEY')
15 } catch {
16 throw new Error(
17 'XAI_API_KEY is required. Please set it in your environment variables or use the factory function with an explicit API key.',
18 )
19 }
20}
21
22/**
23 * Returns a Grok client config with the default xAI base URL applied
24 * when not already set.
25 */
26export function withGrokDefaults(config: GrokClientConfig): GrokClientConfig {
27 return {
28 ...config,
29 baseURL: config.baseURL || 'https://api.x.ai/v1',
30 }
31}