Grok 2 Image

This documentation is valid for the following list of our models:

  • x-ai/grok-2-image

Model Overview

xAI’s flagship image generation model as of summer 2025, producing photorealistic visuals from text prompts.

Setup your API Key

If you don’t have an API key for the AI/ML API yet, feel free to use our Quickstart guide.

API Schema

post
Authorizations
Body
modelundefined · enumRequiredPossible values:
promptstringRequired

The text prompt describing the content, style, or composition of the image to be generated.

nnumber · min: 1 · max: 10Optional

The number of images to generate.

Default: 1
response_formatstring · enumOptional

The format in which the generated images are returned.

Default: urlPossible values:
Responses
201

Successfully generated image

application/json
post
/v1/images/generations
async function main() {
  const response = await fetch('https://api.aimlapi.com/v1/images/generations', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer <YOUR_AIMLAPI_KEY>',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      model: 'x-ai/grok-2-image',
      prompt: 'A T-Rex relaxing on a beach, lying on a sun lounger and wearing sunglasses.',
    }),
  });

  const data = await response.json();
  console.log(JSON.stringify(data, null, 2));
}

main();
201

Successfully generated image

{
  "data": [
    {
      "url": "text",
      "b64_json": "text"
    }
  ]
}

Quick Example

Let's generate an image of the specified size using a simple prompt.

import requests
import json  # for getting a structured output with indentation

def main():
    response = requests.post(
        "https://api.aimlapi.com/v1/images/generations",
        headers={
            # Insert your AIML API Key instead of <YOUR_AIMLAPI_KEY>:
            "Authorization": "Bearer <YOUR_AIMLAPI_KEY>",
            "Content-Type": "application/json",
        },
        json={
            "model": "x-ai/grok-2-image",
            "prompt": "A T-Rex relaxing on a beach, lying on a sun lounger and wearing sunglasses."
        }
    )

    data = response.json()
    print(json.dumps(data, indent=2, ensure_ascii=False))

if __name__ == "__main__":
    main()
Response
{
  "data": [
    {
      "url": "https://cdn.aimlapi.com/xolmis/xai-imgen/xai-tmp-imgen-81fc6308-29a8-46c8-8d5a-16060c0724e8.jpeg",
      "revised_prompt": "A high-resolution photograph of a T-Rex relaxing on a beach, lying on a sun lounger and wearing sunglasses. The T-Rex is facing slightly to the right, with its sunglasses clearly visible. The background features a calm ocean and a few palm trees, set during the day with natural, soft lighting. The beach is relatively empty, focusing attention on the T-Rex. There are no distracting elements like birds or other animals, ensuring the T-Rex remains the central figure in the composition. The overall mood is serene and tranquil, emphasizing the unusual yet peaceful scene."
    }
  ],
  "meta": {
    "usage": {
      "tokens_used": 147000
    }
  }
}

We obtained the following 720x960 image by running this code example:

"A T-Rex relaxing on a beach, lying on a sun lounger and wearing sunglasses."

Last updated

Was this helpful?