Grok Voice Agent API in Node.js
A realtime model running on the Grok Voice Agent API by xAI. It hears the user and answers with its own voice, in place of a speech to text, an agent and a text to speech.
The API speaks the protocol of the OpenAI Realtime API, so GrokVoice extends OpenaiRealtime and shares its handling of turns, interruptions and tool calls. What differs is the session: the voices, the reasoning switch and the tools the API runs itself, such as a web search or a search of X.
Installation
npm install @micdrop/grok @micdrop/openaiThe same package also provides a speech to text and a text to speech. The providers page shows what every provider covers.
Usage with MicdropServer
import { GrokVoice } from '@micdrop/grok'import { endCallTool, MicdropServer } from '@micdrop/server'import { WebSocketServer } from 'ws'
const wss = new WebSocketServer({ port: 8081 })
wss.on('connection', (socket) => { new MicdropServer(socket, { firstMessage: { generate: true }, realtime: new GrokVoice({ apiKey: process.env.XAI_API_KEY || '', model: 'grok-voice-latest', // Default model voice: 'eve', // Default voice systemPrompt: 'You are a helpful assistant', language: 'en', // Optional, helps the transcription of the user reasoningEffort: 'none', // Optional, the first word comes sooner tools: [endCallTool()], // Automatically end call when user requests }), })})The Micdrop client detects when the user speaks, so the turn detection of the API is turned off. The audio of each turn goes to the API at 16 kHz as the client records it, is committed when the client closes the turn, and the answer is asked for right after. Tools go in the tools option or are added with addTool(), as with any agent.
When the user interrupts, the answer is cancelled and the conversation of the model is cut where the user most likely stopped hearing it.
Server Tools
serverTools hands the API tools it runs on its own during the answer, next to the tools of the agent that run in your server:
const realtime = new GrokVoice({ apiKey: process.env.XAI_API_KEY || '', systemPrompt: 'You answer questions about the news.', reasoningEffort: 'none', serverTools: [ { type: 'web_search', allowed_domains: ['reuters.com', 'apnews.com'] }, { type: 'x_search', from_date: '2026-11-01' }, ],})| Tool | What it does |
|---|---|
web_search | Searches the web, within allowed_domains or outside excluded_domains |
x_search | Searches posts on X, by handles and dates |
file_search | Searches the collections given as vector_store_ids |
mcp | Calls the tools of a remote MCP server |
The API bills these searches on top of the minutes of the call.
Events
GrokVoice emits the events of an agent, and two of its own.
| Event | Payload | Description |
|---|---|---|
Audio | Buffer | A chunk of the voice of the model, PCM 16 bits, 24 kHz, mono. |
PartialMessage | string | The transcript of the answer so far. |
Message | MicdropConversationItem | A message, a tool call or a tool result was added to the conversation. |
ToolCall | MicdropToolCall | A tool declared with emitOutput ran, with its parameters and output. |
CancelLastUserMessage | none | The last user message was dropped because it carried no intent. |
SkipAnswer | none | The model gave no spoken answer. |
EndCall | none | The model decided that the call is over. |
Failed | none | The connection could not be restored after its retries. |
Options
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | Required | Your xAI API key |
systemPrompt | string | Required | Instructions of the model |
model | string | 'grok-voice-latest' | grok-voice-latest, which follows the newest model, or grok-voice-3.0 |
voice | string | 'eve' | eve, ara, leo, rex, sal and twenty more, or a custom voice id |
language | string | undefined | Language of the user, as a BCP-47 code, for the transcription |
keyterms | string[] | undefined | Names and terms the transcription should expect, 100 at most |
reasoningEffort | 'none' | 'high' | undefined | Whether the model reasons before speaking, which it does unless set to none |
serverTools | GrokServerTool[] | [] | Tools the API runs itself, see above |
replace | Record<string, string> | undefined | Words to say differently from how they are written, such as { nginx: 'engine x' } |
speed | number | undefined | Speed of the voice, from 0.7 to 1.5 |
tools | Tool[] | [] | Tools the model can call, such as the built-in endCallTool() |
connectionTimeout | number | 5000 | Time to open the connection, in milliseconds |
retryDelay | number | 1000 | Delay before reconnecting, in milliseconds |
maxRetry | number | 3 | Reconnection attempts before giving up |
Reasoning
Grok Voice reasons before it speaks unless told otherwise. GrokVoice leaves the choice to the model, so set reasoningEffort: 'none' for the soonest answer, as the examples of this page do.
Speaking a Fixed Text
say(), and the firstMessage of the server when it is a text, has the model read the text word for word with a forced message of the API. The text lands in the conversation of the model as said by the assistant, once.
Voices
GrokTTS.listVoices() lists the voices Grok Voice speaks with, the same as its text to speech:
import { GrokTTS } from '@micdrop/grok'
const voices = await GrokTTS.listVoices({ apiKey: process.env.XAI_API_KEY || '',})GrokVoice.listModels() returns the models the documentation of the API lists, without calling it.
Reconnection
When the connection closes, GrokVoice reconnects and sends the conversation written so far to the new session. The answer in progress ends with the old session. What the call sends while the connection opens waits for it and goes out once it is back. After maxRetry failed attempts, Failed is emitted and every later answer ends right away.