SDKs

TypeScript SDK

Use Grok from TypeScript with @grok/sdk, the official typed ESM client built on the SpaceXSI REST API. The SDK has no runtime dependencies and covers the Responses API and streaming, structured output, tools, image and video generation, files and batch processing, text to speech and transcription, and tokenization, model, and account lookup.

Experimental. The TypeScript SDK is in early development (pre-1.0), and its interfaces may change between releases. Pin an exact version in package.json and check the changelog when upgrading. Report bugs or share feedback in GitHub issues.

Requirements

Before installing the SDK, verify your environment meets these requirements:

  • Node.js 22.13 or later
  • A SpaceXSI API key
  • An ESM project ("type": "module" in package.json or .mts/.mjs entrypoints)

Installation

Install @grok/sdk with your package manager:

npm install @grok/sdk

Every release is also published identically as @xai-official/sdk with the same build and version. If you install @xai-official/sdk, import from @xai-official/sdk and @xai-official/sdk/tools instead.

Quickstart

Set your API key in the environment. Grok reads XAI_API_KEY or GROK_API_KEY automatically (XAI_API_KEY takes precedence when both are set).

Bash

export XAI_API_KEY="your-api-key"

Create a client and send a request to the Responses API:

TypeScript

import { Grok } from '@grok/sdk';

const client = new Grok();

const response = await client.responses.create({
  model: 'grok-4.7',
  input: 'Explain why the sky is blue in one sentence.',
});

console.log(response.toText());

Keep API keys on the server. The SDK blocks browser and Worker environments by default because shipping a secret key to client-side code exposes it to users.

The client class is also exported as SpaceXSI (and as the deprecated alias SpaceXAI), so existing code continues to run.

Resources

Explore the SDK repository, release history, and REST API schemas:


Last updated: December 1, 2026