π A Python library for interacting with the Grok 3 API without requiring login or manual cookie input. Perfect for out-of-the-box use.
-
πΌοΈ Support for sending images to Grok
Sending images to the Grok server is now much easier! A detailed description of the method is available here. -
π€ Ongoing work on OpenAI compatibility
- β
Now supports any
api_key
for server interaction. - βοΈ Added server configuration options (via command-line arguments and environment variables). Detailed instructions are available in π Running an OpenAI-Compatible Server.
- β
Now supports any
β οΈ The server is still in early development, and some features may be unstable.
Grok3API is a powerful and user-friendly unofficial tool for interacting with Grok models (including Grok3), allowing you to send requests, receive text responses, and, most excitingly, generated images β all with automatic cookie management! π¨β¨ The project is designed with simplicity and automation in mind, so you can focus on creativity rather than technical details.
- π Automatic cookie retrieval via browser with Cloudflare bypass β no manual setup required!
- πΌοΈ Convenient retrieval of generated images with the
save_to
method, enabling one-click saving. - π§ Flexible request customization: model selection, image generation control, attachment support, and more.
- π¦ Attachment support: send files and images along with requests.
- π οΈ Error handling: the client automatically resolves cookie issues and retries requests if something goes wrong.
- π€ Example Telegram bot (
grok3api
+aiogram
), capable of generating text responses and images.
To start using GrokClient, install the required dependencies. Itβs simple:
pip install grok3api
β οΈ Important: Ensure Google Chrome is installed, asundetected_chromedriver
relies on it.
After installation, youβre ready to go! π
Hereβs a complete example of sending a request and saving a generated image:
from grok3api.client import GrokClient
def main():
# Create a client (cookies are automatically retrieved if not provided)
client = GrokClient()
# Create a request
message = "Create an image of a ship"
# Send the request
result = client.ask(message=message,
images="C:\\Users\\user\\Downloads\\photo1_to_grok.jpg") # You can send an image to Grok
print("Grok's response:", result.modelResponse.message)
# Save the first image, if available
if result.modelResponse.generatedImages:
result.modelResponse.generatedImages[0].save_to("ship.jpg")
print("Image saved as ship.jpg! π")
if __name__ == '__main__':
main()
This code:
- Creates a client β automatically retrieves cookies if none are provided.
- Sends a request to generate an image.
- Saves the image to the file
ship.jpg
.
π What will we see?
Grok will generate an image of a ship, for example, something like this:
πΉ Or, for instance, if you request "A gopher on Elbrus":
π‘ Cool feature: You donβt need to manually fetch cookies β the client handles it for you!
The GrokClient.ask
method accepts various parameters to customize your request. Hereβs an example with settings:
from grok3api.client import GrokClient
def main():
# Create a client (cookies are automatically retrieved if not provided)
client = GrokClient(history_msg_count=5)
client.history.set_main_system_prompt("Respond briefly and with emojis.")
# Send a request with settings
result = client.ask(
message="Draw a cat like in this picture",
modelName="grok-3", # Default is grok-3 anyway
images=["C:\\Users\\user\\Downloads\\photo1_to_grok.jpg",
"C:\\Users\\user\\Downloads\\photo2_to_grok.jpg"] # You can send multiple images to Grok!
)
print(f"Grok3 response: {result.modelResponse.message}")
# Save all images
for i, img in enumerate(result.modelResponse.generatedImages):
img.save_to(f"cat_{i}.jpg")
print(f"Saved: cat_{i}.jpg πΎ")
if __name__ == '__main__':
main()
π The best part? It works with automatically retrieved cookies! No need to worry about access β the client sets everything up for you.
If cookies are missing or expired, Grok3API automatically:
- Uses the Chrome browser (ensure itβs installed).
- Visits
https://grok.com/
. - Bypasses Cloudflare protection.
- Continues operation.
You donβt need to do anything manually β just run the code, and it works!
One of the standout features of GrokClient is its super-convenient handling of generated images. Hereβs a complete example:
from grok3api.client import GrokClient
def main():
# Create a client (cookies are automatically retrieved if not provided)
client = GrokClient()
# Send a request
result = client.ask("Draw a sunset over the sea")
# Save all images
for i, image in enumerate(result.modelResponse.generatedImages):
image.save_to(f"sunset_{i}.jpg")
print(f"Saved: sunset_{i}.jpg π
")
if __name__ == '__main__':
main()
The ask
method returns a GrokResponse
object. Hereβs an example of working with it:
from grok3api.client import GrokClient
def main():
cookies = "YOUR_COOKIES_FROM_BROWSER"
# Create a client
client = GrokClient(cookies=cookies)
# Send a request
result = client.ask("Describe and draw a forest")
# Process the response
print(f"Text: {result.modelResponse.message}")
result.modelResponse.generatedImages[0].save_to("forest.jpg")
if __name__ == '__main__':
main()
Fields of the GrokResponse
object:
modelResponse
: The main model response.message
(str): The text response.generatedImages
(List[GeneratedImage]): List of images.
isThinking
: Whether the model was thinking (bool).isSoftStop
: Soft stop (bool).responseId
: Response ID (str).newTitle
: New chat title, if available (Optional[str]).
If somethingβs unclear, feel free to raise an issue β weβll figure it out together! π
Grok3API has no affiliation with xAI or the Grok developers. It is an independent project created by a third party and is not supported, sponsored or endorsed by xAI. Any issues with Grok should be addressed directly to xAI. You are responsible for ensuring that your use of Grok3API complies with all applicable laws and regulations. The developer does not encourage illegal use.