Skip to main content

Python-библиотека для взаимодействия с Grok3, ориентированная на максимальную простоту использования. Автоматически получает cookies, поэтому ничего не нужно указывать. A Python library for interacting with Grok3, focused on maximum ease of use. Automatically gets cookies, so you don't have to specify anything.

Project description

🚀 A Python library for interacting with the Grok 3 API without requiring login or manual cookie input. Perfect for out-of-the-box use.

➡ Ru ReadMe

🤖 Grok3API: Client for Working with Grok

Python Made with ❤️

Stars Forks Issues

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.


📦 Full Changelog

🆕 v0.1.0b1

✨ New:

  • 🛠 Improved code block handling Added automatic transformation of nested blocks <xaiArtifact contentType="text/...">...</xaiArtifact> into standard Markdown code blocks with language indication.

  • ☑️ The feature can be disabled by setting the auto_transform_code_blocks=False parameter when creating GrokClient.


🌟 Features

  • 🚀 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)

📦 Installation

To start using GrokClient, install the required dependencies. It’s simple:

pip install grok3api

⚠️ Important: Ensure Google Chrome is installed, as undetected_chromedriver relies on it.

After installation, you’re ready to go! 🎉


🚀 Usage

Quick Start

🍀 Minimal working example:

from grok3api.client import GrokClient

client = GrokClient()
answer = client.ask("Hi! How are you?")

print(answer.modelResponse.message)

Here’s a complete example of sending a request and saving a generated image:

from grok3api.client import GrokClient

# 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:\\Folder\\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! 🚀")

This code:

  1. Creates a client — automatically retrieves cookies if none are provided.
  2. Sends a request to generate an image.
  3. 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:

Ship example

🐹 Or, for instance, if you request "A gopher on Elbrus":

Gopher on Elbrus

💡 Cool feature: You don’t need to manually fetch cookies — the client handles it for you!


🔧 Request Parameters

The GrokClient.ask method accepts various parameters to customize your request. Here’s an example with settings:

from grok3api.client import GrokClient


client = GrokClient(history_msg_count=5, always_new_conversation=False) # to use conversation history from grok.com
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 🐾")

🌟 The best part? It works with automatically retrieved cookies! No need to worry about access — the client sets everything up for you.


🔄 Automatic Cookie Retrieval

If cookies are missing or expired, Grok3API automatically:

  1. Uses the Chrome browser (ensure it’s installed).
  2. Visits https://grok.com/.
  3. Bypasses Cloudflare protection.
  4. Continues operation.

You don’t need to do anything manually — just run the code, and it works!

💼️ GrokClient Class Description

✈️ ask Method Description

📋 History Class Description

📬 GrokResponse Class Description

🐧 Working with Linux

🌐 Running an OpenAI-Compatible Server


🖼️ Convenient Image Retrieval

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

client = GrokClient()
result = client.ask("Draw a sunset over the sea")

for i, image in enumerate(result.modelResponse.generatedImages):
    image.save_to(f"sunset_{i}.jpg")
    print(f"Saved: sunset_{i}.jpg 🌅")

📋 Response Handling

The ask method returns a GrokResponse object.

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]).

📬 Detailed GrokResponse Class Description


If something’s unclear, feel free to raise an issue — we’ll figure it out together! 🌟

Disclaimer

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grok3api-0.1.0rc2.tar.gz (27.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

grok3api-0.1.0rc2-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

Details for the file grok3api-0.1.0rc2.tar.gz.

File metadata

  • Download URL: grok3api-0.1.0rc2.tar.gz
  • Upload date:
  • Size: 27.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for grok3api-0.1.0rc2.tar.gz
Algorithm Hash digest
SHA256 c75162cfb0a35de6f4c6481884cc63f0c49081164e023c567e6f327c3a2d280a
MD5 1476a80c4a785f4f82cca9ced24568f2
BLAKE2b-256 0b9b401a12d824ca996727963395e402053ca72b422bf1b3371706397137ac7a

See more details on using hashes here.

File details

Details for the file grok3api-0.1.0rc2-py3-none-any.whl.

File metadata

  • Download URL: grok3api-0.1.0rc2-py3-none-any.whl
  • Upload date:
  • Size: 29.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for grok3api-0.1.0rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 fadbeef30ad1ee4c6a8d367ae1d12e9e1dc2176a511c372dc4eb225ebf479d68
MD5 4bc4e31e608c8ca978f88fa9f71cef7c
BLAKE2b-256 821ad65bbf6a163aa38bc50a4280d4267dc6a40826a26f73a20536a9b5fea31e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page