API ReferencexAI (Grok)
xAI (Grok)
xAI Grok API integration - Access Grok-2, Grok-1.5 through UniterRouter. OpenAI-compatible format for Elon Musk's AI with real-time knowledge and reasoning.
xAI (Grok) API
Access xAI's Grok models through UniterRouter using the OpenAI-compatible API format. Grok is known for its real-time knowledge and conversational abilities.
Quick Start
curl https://api.uniterouter.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $UNITEROUTER_API_KEY" \
-d '{
"model": "grok-2",
"messages": [
{"role": "user", "content": "What are the latest developments in AI?"}
]
}'from openai import OpenAI
client = OpenAI(
base_url="https://api.uniterouter.ai/v1",
api_key="your_uniterouter_api_key",
)
response = client.chat.completions.create(
model="grok-2",
messages=[
{"role": "user", "content": "What are the latest developments in AI?"}
]
)
print(response.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.uniterouter.ai/v1",
apiKey: process.env.UNITEROUTER_API_KEY,
});
const response = await client.chat.completions.create({
model: "grok-2",
messages: [
{ role: "user", content: "What are the latest developments in AI?" }
],
});
console.log(response.choices[0].message.content);Available Models
| Model | Description | Context | Best For |
|---|---|---|---|
grok-2 | Latest Grok model | 128K | General purpose, current events |
grok-2-vision | Vision-enabled Grok | 128K | Image understanding |
grok-beta | Beta features | 128K | Testing new capabilities |
Vision Support
Grok-2-Vision supports image understanding:
response = client.chat.completions.create(
model="grok-2-vision",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Describe this image"},
{
"type": "image_url",
"image_url": {"url": "https://example.com/image.jpg"}
}
]
}
]
)Function Calling
Grok supports tool use:
tools = [
{
"type": "function",
"function": {
"name": "search_web",
"description": "Search the web for current information",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"}
},
"required": ["query"]
}
}
}
]
response = client.chat.completions.create(
model="grok-2",
messages=[{"role": "user", "content": "What happened in tech news today?"}],
tools=tools,
tool_choice="auto"
)Streaming
stream = client.chat.completions.create(
model="grok-2",
messages=[{"role": "user", "content": "Tell me about SpaceX"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")Features
- Current Knowledge: Access to recent information
- Long Context: 128K token context window
- Vision: Image understanding capabilities
- Function Calling: Tool use support
- Conversational: Natural dialogue style
Related
- Chat Completions - Full API documentation
- Vision - Image input guide
How is this guide?