Grok
Chat with Grok, Twitter's built-in AI, and manage your Grok conversations. Grok replies stream back from Twitter 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.
| Option | Type | Description |
|---|---|---|
message | string | Your prompt |
opts.conversationId | string | Continue an existing conversation instead of creating a new one |
opts.model | string | Model option id (default grok-3-latest, see grok.models()) |
opts.reasoning | boolean | Enable reasoning mode (default false) |
opts.deepsearch | boolean | Enable DeepSearch (default false) |
opts.systemPromptName | string | Named system prompt |
opts.fileAttachments | array | Attachment objects to include with the prompt |
opts.returnSearchResults | boolean | Include web search results (default true) |
opts.returnCitations | boolean | Include citations (default true) |
opts.raw | boolean | Return the raw NDJSON stream as a string |
opts.body | object | Merge extra fields into the request body |
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:
| Field | Type | Description |
|---|---|---|
conversationId | string | The conversation this response belongs to |
message | string | The assembled final answer |
reasoning | string | The assembled reasoning/thinking trace |
followUpSuggestions | array | Suggested follow-up prompts |
webResults | array | Web search results, when search was used |
citations | array | Citations, when returned |
images | array | Generated image attachments, when any |
agentChatItemId | string | The response message id (use it to delete) |
chunks | array | Every 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).
const id = await client.grok.createConversation();
const res = await client.grok.respond(id, "hello");grok.createConversation()
Create an empty Grok conversation on x.com and return its id. Uses the legacy CreateGrokConversation mutation, which still works. The x.com web client now starts conversations on grok.com over a WebSocket, which emusks does not implement.
const conversationId = await client.grok.createConversation();x.com and grok.com
Every method here uses the x.com Grok chat by default, the same one ask and respond write to, so conversations you create show up in history. The x.com web client now reads conversations from grok.com /rest/app-chat/* with a JWT from grokAccountJwtQuery. Pass { grokCom: true } to do the same. It falls back to x.com when the account has no linked grok.com account. rename, deleteConversation and the share link methods only exist on grok.com.
grok.jwt(opts?)
Mint the grok.com JWT and cache it until it expires. Returns { token, grokUserId, status, expiresAt }, with token: null and status: "NOT_LINKED" when no grok.com account is linked.
const { token, status } = await client.grok.jwt();grok.rest(route, opts?)
Call any grok.com REST route with the JWT. Takes method, body and params.
const conv = await client.grok.rest("/rest/app-chat/conversations", { params: { pageSize: 10 } });grok.home(opts?) / grok.models(opts?)
grok.com /rest/modes, or the legacy GrokHome payload with grok_model_options. models() returns just the list.
const models = await client.grok.models();grok.history(opts?)
List your Grok conversations.
| Option | Type | Description |
|---|---|---|
opts.count | number | Page size |
opts.cursor | string | Page token or cursor |
opts.query | string | grok.com only, full-text search |
opts.starred | boolean | grok.com only, pinned conversations |
opts.grokCom | boolean | Use grok.com |
const page = await client.grok.history({ count: 20 });grok.conversation(id, opts?)
Fetch the responses in a conversation. On x.com, id is the rest id.
const conv = await client.grok.conversation("2062645889592938576");grok.search(query, opts?)
Search your Grok conversations. grok.com searches message text. The x.com search op is gone, so on x.com it filters history titles.
const results = await client.grok.search("falcon 9");grok.pinned(opts?) / grok.pin(conversationId, opts?) / grok.unpin(conversationId, opts?)
List, pin and unpin conversations. grok.com calls this starring.
await client.grok.pin("2062645889592938576");
await client.grok.unpin("2062645889592938576");grok.rename(conversationId, title, opts?)
Rename a grok.com conversation.
grok.deleteConversation(conversationId)
Delete a grok.com conversation.
grok.deleteMessage(conversationId, chatItemId)
Delete a single message from an x.com conversation. Uses the legacy DeleteGrokMessage mutation.
const res = await client.grok.ask("hi");
await client.grok.deleteMessage(res.conversationId, res.agentChatItemId);grok.mediaHistory(opts?)
List your Grok-generated media from grok.com /rest/assets, or the legacy GrokMediaHistory query.
grok.shareConversation(conversationId, responseId) / grok.shareLinks(conversationId) / grok.revokeShare(shareLinkId)
Create, list and revoke grok.com share links.
grok.share(shareId)
Resolve a shared Grok conversation by its share id. Uses GrokShare.
const shared = await client.grok.share("some_share_id");grok.clear()
Delete all your Grok conversations. Uses grokSettingsClearConversationsMutation.
grok.preferences() / grok.setPreferences(params)
Read and update the three Grok data settings: allowGrokMemory, allowXaiDataSharing and allowXaiPersonalization.
const prefs = await client.grok.preferences();
await client.grok.setPreferences({ allowGrokMemory: false });grok.linkedAccounts() / grok.link(params)
List grok.com accounts that match this X account, and link one with { linkToken, oauthToken } or { authorizationCode }.
grok.tags(opts?) / grok.categories(opts?)
Grok tags for the explore page (language, placeId, topicId) and Grok categories for global trending (language).
const tags = await client.grok.tags({ language: "en" });grok.mediaProvenance(tweetId)
Media provenance for a post: the first post with the same media, the AI overview and the match count.
grok.mediaMatches(tweetId, opts?)
Other posts with the same media. opts.kind is recent (default), similar or mostLiked. Pass cursor from the previous page to continue. Returns { tweets, cursor, raw }.