The GrokClient class is the primary tool for working with Grok, used for sending requests to the model and automatically saving the history.
📁 Working with history:
When initializing an object of theGrokClientclass, an object of theHistoryclass is automatically initialized. The history is automatically loaded from a file whenGrokClientis initialized.
| Parameter | Type | Description | Default |
|---|---|---|---|
cookies |
str / dict / List[str / dict] |
Cookie from grok.com (not necessary) | - |
use_xvfb |
bool |
Flag to use Xvfb on Linux. | True |
proxy |
str |
URL of the proxy server, used only in case of regional blocking. | ... |
history_msg_count |
int |
Number of messages in the history. | 0 (history saving is disabled) |
history_path |
str |
Path to the history file in JSON format. | "chat_histories.json" |
history_as_json |
bool |
Whether to send the history to Grok in JSON format (if > 0). | True |
always_new_conversation |
bool |
Whether to use the URL for creating a new chat when sending a request to Grok. | True |
conversation_id |
str |
Chat ID at grok.com. If you want to continue the conversation from where you left off. Only used together with response_id. | None |
response_id |
str |
Grok response ID in the conversation_id chat. If you want to continue the conversation from where you left off. Only used together with conversation_id. | None |
enable_artifact_files |
bool |
If False, html file declarations will be replaced with markdown style with ```{lang}. |
False |
timeout |
int |
Maximum time for client initialization (in seconds). | 120 |
- An instance of the
GrokClientclass, ready for use.
- 🌐 Automatic Browser Initialization: When the client is initialized, a Chrome session will automatically start to prepare everything for sending requests.
- 🍪 Automatic Cookie Rotation: If a list of cookies (as strings or dictionaries) is provided, cookies will automatically rotate upon reaching the message limit — the new order will be preserved for the current and subsequent requests.
- 🐧 Linux Support: Detailed instructions for running on Linux
💡 On Linux without GUI, it is recommended to use Xvfb for stable operation in headless mode.
🛠️ To start working with the Grok API, create an instance of
GrokClientand use its methods, such asChatCompletion.create, to send requests.
from grok3api.client import GrokClient
def main():
# You can add a list of strings/dictionaries to automatically change when the limit is reached
# client = GrokClient(cookies="YOUR_COOKIES_FROM_BROWSER")
# Create a client (cookies will be automatically retrieved if not present)
client = GrokClient()
# Send a request via ChatCompletion
response = client.ask(message="Hello, Grok!")
print(response.modelResponse.message) # Prints the response from Grok
if __name__ == '__main__':
main()ChatCompletion: An object created withinGrokClientthat provides thecreatemethod for sending requests to the Grok model. For details, see Description of thecreatemethod.
- Error handling: Exceptions may occur during class initialization (e.g., if cookies could not be obtained). These are logged via
logger.error.