Skip to Content

xAI API accounts

An xAI API-key account and a Grok CLI subscription are different credentials. This guide uses a regular product connected to an xAI API-key account. A Grok subscription must use its native relay setup instead.

Discover a product model

import { ExternalServiceProvider } from '@preternatural-js/client-sdk' const productId = 'your-product-id' const models = await client.models.list({ productId, provider: ExternalServiceProvider.xAI }) const model = models[0] if (!model) throw new Error('No xAI model is configured for this product')

The product must have an xAI account and a usage rule matching that model. A catalog result is configuration discovery, not a live quota or feature probe.

Send a completion

const answer = await client.chat.complete({ scope: { productId, provider: ExternalServiceProvider.xAI, modelIdentifier: model.id }, messages: [{ role: 'user', content: 'Summarize this task.' }] })

Use client.chat.stream() for streamed text, or construct an OpenAI-compatible provider client with client.chat.createProviderClient(scope) for fields beyond the helper’s basic text options. The returned client’s requests remain customer-scoped; it does not require your application to expose the xAI API key.

The selected model and provider must support the requested features. Do not assume that a Grok CLI subscription’s model catalog, quota or credits apply to an xAI API-key account. See gateway errors and model discovery.

Last updated on