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: 10OptionalDefault:
The number of images to generate.
1response_formatstring · enumOptionalDefault:
The format in which the generated images are returned.
urlPossible values: Responses
201
Successfully generated image
application/json
post
/v1/images/generationsasync 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()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?