Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Tools · Function

createXai

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.

How it works

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.

Syntax

createXai(settings?)

Parameters

NameTypeDescription
baseURLstringOptional URL prefix for API calls; the documented default is https://api.x.ai/v1.
apiKeystringAPI key sent through the Authorization header; defaults to XAI_API_KEY.
headersRecord<string,string>Additional request headers.
fetch(input: RequestInfo, init?: RequestInit) => Promise<Response>Custom request implementation.

Returns

A configured xAI provider instance that can be called with a model identifier.

Key points

  • Import createXai from @ai-sdk/xai when the default xAI provider instance is not sufficient.
  • Call createXai with a settings object to construct a configured provider.
  • The baseURL option redirects API calls to a different URL prefix, which supports proxy configurations.
  • The apiKey option supplies the credential sent in the Authorization header.
  • The headers option adds extra headers to outgoing requests.
  • A custom fetch function can be provided to control how HTTP requests are made.

Common mistakes

  • If apiKey is omitted, authentication uses the XAI_API_KEY environment variable; supplying apiKey overrides that default.

Example: Create an xAI provider

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.

When to use it

Route requests through a custom endpoint

An application must use a non-default xAI endpoint or proxy for model requests.

Add custom request headers

A service needs to attach organization-specific metadata or other HTTP values.

Replace the fetch implementation

Tests or infrastructure require control over the HTTP transport.

Interview questions

QWhat does createXai return and how is it used?

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.

QHow does createXai handle authentication?

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.

QWhich request-level settings can be customized?

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/xai
  • xai
  • Grok provider

Source: xAI Grok Provider. Checked 2026-10-15.