REST API
The xAI Enterprise API is a robust, high-performance RESTful interface designed for seamless integration into existing systems. It offers advanced AI capabilities with full compatibility with the OpenAI REST API.
The base for all routes is at https://api.x.ai
. For all routes, you have to authenticate with the header Authorization: Bearer <your xAI API key>
.
Chat completions
/v1/chat/completions
Create a chat response from text/image chat prompts. This is the endpoint for making requests to chat and image understanding models.
Request body
messages
array
required
A list of messages that make up the the chat conversation. Different models support different message types, such as image and text.
model
string
required
Model name for the model to use.
POST
/v1/chat/completions
{
"messages": [
{
"role": "system",
"content": "You're an assistant"
},
{
"role": "user",
"content": "Hi"
}
],
"model": "grok-2-latest"
}
200
Response
{
"id": "c6f2d009-77ca-40d9-9de5-6d19716e1b4d",
"object": "chat.completion",
"created": 1728646283,
"model": "grok-2-latest",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I assist you today?",
"refusal": null
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 10,
"total_tokens": 25,
"prompt_tokens_details": {
"text_tokens": 15,
"audio_tokens": 0,
"image_tokens": 0,
"cached_tokens": 0
}
},
"system_fingerprint": "fp_9877325691"
}
Messages (Anthropic compatible)
/v1/messages
Create a messages response. This endpoint is compatible with the Anthropic API.
Request body
max_tokens
integer
required
The maximum number of tokens to generate before stopping. The model may stop before the max_tokens when it reaches the stop sequence.
messages
array
required
Input messages.
model
string
required
Model name for the model to use.
POST
/v1/messages
{
"model": "grok-2-latest",
"max_tokens": 32,
"messages": [
{
"role": "user",
"content": "Hello, world"
}
]
}
200
Response
{
"id": "107baefc-993f-4632-b504-3f0c90d089aa",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello! How can I assist you today?"
}
],
"model": "grok-2-latest",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 9,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0,
"output_tokens": 10
}
}
API key
/v1/api-key
Get information about an API key, including name, status, permissions and users who created or modified this key.
GET
/v1/api-key
No parameters.
200
Response
{
"redacted_api_key": "xai-...b14o",
"user_id": "59fbe5f2-040b-46d5-8325-868bb8f23eb2",
"name": "My API Key",
"create_time": "2024-01-01T12:55:18.139305Z",
"modify_time": "2024-08-28T17:20:12.343321Z",
"modified_by": "3d38b4dc-4eb7-4785-ae26-c3fa8997ffc7",
"team_id": "5ea6f6bd-7815-4b8a-9135-28b2d7ba6722",
"acls": [
"api-key:model:*",
"api-key:endpoint:*"
],
"api_key_id": "ae1e1841-4326-4b36-a8a9-8a1a7237db11",
"team_blocked": false,
"api_key_blocked": false,
"api_key_disabled": false
}
List models
/v1/models
List all models available to the authenticating API key with minimalized information, including model names (ID), creation times, etc.
GET
/v1/models
No parameters.
200
Response
{
"data": [
{
"id": "grok-2-1212",
"created": 1733961600,
"object": "model",
"owned_by": "xai"
},
{
"id": "grok-2-vision-1212",
"created": 1733961600,
"object": "model",
"owned_by": "xai"
}
],
"object": "list"
}
Get model
/v1/models/{model_id}
Get minimalized information about a model with its model_id.
Path parameters
model_id
string
ID of the model to get.
GET
/v1/models/{model_id}
No parameters.
200
Response
{
"id": "grok-2-latest",
"created": 1726444800,
"object": "model",
"owned_by": "xai"
}
List language models
/v1/language-models
List all chat and image understanding models available to the authenticating API key with full information. Additional information compared to /v1/models includes modalities, pricing, fingerprint and alias(es).
GET
/v1/language-models
No parameters.
200
Response
{
"models": [
{
"id": "grok-2-1212",
"fingerprint": "fp_1a5ab39b2d",
"created": 1733961600,
"object": "model",
"owned_by": "xai",
"version": "1.0.0",
"input_modalities": [
"text"
],
"output_modalities": [
"text"
],
"prompt_text_token_price": 20000,
"prompt_image_token_price": 0,
"completion_text_token_price": 100000,
"aliases": [
"grok-2",
"grok-2-latest"
]
},
{
"id": "grok-2-vision-1212",
"fingerprint": "fp_daba7546e5",
"created": 1733961600,
"object": "model",
"owned_by": "xai",
"version": "0.1.0",
"input_modalities": [
"text",
"image"
],
"output_modalities": [
"text"
],
"prompt_text_token_price": 20000,
"prompt_image_token_price": 20000,
"completion_text_token_price": 100000,
"aliases": []
}
]
}
Get language model
/v1/language-models/{model_id}
Get full information about a chat or image understanding model with its model_id.
Path parameters
model_id
string
ID of the model to get.
GET
/v1/language-models/{model_id}
No parameters.
200
Response
{
"id": "grok-2-latest",
"created": 1726444800,
"object": "model",
"owned_by": "xai",
"version": "1.0.0",
"input_modalities": [
"text"
],
"output_modalities": [
"text"
],
"prompt_text_token_price": 20000,
"prompt_image_token_price": 0,
"completion_text_token_price": 100000,
"aliases": [
"grok-2",
"grok-2-latest"
]
}
Tokenize text
/v1/tokenize-text
Tokenize text with the specified model
Request body
model
string
required
The model to tokenize with.
text
string
required
The text content to be tokenized.
POST
/v1/tokenize-text
{
"text": "Hello world!",
"model": "grok-2-latest"
}
200
Response
{
"token_ids": [
{
"token_id": 13902,
"string_token": "Hello",
"token_bytes": [
72,
101,
108,
108,
111
]
},
{
"token_id": 1749,
"string_token": " world",
"token_bytes": [
32,
119,
111,
114,
108,
100
]
},
{
"token_id": 161,
"string_token": "!",
"token_bytes": [
33
]
}
]
}
Completions (legacy)
/v1/completions
(Legacy) Create a text completion response for a given prompt. Replaced by /v1/chat/completions
Request body
model
string
required
Specifies the model to be used for the request.
prompt
string | array
required
Input for generating completions, which can be a string, list of strings, token list, or list of token lists. `<|endoftext|>` is used as a document separator, implying a new context start if omitted.
POST
/v1/completions
{
"prompt": "1, 2, 3, 4, ",
"model": "grok-2-latest",
"max_tokens": 3
}
200
Response
{
"id": "3a34a6a3-82b2-46d9-874d-99dbca084813",
"object": "text_completion",
"created": 1728652460,
"model": "grok-2-latest",
"choices": [
{
"index": 0,
"text": "5, ",
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 12,
"completion_tokens": 3,
"total_tokens": 15
},
"system_fingerprint": "fp_8933231290"
}
Completions (Anthropic compatible - legacy)
/v1/complete
(Legacy) Create a text completion response. This endpoint is compatible with the Anthropic API.
Request body
max_tokens_to_sample
integer
required
The maximum number of tokens to generate before stopping.
model
string
required
Model to use for completion.
prompt
string
required
Prompt for the model to perform completion on.
POST
/v1/complete
{
"model": "grok-2-latest",
"max_tokens_to_sample": 8,
"temperature": 0.1,
"prompt": "\n\nHuman: Hello, how are you?\n\nAssistant:"
}
200
Response
{
"type": "completion",
"id": "8d3e45c6-f882-4d40-bb4a-54c6af166e18",
"completion": "Hello there! I'm Grok",
"stop_reason": "max_tokens",
"model": "grok-2-latest"
}