TypeScript SDK

Models and account

Examples use the client created in the quickstart.

Tokenization

Encode text with a language model's tokenizer using client.tokenizer.encode() to inspect token counts and splits:

TypeScript

const { token_ids } = await client.tokenizer.encode({
  model: 'grok-4.7',
  text: 'Hello world!',
});

console.log(token_ids.length);
for (const token of token_ids) {
  console.log(token.token_id, token.string_token);
}

Inference requests add framing tokens of their own, so usage.input_tokens can exceed the raw count returned here.

To reconstruct text from encoded tokens, decode token_bytes rather than joining string_token, because a single token can contain part of a multi-byte UTF-8 character:

TypeScript

const text = new TextDecoder().decode(
  new Uint8Array(token_ids.flatMap(token => token.token_bytes)),
);

Models

Pass model IDs directly as strings. ModelId autocompletes known model IDs while still accepting newer models released after your installed SDK version. Use KnownModelId when you want strict type checking against models documented in the Models reference at release time:

TypeScript

import { type KnownModelId } from '@grok/sdk';

const model: KnownModelId = 'grok-4.7';

const available = await client.models.list();
for (const availableModel of available.data) {
  console.log(availableModel.id);
}

const modelInfo = await client.models.get(model);
console.log(modelInfo);

Language, image, and video models also have dedicated catalog endpoints with modality, alias, capability, and pricing metadata (ImageModelId / KnownImageModelId and VideoModelId / KnownVideoModelId follow the same pattern):

TypeScript

import { type KnownImageModelId } from '@grok/sdk';

const imageModel: KnownImageModelId = 'grok-imagine-image-2.0';

const imageModels = await client.models.image.list();
for (const availableImageModel of imageModels.models) {
  console.log(availableImageModel.id, availableImageModel.aliases);
}

const imageModelInfo = await client.models.image.get(imageModel);
console.log(imageModelInfo);

Query language model capabilities and token pricing with client.models.language:

TypeScript

const languageModels = await client.models.language.list();
for (const languageModel of languageModels.models) {
  console.log(languageModel.id, languageModel.input_modalities);
}

const languageModelInfo = await client.models.language.get('grok-4.7');
console.log(languageModelInfo.capabilities?.reasoning_effort);

Token prices such as prompt_text_token_price are reported in USD cents per 100 million tokens; divide by 10,000 for US dollars per million tokens.

Account

Inspect the active API key's name, permissions, and status with client.account.apiKey():

TypeScript

const apiKeyInfo = await client.account.apiKey();
console.log(apiKeyInfo.name, apiKeyInfo.acls, apiKeyInfo.api_key_disabled);

Last updated: December 1, 2026