Applying for and Using the Grok Chat Completion API
xAI Grok is a powerful AI conversation system that can generate fluent, natural responses in just a few seconds with a prompt. Grok stands out in the industry for its distinctive sense of humor and ability to access real-time web information. Today, Grok has begun to make its mark in multiple innovative fields, and its influence is expanding rapidly. Whether for everyday conversation, creative writing, technical analysis, or code debugging, Grok can provide insightful, intelligent assistance, bringing a new dimension of support to users’ decisions and creative work.
This document primarily introduces the process of using the Grok Chat Completion API, which allows us to easily use the official Grok conversation features.
grok-4.7, grok-4.5, grok-4, and grok-3 also support the Anthropic Messages format. When using POST /v1/messages, send model, messages, and max_tokens, and read the text from the response’s content array. Messages Integration Guide
¶ Application Process
To use the Grok Chat Completion API, first go to the Ace Data Cloud Console to obtain your API Token for later use.

If you have not yet logged in or registered, you will be automatically redirected to the login page to register and log in. Once you have done so, you will automatically return to the current page.
A general-purpose application’s API Key can be used for services enabled for the account; a dedicated application’s Key can be used within that application’s service scope. Please check the Key’s permissions, validity period, and limits, and confirm that the application balance is sufficient to pay for this request. Available trial credit is subject to what is displayed in the account. If the balance is insufficient, click the “Recharge” button.
📘 Full documentation: Grok Chat Completion API →
¶ Basic Usage
Next, you can fill in the corresponding content in the interface, as shown below:

When using this endpoint for the first time, we need to fill in at least three items. One is authorization, which can be selected directly from the drop-down list. Another parameter is model, which is the category of the official Grok model we choose to use. There are mainly 8 models here; see the models we provide for details. The last parameter is messages. messages is the array of prompts we enter; it is an array, meaning that multiple prompts can be submitted at the same time. Each prompt contains role and content, where role indicates the role of the person asking the question. We provide three roles: user, assistant, and system. The other field, content, is the specific content of our question.
You may also notice that corresponding calling code is generated on the right. You can copy the code and run it directly, or click the “Try” button to test it.
Common optional parameters:
max_tokens: Limits the maximum number of tokens in a single response.temperature: Controls generation randomness, from 0 to 2; higher values produce more varied results.n: The number of candidate responses to generate at once.

After making the call, we find that the result is as follows:
{
"id": "foaicmpl-13936918-cb99-49e1-b94c-bde98b482ed4",
"model": "grok-4",
"object": "chat.completion",
"created": 1755839683,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Yo! What's up? 😎 Ready to dive into whatever you're pondering about today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 8,
"completion_tokens": 20,
"total_tokens": 28,
"prompt_tokens_details": {
"cached_tokens": 0,
"text_tokens": 0,
"audio_tokens": 0,
"image_tokens": 0
},
"completion_tokens_details": {
"text_tokens": 0,
"audio_tokens": 0,
"reasoning_tokens": 0
},
"input_tokens": 0,
"output_tokens": 0,
"input_tokens_details": null
}
}
The result contains several fields, described below:
id, the ID generated for this conversation task, used to uniquely identify it.model, the selected official Grok model.choices, the response information Grok provides for the prompt.usage: Token statistics for this question and answer.
The choices field contains Grok’s response information, and the choices inside it contains the specific details of Grok’s response, as shown in the figure.

As you can see, the content field inside choices contains the specific content of Grok’s response.
¶ Streaming Responses
This endpoint also supports streaming responses, which is very useful for web integration and allows a webpage to display text character by character.
To return a streaming response, you can change the stream parameter in the request header to true.
Make the change as shown below, although the calling code also needs corresponding changes to support streaming responses.

