Skip to content

Grok

Chat with Grok, X's built-in AI, and manage your Grok conversations. Grok replies stream back from X as newline-delimited JSON; emusks parses that stream into a single result object for you.

TIP

The high-level grok.ask() is all you need for one-shot questions. Use grok.createConversation() + grok.respond() when you want to manage conversation IDs yourself.

grok.ask(message, opts?)

Ask Grok a question. Creates a conversation (unless you pass opts.conversationId), sends the message, and returns the parsed response.

OptionTypeDescription
messagestringYour prompt
opts.conversationIdstringContinue an existing conversation instead of creating a new one
opts.modelstringModel option id (default grok-3-latest, see grok.models())
opts.reasoningbooleanEnable reasoning mode (default false)
opts.deepsearchbooleanEnable DeepSearch (default false)
opts.systemPromptNamestringNamed system prompt
opts.fileAttachmentsarrayAttachment objects to include with the prompt
opts.returnSearchResultsbooleanInclude web search results (default true)
opts.returnCitationsbooleanInclude citations (default true)
opts.rawbooleanReturn the raw NDJSON stream as a string
opts.bodyobjectMerge extra fields into the request body
js
const res = await client.grok.ask("what's the latest on the falcon 9?");
console.log(res.message);
console.log(res.conversationId);

// continue the same conversation
const followUp = await client.grok.ask("and the booster landing?", {
  conversationId: res.conversationId,
});

// reasoning mode
const math = await client.grok.ask("what is 17 * 23? show your reasoning", {
  reasoning: true,
});
console.log(math.reasoning, math.message);

The returned object:

FieldTypeDescription
conversationIdstringThe conversation this response belongs to
messagestringThe assembled final answer
reasoningstringThe assembled reasoning/thinking trace
followUpSuggestionsarraySuggested follow-up prompts
webResultsarrayWeb search results, when search was used
citationsarrayCitations, when returned
imagesarrayGenerated image attachments, when any
agentChatItemIdstringThe response message id (use it to delete)
chunksarrayEvery raw parsed line from the stream

grok.respond(conversationId, message, opts?)

Low-level: send a message to an existing conversation. Same opts as ask. Returns the parsed response (without re-wrapping the conversation id).

js
const id = await client.grok.createConversation();
const res = await client.grok.respond(id, "hello");

grok.createConversation()

Create an empty Grok conversation and return its id. Uses the GraphQL CreateGrokConversation mutation.

js
const conversationId = await client.grok.createConversation();

grok.home()

Get the Grok home payload: eligibility, default model, and available model options. Uses the GraphQL GrokHome query.

js
const home = await client.grok.home();
console.log(home.eligible_for_grok, home.default_grok_model_option_id);

grok.models()

Convenience wrapper around grok.home() that returns just the grok_model_options array.

js
const models = await client.grok.models();
// [{ id: "grok-3-latest", name: "Fast", ... }]

grok.history(opts?)

List your Grok conversations. Uses the GraphQL GrokHistory query.

OptionTypeDescription
opts.countnumberNumber of results
opts.cursorstringPagination cursor
js
const { items } = await client.grok.history({ count: 20 });

grok.conversation(restId, opts?)

Fetch the items (messages) in a conversation by its rest id. Uses the GraphQL GrokConversationItemsByRestId query.

js
const conv = await client.grok.conversation("2062645889592938576");
console.log(conv.grok_conversation_items_by_rest_id.items);

grok.mediaHistory(opts?)

List your Grok-generated media. Uses the GraphQL GrokMediaHistory query.

js
const media = await client.grok.mediaHistory();

grok.search(query, opts?)

Search your Grok conversations. Uses the GraphQL SearchGrokConversations query.

js
const results = await client.grok.search("falcon 9");

grok.pinned() / grok.pin(conversationId) / grok.unpin(conversationId)

Manage pinned conversations. Use the GraphQL GrokPinnedConversations, GrokPinConversation, and GrokUnpinConversation operations.

js
await client.grok.pin("2062645889592938576");
const { items } = await client.grok.pinned();
await client.grok.unpin("2062645889592938576");

grok.deleteMessage(conversationId, chatItemId)

Delete a single message from a conversation. Uses the GraphQL DeleteGrokMessage mutation.

js
const res = await client.grok.ask("hi");
await client.grok.deleteMessage(res.conversationId, res.agentChatItemId);

grok.clear()

Delete all your Grok conversations. Uses the GraphQL ClearGrokConversations mutation.

js
await client.grok.clear();

grok.share(shareId)

Resolve a shared Grok conversation by its share id. Uses the GraphQL GrokShare query.

js
const shared = await client.grok.share("some_share_id");

grok.setPreferences(params)

Update your Grok preferences. Uses the GraphQL SetGrokPreferences mutation.

js
await client.grok.setPreferences({ default_grok_mode: "Normal" });

not affiliated with X Corp.