API / understand-an-image-with-grok-api

API

Understand an image with the Grok API

Understand an image with the Grok API

Send an image as a content item with type: "input_image" and an image_url, then ask your question with type: "input_text". Call POST https://api.x.ai/v1/responses with model grok-4.6. Get a key and credits at console.x.ai.

Minimal call

curl https://api.x.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
  "model": "grok-4.6",
  "input": [
    {
      "role": "user",
      "content": [
        {
          "type": "input_image",
          "image_url": "https://science.nasa.gov/wp-content/uploads/2023/09/web-first-images-release.png",
          "detail": "high"
        },
        {
          "type": "input_text",
          "text": "What is in this image?"
        }
      ]
    }
  ]
}'

image_url can be a public URL or a data URI (data:image/jpeg;base64,...). Python with the official SDK:

import os
from xai_sdk import Client
from xai_sdk.chat import user, image

client = Client(api_key=os.getenv("XAI_API_KEY"))
image_url = "https://science.nasa.gov/wp-content/uploads/2023/09/web-first-images-release.png"

chat = client.chat.create(model="grok-4.6")
chat.append(
    user(
        "What's in this image?",
        image(image_url=image_url, detail="high"),
    )
)
print(chat.sample().content)

The response includes an id. Pass it on later turns to continue the conversation.

Limits

  • Max image size: 20 MiB.
  • Formats: jpg/jpeg or png only.
  • No limit on how many images you send in one request.
  • Image and text can appear in any order in content.

Pitfalls

  • When you send images, do not store request/response history on the server or the request may fail.
  • Stick to jpg/jpeg or png. Other formats are rejected.
  • Keep each file under 20 MiB.
  • Console API credits are a separate bill from SuperGrok's weekly pool on grok.com.