grok3-api Python Client

repository·master·Indexed 19 days ago

https://github.com/mem0ai/grok3-api

An unofficial Python client (version 0.1.0) for interacting with the Grok 3 API using browser authentication cookies. It features the GrokClient for sending messages and supports optional integration with Mem0 for persistent AI memory.

Tokens
1.1K
Snippets
5
Records
6
Agent score
19%

What's inside grok3-api

  1. Install the Grok3 API client

    master

    To install the unofficial Grok3 API client, clone the repository, set up a virtual environment, and install the package locally using pip.

    git clone https://github.com/mem0ai/grok3-api.git
    cd grok3-api
    virtualenv pyenv
    source pyenv/bin/activate
    pip install .
  2. Obtain required cookie values for authentication

    master

    Since this is an unofficial client, you must authenticate by extracting cookies from a live browser session on grok.com:

    1. Log in to grok.com in your browser.
    2. Open Developer Tools (F12) and go to the Network tab.
    3. Filter for requests containing the new-chat endpoint (e.g., https://grok.com/rest/app-chat/conversations/new).
    4. Right-click the request and select Copy as cURL.
    5. From the resulting cURL command, extract the following keys from the -H 'cookie: ...' header:
      • x-anonuserid
      • x-challenge
      • x-signature
      • sso
      • sso-rw
  3. Integrate Mem0 for conversation memory

    master

    You can add a memory layer to your Grok interactions using mem0ai. This allows you to store user-specific facts and retrieve them later.

    1. Install the package: pip install mem0ai
    2. Use Memory.add() to store information associated with a user_id.
    3. Use Memory.search() to retrieve relevant memories for a specific user.
    from mem0 import Memory
    
    memory = Memory()
    
    # Add memory for a specific user
    result = memory.add("I like to take long walks on weekends.", user_id="alice")
    
    # Retrieve related memories
    related_memories = memory.search("What does alice like to do?", user_id="alice")
    print(related_memories)
  4. Use the GrokClient to send messages

    master

    Initialize the GrokClient by passing a dictionary containing your extracted cookie values. You can then use the .send_message() method to interact with Grok 3.

    from grok_client import GrokClient
    
    # Your cookie values
    cookies = {
        "x-anonuserid": "ffdd32e1",
        "x-challenge": "TkC4D..",
        "x-signature": "fJ0...",
        "sso": "ey...",
        "sso-rw": "ey..."
    }
    
    # Initialize the client
    client = GrokClient(cookies)
    
    # Send a message and get response
    response = client.send_message("write a poem")
    print(response)
  5. Initialize GrokClient with session cookies

    master

    To use the GrokClient, you must initialize it with a dictionary of session cookies obtained from your browser session on grok.com. The required keys in the cookies dictionary are:

    • x-anonuserid
    • x-challenge
    • x-signature
    • sso
    • sso-rw
    from grok_client.client import GrokClient
    
    cookies = {
        "x-anonuserid": "your_value",
        "x-challenge": "your_value",
        "x-signature": "your_value",
        "sso": "your_value",
        "sso-rw": "your_value"
    }
    
    client = GrokClient(cookies=cookies)
  6. Send a message using send_message()

    master

    The send_message(message) method sends a text string to the Grok 3 model. It handles the streaming response internally and returns the complete, concatenated text response as a single string.

    Arguments:

    • message (str): The user's input text.

    Returns:

    • str: The full text response from Grok.
    # Assuming client is already initialized
    response = client.send_message("Hello, Grok! How are you today?")
    print(response)