Route requests through a custom endpoint
An application must use a non-default xAI endpoint or proxy for model requests.
AI Tools · Function
createXai creates a customized xAI provider from @ai-sdk/xai. Configure its URL, authentication, headers, or fetch implementation, then use the returned provider to select xAI models.
createXai builds a customized xAI provider instance from optional settings. The returned provider can then be called with a model identifier, while authentication defaults to the XAI_API_KEY environment variable unless an explicit key is supplied.
createXai(settings?)| Name | Type | Description |
|---|---|---|
baseURL | string | Optional URL prefix for API calls; the documented default is https://api.x.ai/v1. |
apiKey | string | API key sent through the Authorization header; defaults to XAI_API_KEY. |
headers | Record<string,string> | Additional request headers. |
fetch | (input: RequestInfo, init?: RequestInit) => Promise<Response> | Custom request implementation. |
A configured xAI provider instance that can be called with a model identifier.
import { createXai } from '@ai-sdk/xai';
const xai = createXai({
apiKey: process.env.XAI_API_KEY,
});
const model = xai('grok-3');Creates a customized xAI provider and uses it to select a model.
An application must use a non-default xAI endpoint or proxy for model requests.
A service needs to attach organization-specific metadata or other HTTP values.
Tests or infrastructure require control over the HTTP transport.
createXai is the factory for a customized xAI provider from @ai-sdk/xai. Its result is callable with a model identifier, such as xai('grok-3').
Follow-up: How would you route requests through a proxy? Pass the proxy endpoint as the provider's baseURL setting.
The provider accepts an apiKey setting for the Authorization header. When that setting is absent, it reads XAI_API_KEY from the environment.
Follow-up: What is the effect of providing both values? The explicitly supplied apiKey takes precedence over the environment default.
baseURL changes the URL prefix used for API calls, and headers adds custom request headers. A fetch implementation can also be supplied to customize request execution.
Follow-up: Which setting is appropriate for a proxy server? Use baseURL to point calls at the proxy's URL prefix.
@ai-sdk/xaixaiGrok provider