中文

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.grok.cadn.net.cn

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>.grok.cadn.net.cn


/v1/chat/completionsgrok.cadn.net.cn

Create a chat response from text/image chat prompts. This is the endpoint for making requests to chat and image understanding models.grok.cadn.net.cn

Request body

A list of messages that make up the the chat conversation. Different models support different message types, such as image and text.grok.cadn.net.cn

Model name for the model to use.grok.cadn.net.cn

POSTgrok.cadn.net.cn

/v1/chat/completionsgrok.cadn.net.cn

{
  "messages": [
    {
      "role": "system",
      "content": "You're an assistant"
    },
    {
      "role": "user",
      "content": "Hi"
    }
  ],
  "model": "grok-2-latest"
}
{
  "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"
}

/v1/messagesgrok.cadn.net.cn

Create a messages response. This endpoint is compatible with the Anthropic API.grok.cadn.net.cn

Request body

The maximum number of tokens to generate before stopping. The model may stop before the max_tokens when it reaches the stop sequence.grok.cadn.net.cn

Model name for the model to use.grok.cadn.net.cn

{
  "model": "grok-2-latest",
  "max_tokens": 32,
  "messages": [
    {
      "role": "user",
      "content": "Hello, world"
    }
  ]
}
{
  "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
  }
}

/v1/api-keygrok.cadn.net.cn

Get information about an API key, including name, status, permissions and users who created or modified this key.grok.cadn.net.cn

No parameters.
{
  "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
}

/v1/modelsgrok.cadn.net.cn

List all models available to the authenticating API key with minimalized information, including model names (ID), creation times, etc.grok.cadn.net.cn

No parameters.
{
  "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"
}

/v1/models/{model_id}grok.cadn.net.cn

Get minimalized information about a model with its model_id.grok.cadn.net.cn

Path parameters

ID of the model to get.grok.cadn.net.cn

GETgrok.cadn.net.cn

/v1/models/{model_id}grok.cadn.net.cn

No parameters.
{
  "id": "grok-2-latest",
  "created": 1726444800,
  "object": "model",
  "owned_by": "xai"
}

/v1/language-modelsgrok.cadn.net.cn

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).grok.cadn.net.cn

GETgrok.cadn.net.cn

/v1/language-modelsgrok.cadn.net.cn

No parameters.
{
  "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": []
    }
  ]
}

/v1/language-models/{model_id}grok.cadn.net.cn

Get full information about a chat or image understanding model with its model_id.grok.cadn.net.cn

Path parameters

ID of the model to get.grok.cadn.net.cn

GETgrok.cadn.net.cn

/v1/language-models/{model_id}grok.cadn.net.cn

No parameters.
{
  "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"
  ]
}

/v1/tokenize-textgrok.cadn.net.cn

Tokenize text with the specified modelgrok.cadn.net.cn

Request body

The model to tokenize with.grok.cadn.net.cn

The text content to be tokenized.grok.cadn.net.cn

{
  "text": "Hello world!",
  "model": "grok-2-latest"
}
{
  "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
      ]
    }
  ]
}

/v1/completionsgrok.cadn.net.cn

(Legacy) Create a text completion response for a given prompt. Replaced by /v1/chat/completionsgrok.cadn.net.cn

Request body

Specifies the model to be used for the request.grok.cadn.net.cn

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.grok.cadn.net.cn

{
  "prompt": "1, 2, 3, 4, ",
  "model": "grok-2-latest",
  "max_tokens": 3
}
{
  "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"
}

/v1/completegrok.cadn.net.cn

(Legacy) Create a text completion response. This endpoint is compatible with the Anthropic API.grok.cadn.net.cn

Request body

max_tokens_to_samplegrok.cadn.net.cn

integergrok.cadn.net.cn

requiredgrok.cadn.net.cn

The maximum number of tokens to generate before stopping.grok.cadn.net.cn

Model to use for completion.grok.cadn.net.cn

Prompt for the model to perform completion on.grok.cadn.net.cn

{
  "model": "grok-2-latest",
  "max_tokens_to_sample": 8,
  "temperature": 0.1,
  "prompt": "\n\nHuman: Hello, how are you?\n\nAssistant:"
}
{
  "type": "completion",
  "id": "8d3e45c6-f882-4d40-bb4a-54c6af166e18",
  "completion": "Hello there! I'm Grok",
  "stop_reason": "max_tokens",
  "model": "grok-2-latest"
}