This document provides examples of how to use the xAI Grok API endpoints with curl commands.
- An API key for xAI Grok API
- curl installed on your system
curl -X GET http://localhost:8000/healthExpected response:
{
"status": "healthy",
"version": "0.1.0"
}Note: Currently, according to xAI documentation, the parameters
quality,size, andstyleare not supported by the xAI API and will be ignored.
curl -X POST http://localhost:8000/api/v1/images/generate \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_XAI_API_KEY" \
-d '{
"prompt": "A beautiful sunset over mountains with a lake in the foreground",
"model": "grok-2-image",
"n": 1,
"response_format": "url"
}'Expected response:
{
"created": 1700000000,
"data": [
{
"url": "https://example.com/image-url.jpg",
"revised_prompt": "A beautiful sunset over mountains with a lake in the foreground"
}
],
"model": "grok-2-image"
}This endpoint is identical to the one above but matches the OpenAI SDK endpoint naming for compatibility:
curl -X POST http://localhost:8000/api/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_XAI_API_KEY" \
-d '{
"prompt": "A beautiful sunset over mountains with a lake in the foreground",
"model": "grok-2-image",
"n": 1
}'curl -X POST http://localhost:8000/api/v1/images/generate \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_XAI_API_KEY" \
-d '{
"prompt": "A futuristic city with flying cars and tall skyscrapers",
"model": "grok-2-image",
"n": 3,
"response_format": "url"
}'curl -X POST http://localhost:8000/api/v1/images/generate \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_XAI_API_KEY" \
-d '{
"prompt": "Bioluminescent jellyfish in the deep ocean",
"model": "grok-2-image",
"n": 1,
"response_format": "b64_json"
}'curl -X POST http://localhost:8000/api/v1/vision/analyze \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_XAI_API_KEY" \
-d '{
"model": "grok-2-vision-latest",
"image": {
"url": "https://api.time.com/wp-content/uploads/2017/11/dogs-cats-brain-study.jpg"
},
"prompt": "What animals are in this image and what are they doing?",
"detail": "high",
"temperature": 0.01,
"max_tokens": 1024
}'Expected response:
{
"model": "grok-2-vision-latest",
"created": 1700000000,
"content": "In this image, there is a dog and a cat sitting side by side on what appears to be a couch or soft surface. The dog is a brown and white Boxer or similar breed with a short coat, while the cat appears to be a gray tabby. Both animals are looking directly at the camera. They seem to be in a relaxed, calm state, simply sitting together in what looks like a domestic home environment.",
"usage": {
"prompt_tokens": 100,
"completion_tokens": 156,
"total_tokens": 256
}
}curl -X POST http://localhost:8000/api/v1/vision/analyze \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_XAI_API_KEY" \
-d '{
"model": "grok-2-vision-latest",
"image": {
"b64_json": "BASE64_ENCODED_IMAGE_DATA"
},
"prompt": "Describe this image in detail",
"detail": "high",
"max_tokens": 1024
}'Here's an example using the OpenAI SDK to analyze an image:
import os
from openai import OpenAI
XAI_API_KEY = os.getenv("XAI_API_KEY") # Or use your API key directly
image_url = "https://api.time.com/wp-content/uploads/2017/11/dogs-cats-brain-study.jpg"
# Initialize client with xAI base URL
client = OpenAI(
api_key=XAI_API_KEY,
base_url="http://localhost:8000/api/v1", # Point to your FastAPI server
)
messages = [
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": image_url,
"detail": "high",
},
},
{
"type": "text",
"text": "What's in this image?",
},
],
},
]
completion = client.chat.completions.create(
model="grok-2-vision-latest",
messages=messages,
temperature=0.01,
)
print(completion.choices[0].message.content)curl -X POST http://localhost:8000/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_XAI_API_KEY" \
-d '{
"model": "grok-4-1-fast-non-reasoning",
"messages": [
{"role": "user", "content": "What is the capital of France?"}
],
"temperature": 0.7
}'You can use streaming to receive responses as they're being generated:
curl -X POST http://localhost:8000/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_XAI_API_KEY" \
-d '{
"model": "grok-4-1-fast-non-reasoning",
"messages": [
{"role": "user", "content": "Write a short poem about the ocean"}
],
"stream": true,
"temperature": 0.7
}'For more detailed information about streaming, see the Streaming API documentation.
curl -X POST http://localhost:8000/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_XAI_API_KEY" \
-d '{
"model": "grok-4-1-fast-non-reasoning",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant specialized in explaining scientific concepts."
},
{
"role": "user",
"content": "What is quantum computing?"
},
{
"role": "assistant",
"content": "Quantum computing is a type of computing that uses quantum phenomena such as superposition and entanglement to perform operations on data."
},
{
"role": "user",
"content": "Can you explain superposition in simpler terms?"
}
],
"max_tokens": 1024,
"temperature": 0.5
}'curl -X POST http://localhost:8000/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_XAI_API_KEY" \
-d '{
"model": "grok-4-1-fast-non-reasoning",
"messages": [
{
"role": "user",
"content": "Write a short poem about artificial intelligence"
}
],
"max_tokens": 256,
"temperature": 0.2,
"top_p": 0.9
}'import requests
import json
def generate_image(prompt):
url = "http://localhost:8000/api/v1/images/generate"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_XAI_API_KEY"
}
data = {
"prompt": prompt,
"model": "grok-2-image",
"n": 1
}
response = requests.post(url, headers=headers, json=data)
return response.json()
image_result = generate_image("A surreal landscape with floating islands")
print(json.dumps(image_result, indent=2))async function analyzeImage(imageUrl) {
const response = await fetch('http://localhost:8000/api/v1/vision/analyze', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_XAI_API_KEY'
},
body: JSON.stringify({
model: 'grok-2-vision-latest',
image: {
url: imageUrl
},
max_tokens: 1024
})
});
return await response.json();
}
// Usage
analyzeImage('https://example.com/image.jpg')
.then(result => console.log(result))
.catch(error => console.error('Error:', error));You can also test these endpoints using Postman or similar API testing tools:
- Set the request type (GET/POST)
- Enter the endpoint URL
- Add request headers including:
- Content-Type: application/json
- Authorization: Bearer YOUR_XAI_API_KEY
- For POST requests, add the request body in JSON format
- Send the request and view the response