Skip to main content

GrokAiChat

A Python library for interacting with Grok AI through X's user account API. This project provides a clean interface for creating conversations, sending messages, and handling responses. Note: This uses the account user API, not the paid enterprise API. Grok AI is free for all X (Twitter) members.

Features

  • 🤖 Full Grok API integration
  • 📁 File upload support
  • 💬 Conversation management
  • 🛠️ Easy-to-use interface

Prerequisites

  • Python 3.8 or higher
  • A valid X (formerly Twitter) account
  • Grok AI access (free for all X members)
  • Your account's authentication tokens

Installation

  1. Clone the repository:

    git clone https://github.com/vibheksoni/GrokAiChat.git
    cd GrokAiChat
    
  2. Install dependencies:

    pip install -r requirements.txt
    

Configuration

  1. Create a .env file in the project root:

    # Raw cookies string
    # Example: "cookie1=value1; cookie2=value2"
    COOKIES=""
    # CSRF token
    # Create a chat and send a message look at the headers of the request and check for the CSRF token
    X_CSRF_TOKEN=""
    # Bearer token
    # Create a chat and send a message look at the headers of the request and check for the Bearer token
    BEARER_TOKEN=""
    
  2. To obtain your tokens:

    • Log into X.com
    • Open Developer Tools (F12)
    • Create a Grok chat
    • Find the tokens in the Network tab request headers

Account Requirements & Credentials

⚠️ IMPORTANT: You need:

  1. A standard X account (Grok AI is free for all X members)
  2. The following credentials from your account:

How to Get Your Credentials

  1. Cookies:

    • Log into X.com
    • Open Developer Tools (F12) → Network tab
    • Interact with Grok
    • Find any request to x.com
    • Copy the entire cookie header value
  2. X-CSRF-Token:

    • In the same Network tab
    • Look for x-csrf-token in request headers
    • It's usually a 32-character string
  3. Bearer Token:

    • Find any request header containing authorization
    • Copy the token after "Bearer "
    • Usually starts with "AAAA..."

Store these in your .env file:

COOKIES="your_copied_cookie_string"
X_CSRF_TOKEN="your_csrf_token"
BEARER_TOKEN="your_bearer_token"

⚠️ Important Legal Warnings

  1. Terms of Service: This project may violate X's Terms of Service. Use at your own risk.
  2. Account Security:
    • Never share your credentials
    • Avoid excessive requests
    • Use reasonable rate limits
  3. Compliance:
    • This tool is for educational purposes only
    • Commercial use may violate X's terms
    • You are responsible for how you use this code

Rate Limiting

To avoid account flags:

  • Limit requests to reasonable human speeds
  • Add delays between messages
  • Don't automate mass messaging

Quick Start

from grok import Grok, GrokMessages
from dotenv import load_dotenv
import os

load_dotenv()
grok = Grok(
    os.getenv("BEARER_TOKEN"),
    os.getenv("X_CSRF_TOKEN"),
    os.getenv("COOKIES")
)

# Create a conversation
grok.create_conversation()

# Send a message
request = grok.create_message("grok-2")
grok.add_user_message(request, "Hello, Grok!")
response = grok.send(request)

# Parse and print response
messages = GrokMessages(response)
print(messages.get_full_message())

Advanced Usage

Check the examples/ directory for more advanced use cases:

  • Basic chat interaction (chat.py)
  • File attachments (chatwithfiles.py)
  • Custom model parameters
  • Response parsing

API Documentation

Main Classes

  • Grok: Main interface for API interactions
  • GrokMessages: Response parser and message handler

Full documentation is available in the code comments.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Submit a pull request

Legal Notice

⚠️ Disclaimer: This project is for educational purposes only. Users are responsible for ensuring their usage complies with X's terms of service.

License

MIT License - See license file for details.

Author

Vibhek Soni

Metadata

Release files for GrokAiChat 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for GrokAiChat 0.1.0
File Size Uploaded Provenance
grokaichat-0.1.0.tar.gz 4.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for GrokAiChat 0.1.0
File Interpreter ABI Platform
GrokAiChat-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.4 kB

Release files / grokaichat-0.1.0.tar.gz

Download URL grokaichat-0.1.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fe3db06eb5651caf2dd37750d2623bd6bd332aebb31648464b3e6259bcf0365a
BLAKE2b-256 checksum
How to use checksums
3754e0c964c1b1cb8f1eb81a88cb2c269277dbcd69d2d5c4132a91745a41f366
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.6

Release files / GrokAiChat-0.1.0-py3-none-any.whl

Download URL GrokAiChat-0.1.0-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3cae522bd95fcea330bf6c4b66cdd105b90143c7b346f23af789c4d05163761c
BLAKE2b-256 checksum
How to use checksums
7266efa4bb4668b622347a104c82ff9723d1d79806ddbea5e62e6c1404d23af0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.6

Security for GrokAiChat 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page