---
title: "Grok Text to Speech API in Node.js | Micdrop"
description: "Speak the answers of your voice agent with the Grok streaming text to speech by xAI, from a Node server. Twenty-six voices, speech tags, and one npm package."
url: "https://micdrop.dev/docs/text-to-speech/grok"
---

# Grok Text to Speech API in Node.js

`GrokTTS` speaks the answers of the agent with the [Grok streaming text to speech](https://docs.x.ai/developers/model-capabilities/audio/text-to-speech) by xAI, for [@micdrop/server](/docs/server). It reads the text while the agent writes it, and returns raw PCM at the rate of your choice.

## Installation

```bash
npm install @micdrop/grok @micdrop/openai
```

The same package also provides [Grok Voice](/docs/realtime/grok-voice), a model that hears and speaks, and [a speech to text](/docs/speech-to-text/grok).

## Usage with MicdropServer

```typescript
import { GladiaSTT } from '@micdrop/gladia'
import { GrokTTS } from '@micdrop/grok'
import { OpenaiAgent } from '@micdrop/openai'
import { MicdropServer } from '@micdrop/server'
import { WebSocketServer } from 'ws'

const wss = new WebSocketServer({ port: 8081 })

wss.on('connection', (socket) => {
  new MicdropServer(socket, {
    stt: new GladiaSTT({ apiKey: process.env.GLADIA_API_KEY || '' }),
    agent: new OpenaiAgent({
      apiKey: process.env.OPENAI_API_KEY || '',
      systemPrompt: 'You are a helpful assistant',
    }),
    tts: new GrokTTS({
      apiKey: process.env.XAI_API_KEY || '',
      voice: 'eve', // Default voice
      language: 'en', // Default: 'auto'
    }),
  })
})
```

## Usage without MicdropServer

```typescript
import { GrokTTS } from '@micdrop/grok'
import { Readable } from 'stream'

const tts = new GrokTTS({
  apiKey: process.env.XAI_API_KEY || '',
  voice: 'ara',
})

tts.on('Audio', (chunk) => console.log('Audio:', chunk.length, 'bytes'))
tts.on('Failed', (texts) => console.error('Failed:', texts))

tts.speak(Readable.from(['Hello! [laugh] ', 'How can I help you today?']))
```

## Events

Event

Payload

Description

`Audio`

`Buffer`

A chunk of the voice, PCM 16 bits mono at `sampleRate`.

`Failed`

`string[]`

The synthesis failed, with the text that was not spoken.

See the [TTS interface](/docs/text-to-speech/custom) for the full contract.

## Options

Option

Type

Default

Description

`apiKey`

`string`

Required

Your xAI API key

`voice`

`string`

`'eve'`

`eve`, `ara`, `leo`, `rex`, `sal` and twenty more, or a custom voice id

`language`

`string`

`'auto'`

Language of the text, as a BCP-47 code such as `en`, `fr` or `pt-BR`

`sampleRate`

`number`

`24000`

Rate of the audio: 8000, 16000, 22050, 24000, 44100 or 48000 Hz

`speed`

`number`

`1`

Speed of the voice, from 0.7 to 1.5

`textNormalization`

`boolean`

`false`

Reads numbers, abbreviations and symbols as they are spoken

`optimizeStreamingLatency`

`boolean`

`true`

Sends a shorter first chunk so the voice starts sooner, false favors quality

`connectionTimeout`

`number`

`5000`

Time to open the connection, in milliseconds

`retryDelay`

`number`

`1000`

Delay between two connection attempts, in milliseconds

`maxRetry`

`number`

`3`

Failed attempts in a row before `Failed`

## Speech Tags

The text can direct the voice with inline tags such as `[pause]`, `[laugh]` or `[sigh]`, and wrapping tags such as `<whisper>…</whisper>` or `<slow>…</slow>`. An agent prompted to use them makes the answers more expressive. The [xAI documentation](https://docs.x.ai/developers/model-capabilities/audio/text-to-speech) lists them all.

## Interruptions and Reconnection

The API speaks one answer at a time on a connection, and stops one only by closing it. When the user interrupts, `GrokTTS` closes the connection and opens a new one right away, so the next answer starts clear of the old audio. A connection that drops mid answer is opened again, and the text read so far goes out again. After `maxRetry` failed attempts in a row, `Failed` hands the text back, for a [`FallbackTTS`](/docs/text-to-speech/fallback) to speak it.

## Listing Voices

`GrokTTS.listVoices()` lists the voices of the API, which [Grok Voice](/docs/realtime/grok-voice) speaks with too:

```typescript
const voices = await GrokTTS.listVoices({
  apiKey: process.env.XAI_API_KEY || '',
  language: 'en', // Optional
})
```

It returns `MicdropVoice` objects with `id`, `name` and `languages`. The list is fetched once, then shared by every call of the process for a day. Pass `refresh: true` to fetch it again.