After changing stream to true, the API will return the corresponding JSON data line by line. We need to make corresponding changes to the code to obtain the results line by line.
Python sample calling code:
import requests
url = "https://api.acedata.cloud/grok/chat/completions"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "grok-3",
"messages": [{"role":"user","content":"Hello"}],
"stream": True
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
The output is as follows:
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"role": "assistant"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "Yo, "}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data:
{"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "what"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "'s g"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "ood?"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": " Rea"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "dy t"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "o di"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "ve i"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "nto "}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "what"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "ever"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": " you"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "'re "}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "pond"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "erin"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "g ab"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "out "}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "toda"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {"content": "y?"}, "logprobs": null, "finish_reason": null, "index": 0}], "usage": null}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": null, "choices": [{"delta": {}, "logprobs": null, "finish_reason": "stop", "index": 0}], "usage":
{"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0, "prompt_tokens_details": {"cached_tokens": 0, "text_tokens": 0, "audio_tokens": 0, "image_tokens": 0}, "completion_tokens_details": {"text_tokens": 0, "audio_tokens": 0, "reasoning_tokens": 0}, "input_tokens": 0, "output_tokens": 0, "input_tokens_details": null}}
data: {"id": "foaicmpl-503ab14f-3f22-46ab-9f91-3fb44773be38", "object": "chat.completion.chunk", "created": 1755839790, "model": "grok-3", "system_fingerprint": "", "choices": [], "usage": {"prompt_tokens": 8, "completion_tokens": 18, "total_tokens": 26, "prompt_tokens_details": {"cached_tokens": 0, "text_tokens": 0, "audio_tokens": 0, "image_tokens": 0}, "completion_tokens_details": {"text_tokens": 0, "audio_tokens": 0, "reasoning_tokens": 0}, "input_tokens": 0, "output_tokens": 0, "input_tokens_details": null}}
data: [DONE]
As you can see, the response contains many data entries, and the choices within data contain the latest response content, consistent with the content described above. choices contains newly generated response content, which you can integrate into your system based on the results. At the same time, the end of the streaming response is determined by the content of data: if the content is [DONE], it indicates that the streaming response has ended completely. The returned data results contain multiple fields, described below:
id, the ID generated for this conversation task, used to uniquely identify the task.model, the selected model from the official Grok website.choices, the response information Grok provides for the prompt.
JavaScript is also supported; for example, the Node.js streaming call code is as follows:
const options = {
method: "post",
headers: {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
},
body: JSON.stringify({
"model": "grok-3",
"messages": [{"role":"user","content":"Hello"}],
"stream": true
})
};
fetch("https://api.acedata.cloud/grok/chat/completions", options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
Java sample code:
JSONObject jsonObject = new JSONObject();
jsonObject.put("model", "grok-3");
jsonObject.put("messages", [{"role":"user","content":"Hello"}]);
jsonObject.put("stream", true);
MediaType mediaType = "application/json; charset=utf-8".toMediaType();
RequestBody body = jsonObject.toString().toRequestBody(mediaType);
Request request = new Request.Builder()
.url("https://api.acedata.cloud/grok/chat/completions")
.post(body)
.addHeader("accept", "application/json")
.addHeader("authorization", "Bearer {token}")
.addHeader("content-type", "application/json")
.build();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
System.out.print(response.body!!.string())
For other languages, you can adapt the code yourself; the principle is the same.
¶ Multi-Turn Conversations
If you want to integrate multi-turn conversation functionality, you need to submit multiple prompts in the messages field. A specific example of multiple prompts is shown below:

Python sample call code:
import requests
url = "https://api.acedata.cloud/grok/chat/completions"
headers = {
"accept": "application/json",
"authorization": "Bearer {token}",
"content-type": "application/json"
}
payload = {
"model": "grok-3",
"messages": [{"role":"user","content":"Hello"},{"role":"assistant","content":"What model are you?"},{"role":"user","content":"What did I just say?"}]
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
By submitting multiple prompts, you can easily implement multi-turn conversations and obtain a response like the following:
{
"id": "foaicmpl-984ebc53-76b3-4d33-b0e8-0307ab4965af",
"model": "grok-3",
"object": "chat.completion",
"created": 1755839996,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "You said, \"Hello.\""
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 27,
"completion_tokens": 6,
"total_tokens": 33,
"prompt_tokens_details": {
"cached_tokens": 0,
"text_tokens": 0,
"audio_tokens": 0,
"image_tokens": 0
},
"completion_tokens_details": {
"text_tokens": 0,
"audio_tokens": 0,
"reasoning_tokens": 0
},
"input_tokens": 0,
"output_tokens": 0,
"input_tokens_details": null
}
}
As you can see, the information contained in choices is consistent with that in the basic usage example. It contains the specific content of Grok's response to multiple conversation turns, allowing it to answer corresponding questions based on the content of those turns.
¶ Error Handling
When calling the API, if an error occurs, the API will return the corresponding error code and message. For example:
400 token_mismatched: Bad request, possibly due to missing or invalid parameters.400 api_not_implemented: Bad request, possibly due to missing or invalid parameters.401 invalid_token: Unauthorized, invalid or missing authorization token.429 too_many_requests: Too many requests, you have exceeded the rate limit.500 api_error: Internal server error, something went wrong on the server.
¶ Error Response Example
{
"success": false,
"error": {
"code": "api_error",
"message": "fetch failed"
},
"trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}
¶ Conclusion
Through this document, you have learned how to use the OpenAI Chat Completion API to easily implement the conversation functionality of the official OpenAI ChatGPT. We hope this document helps you better integrate and use the API. If you have any questions, please feel free to contact our technical support team.
