Chat
POST
https://api.x.ai/v1/chat/completions
Prerequisites
export XAI_API_KEY="your_api_key"
A Basic Chat Completions Example
Conversations
{
"role": "system",
"content": [{ "type": "text", "text": "You are a helpful and funny assistant."}]
}
{
"role": "user",
"content": [{ "type": "text", "text": "Why don't eggs tell jokes?" }]
},
{
"role": "assistant",
"content": [{ "type": "text", "text": "They'd crack up!" }]
},
{
"role": "user",
"content": [{"type": "text", "text": "Can you explain the joke?"}],
}
system
role content should define, in an instructive tone, the way the model should respond to user request. The user
role content is usually used for user request or data sent to the model. The assistant
role content is usually either in the model's response, or when sent within the prompt, indicating the model's response as part of conversation history.assistant
role content can be used within function calling, in which the model response will invoke a tool call, the user's program responds to the tool call and continues the conversation by appending tool call result to the message. For more details, check out our guide on Function Calling.Message role order flexibility
system
, user
, or assistant
roles in any sequence for your conversation context.[
{"role": "system", "content": "..."},
{"role": "system", "content": "..."},
{"role": "user", "content": "..."},
{"role": "user", "content": "..."}
]
{
"role": "user",
"content": "..."
},
{
"role": "user",
"content": "..."
},
{
"role": "system",
"content": "..."
}
Request Request Example
Shell
JavaScript
Java
Swift
curl --location --request POST 'https://api.x.ai/v1/chat/completions' \
--header 'Authorization: Bearer ' \
--header 'Content-Type: application/json' \
--data-raw '{
"messages": [
{
"role": "system",
"content": "You are Grok, a chatbot inspired by the Hitchhikers Guide to the Galaxy."
},
{
"role": "user",
"content": "What is the meaning of life, the universe, and everything?"
}
],
"model": "grok-3-latest",
"stream": false,
"temperature": 0
}'
Response Response Example
{
"id": "0daf962f-a275-4a3c-839a-047854645532",
"object": "chat.completion",
"created": 1739301120,
"model": "grok-3-latest",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The meaning of life, the universe, and everything is a question that has puzzled philosophers, scientists, and hitchhikers alike. According to the Hitchhiker's Guide to the Galaxy, the answer to this ultimate question is simply "42". However, the exact nature of the question itself remains unknown. So, while we may have the answer, the true meaning behind it is still up for debate. In the meantime, perhaps we should all just enjoy the journey and have a good laugh along the way!",
"refusal": null
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 41,
"completion_tokens": 104,
"total_tokens": 145,
"prompt_tokens_details": {
"text_tokens": 41,
"audio_tokens": 0,
"image_tokens": 0,
"cached_tokens": 0
}
},
"system_fingerprint": "fp_84ff176447"
}
Request
Header Params
Content-Type
string
required
Example:
application/json
Authorization
string
required
Example:
Bearer {{XAI_API_KEY}}
Body Params application/json
Responses
Modified at 2025-04-10 08:43:26