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.
- 🤖 Full Grok API integration
- 📁 File upload support
- 💬 Conversation management
- 🛠️ Easy-to-use interface
- Python 3.8 or higher
- A valid X (formerly Twitter) account
- Grok AI access (free for all X members)
- Your account's authentication tokens
-
Clone the repository:
git clone https://github.com/vibheksoni/GrokAiChat.git cd GrokAiChat
-
Install dependencies:
pip install -r requirements.txt
-
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=""
-
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
- A standard X account (Grok AI is free for all X members)
- The following credentials from your account:
-
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
-
X-CSRF-Token:
- In the same Network tab
- Look for
x-csrf-token
in request headers - It's usually a 32-character string
-
Bearer Token:
- Find any request header containing
authorization
- Copy the token after "Bearer "
- Usually starts with "AAAA..."
- Find any request header containing
Store these in your .env
file:
COOKIES="your_copied_cookie_string"
X_CSRF_TOKEN="your_csrf_token"
BEARER_TOKEN="your_bearer_token"
- Terms of Service: This project may violate X's Terms of Service. Use at your own risk.
- Account Security:
- Never share your credentials
- Avoid excessive requests
- Use reasonable rate limits
- Compliance:
- This tool is for educational purposes only
- Commercial use may violate X's terms
- You are responsible for how you use this code
To avoid account flags:
- Limit requests to reasonable human speeds
- Add delays between messages
- Don't automate mass messaging
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())
Check the examples/
directory for more advanced use cases:
- Basic chat interaction (
chat.py
) - File attachments (
chatwithfiles.py
) - Custom model parameters
- Response parsing
Grok
: Main interface for API interactionsGrokMessages
: Response parser and message handler
Full documentation is available in the code comments.
- Fork the repository
- Create a feature branch
- Submit a pull request
MIT License - See license file for details.
Vibhek Soni
- Age: 19
- GitHub: @vibheksoni
- Project Link: GrokAiChat