Uncategorized

How to use the Grok Rest API with images (Example)

The Grok API, by xAI, is an AI platform designed for natural language processing and, more recently, multi-modal tasks including image understanding.

In this article, we show how to get setup with Grok and perform your first REST API call, asking Grok to describe an image.

How to Get Your Grok API Key

To begin, you’ll need an API key from xAI.
Visit console.x.ai to sign up and generate your key.

create a grok api key

Top up your Grok account in The X.AI Console

You only need to add around $5 / £5 to get started
Add pre-paid credits to your account.

Install Postman

Postman is useful for testing API calls before incorporating them into your script or application.
Ensure Postman is installed, available at postman.com.

How do I make the Grok REST API call?

In postman, create a new request:

TypePOST
URLhttps://api.x.ai/v1/chat/completions
HeadersContent-type application/json
Authorization Bearer APIKEYHERE

Body:

{
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful AI assistant created by xAI."
    },
    {
      "role": "user",
      "content": [
        {
          "type": "image_url",
          "image_url": {
            "url": "https://virtualg.uk/wp-content/uploads/2024/06/image-1-1.jpg",
            "detail": "high"
          }
        },
        {
          "type": "text",
          "text": "Provide a concise description of the technology featured in this image"
        }
      ]
    }
  ],
  "model": "grok-2-vision-latest",
  "temperature": 0.5
}
sample grok api rest request in postman

Hit Send

Inspecting the Grok Response

Within the request you should see a choices variable, containing “content” as your response:

“The image features a Raspberry Pi setup with an attached touchscreen display, encased in a protective frame. The display shows a Linux-based operating system, likely running on the Raspberry Pi.”

sample grok api output
input grok image

Final Thoughts

That’s it, once you know the syntax it’s really easy to integrate Grok into your script / app! Enjoy!

author avatar
Graham
Graham works closely with VMware & Microsoft solutions. He is a VMware Certified Implementation Expert, 8x VMware vExpert & a VMware User Moderator on the official VMware VMTN forums. Feel free to reach out via Twitter @VirtualG.uk or email: contact@virtualg.uk

Leave a Response

This site uses Akismet to reduce spam. Learn how your comment data is processed.