npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

grok-api-ts

v1.1.2

Published

TypeScript client for interacting with Grok AI with automated login and cookie management

Readme

Unofficial Grok API Client

A TypeScript client for interacting with Grok AI. This client provides automated login functionality, cookie management, and comprehensive interaction with Grok's API.

Features

  • Automated browser-based login to Grok via X.AI auth
  • Cookie persistence to avoid repeated logins
  • Automatic handling of expired cookies
  • Send messages to Grok with customizable options
  • Continue conversations with follow-up messages
  • Support for image attachments
  • Customizable search settings and image generation
  • Support for custom instructions and reasoning mode
  • Conversation management

Installation

# Using npm
npm install grok-api-ts

# Using pnpm
pnpm add grok-api-ts

# Using yarn
yarn add grok-api-ts

Usage

Basic Usage

import GrokAPI from 'grok-api-ts';
// Or use named import
// import { GrokAPI } from 'grok-api-ts';

async function main() {
  // Initialize the Grok API (will prompt for login if needed)
  const grokApi = new GrokAPI();
  
  // If not authenticated, you'll need to login
  if (!grokApi.isAuthenticated()) {
    await grokApi.login('[email protected]', 'your-password');
  }
  
  // Start a new conversation
  const response = await grokApi.sendMessage({
    message: "What are the three laws of robotics?",
  });
  
  console.log(response.fullMessage);
  
  // Continue the conversation
  const followUp = await grokApi.continueConversation(
    "Who created these laws and in what book did they first appear?"
  );
  
  console.log(followUp.fullMessage);
}

main();

Automated Login Process

The client handles authentication automatically:

  1. First, it checks for existing cookies stored in grok-cookies.json
  2. If valid cookies are found, they will be used for API requests
  3. If no cookies are found, you'll need to call login(username, password) method
  4. During login, a real browser will open to complete the authentication via X.AI's auth system
  5. After successful login, cookies are saved for future use
  6. If cookies expire during usage, the client will detect auth failures and prompt for re-login

Advanced Usage: Custom Options

import GrokAPI from 'grok-api-ts';

async function main() {
  const grokApi = new GrokAPI();
  
  // Ensure authenticated
  await grokApi.ensureAuthenticated('[email protected]', 'your-password');
  
  // Start a new conversation with custom options
  const response = await grokApi.sendMessage({
    message: "Explain the concept of quantum computing",
    disableSearch: false,           // Enable web search (default)
    enableImageGeneration: true,    // Allow Grok to generate images (default)
    customInstructions: "Explain in simple terms with analogies",
    forceConcise: true              // Request a more concise response
  });
  
  console.log(response.fullMessage);
  
  // Access response metadata
  if (response.metadata?.followUpSuggestions) {
    console.log("Follow-up suggestions:", response.metadata.followUpSuggestions);
  }
  
  // Access conversation information
  const { conversationId, lastResponseId } = grokApi.getConversationInfo();
  console.log(`Conversation ID: ${conversationId}`);
}

main();

Streaming Responses

The client supports streaming responses, allowing you to process Grok's response as it's being generated:

import GrokAPI from 'grok-api-ts';

async function main() {
  const grokApi = new GrokAPI();
  await grokApi.ensureAuthenticated();

  // Use streaming API with callbacks
  await grokApi.sendMessageStream(
    {
      message: "Write a story about a space adventure",
    },
    {
      // Called for each token of the response
      onToken: (token: string) => {
        process.stdout.write(token);
      },
      // Called when the response is complete with full response data
      onComplete: (response: ParsedGrokResponse) => {
        console.log("\nResponse completed!");
        console.log("Follow-up suggestions:", response.metadata?.followUpSuggestions);
      },
      // Called if an error occurs during streaming
      onError: (error: any) => {
        console.error("Error during streaming:", error);
      }
    }
  );
}

main();

Handling Authentication Errors

The client automatically handles authentication errors:

import GrokAPI from 'grok-api-ts';

async function main() {
  const grokApi = new GrokAPI();
  
  try {
    // Try making a request
    const response = await grokApi.sendMessage({
      message: "Hello Grok!"
    });
    console.log(response.fullMessage);
  } catch (error) {
    // If authentication failed, login and retry
    console.log("Authentication error, attempting login...");
    const loginSuccess = await grokApi.login('[email protected]', 'your-password');
    
    if (loginSuccess) {
      // Retry the request after successful login
      const response = await grokApi.sendMessage({
        message: "Hello Grok!"
      });
      console.log(response.fullMessage);
    }
  }
}

main();

Customizing Cookie Storage Location

You can specify a custom location for cookie storage:

const grokApi = new GrokAPI([], '/path/to/your/cookies.json');

Using with TypeScript

All types are exported for TypeScript users:

import GrokAPI, { 
  GrokSendMessageOptions, 
  ParsedGrokResponse, 
  GrokModelResponse 
} from 'grok-api-ts';

// Use types in your code
const options: GrokSendMessageOptions = {
  message: "Hello Grok!",
  disableSearch: true
};

// Type safety for function returns
const handleResponse = (response: ParsedGrokResponse) => {
  console.log(response.fullMessage);
};

API Reference

GrokAPI Class

Constructor

constructor(cookieStrings?: string[], cookiesPath?: string)
  • cookieStrings: Optional array of cookie strings
  • cookiesPath: Optional path to the cookies file (defaults to grok-cookies.json)

Methods

  • async login(username: string, password: string): Promise<boolean> Automates the login process using a real browser and returns success status.

  • isAuthenticated(): boolean Checks if the client has valid authentication cookies.

  • async ensureAuthenticated(username?: string, password?: string): Promise<boolean> Ensures the client is authenticated, triggering login if needed.

  • async sendMessage(options: GrokSendMessageOptions): Promise<ParsedGrokResponse> Sends a message to Grok and returns the response.

    Options include:

    • message: The message text to send
    • disableSearch: Whether to disable web search capability (default: false)
    • enableImageGeneration: Whether to allow Grok to generate images (default: true)
    • customInstructions: Custom instructions to guide Grok's response
    • forceConcise: Request a more concise response (default: false)
    • imageAttachments: Array of image attachments
    • conversationId: ID of the conversation (defaults to 'new')
    • parentResponseId: ID of the parent response for threading
    • isReasoning: Enable reasoning mode (default: false)
  • async continueConversation(message: string, options?: Omit<GrokSendMessageOptions, 'message'>): Promise<ParsedGrokResponse> Continues an existing conversation with a follow-up message.

  • getConversationInfo(): { conversationId: string; lastResponseId: string } Returns information about the current conversation.

Response Types

The ParsedGrokResponse includes:

  • fullMessage: The complete text response from Grok
  • responseId: Unique ID for the response (for continuing the conversation)
  • title: Conversation title (for new conversations)
  • metadata: Additional metadata including follow-up suggestions
  • modelResponse: Detailed model response object
  • conversationId: ID of the conversation

Requirements

  • Node.js 14+
  • Chrome browser installed (used by patchright for authentication)

License

MIT