pip install grokit
Import the Grokit
class and create an instance:
from grokit import Grokit
# You can set the X_AUTH_TOKEN and X_CSRF_TOKEN environment variables instead
grok = Grokit(
auth_token='***',
csrf_token='***',
)
response = grok.generate('Who are you?', model_id='grok-2-mini')
print(response)
for chunk in grok.stream('Who are you?', model_id='grok-2'):
print(chunk, end='', flush=True)
image_data = grok.image('An astronaut riding a horse.')
with open('image.jpg', 'wb') as f:
f.write(image_data)
image_url = grok.image_url('An astronaut riding a horse.')
print(image_url)
To obtain the necessary credentials for using Grokit, follow these steps:
-
Log in to x.com with a Premium account.
-
Open your browser's Developer Tools.
-
Navigate to the Network tab in the Developer Tools.
-
Load x.com/i/grok in your browser.
-
In the Network tab, look for the first request.
-
Click on this request to view its details.
-
In the Headers section, find the "Cookie" header under Request Headers.
-
From the cookie string, extract the following values:
ct0
: This is your csrf tokenauth_token
: This is your auth token