First Look at xAI’s Grok 2 API

First Look at xAI’s Grok 2 API

If you’ve been eagerly waiting to integrate Grok 3 into your project, I have some news: you’ll have to wait a little longer. But fear not! Grok 2 is still here, and it’s more than capable of handling your AI-powered needs. So, let’s walk through the signup process and how to get started with the Grok 2 API.

In this post, we’ll take a look at the Chat Completions API to see what benefits there are over the competition.

Signing Up for the GROK 2 API

Following the instructions outlined on the xAI documentation website it is relatively easy to sign up and a new account. Click on the ‘Quickstart‘ tutorial for more information, but the basic instructions are:

  • Create an xAI Account
  • Generate an API Key
  • Make your first request

I’m not on Elon’s mailing list, so when it came time to use the API, I didn’t have a promo code to use, so I was forced to put $10 towards his new baby Seldon’s college fund.

Making a request to the Chat Completion Endpoint

Once you’ve signed up and obtained an API key, you can start checking out the API.

Thankfully, HTTP requests for follow the same pattern as OpenAI, DeepSeek, Qwen and the rest, so it is very easy to test using any existing code samples you have.

The only changes required are to replace the API key with the one you just generated and use the Grok HTTP endpoint URLs. For example, the Chat Completions endpoint is:

Authentication

Authentication is simple using your API key as the Bearer token in an HTTP request using PostMan, or cURL.

curl -X POST https://api.x.ai/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "grok-2-latest",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user", 
            "content": "Who are you?"
        }
    ]
}'

The Chat Completion Endpoint

POST: https://api.x.ai/v1/chat/completions

Chat Completions Request

Send the JSON formatted request in the body via a POST to the completion URL.

{
    "model": "grok-2-latest",
    "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the average age of a university student in Australia?"}
    ],
    "stream": false
}

Chat Completion Response

    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "The average age of a university student in Australia can vary depending on the specific institution and program of study. However, according to the Australian Institute of Health and Welfare, the median age of students commencing a bachelor's degree in 2021 was around 19 years old. This reflects the typical age of students entering university directly after completing their secondary education. Keep in mind that mature-age students, who might be returning to study after some years in the workforce, can increase the average age in some programs or institutions.",
                "refusal": null
            },
            "finish_reason": "stop"
        }
    ],

As you can see from both the Request and Response object, the API is completely compatible with the OpenAI, DeepSeek and Qwen API.

Differences Between Grok, Qwen and DeepSeek

If you’re looking for an alternative to OpenAI, obviously Grok, Qwen and DeepSeek are worth considering. Your choice should align with your project’s specific requirements and budget. Some people also have concerns about security and data sovereignty issues when using the Chinese contenders, but if you’re not in that category, maybe it just comes down to price.

FeatureGrokQwenDeepSeek
DeveloperxAIAlibabaDeepSeek
Input Token Price$2.00 per million$0.79 per million$0.07–$0.27 per million
Output Token Price$10.00 per million$0.79 per million$1.10 per million
Key StrengthsReal-time personalization, deep reasoningMultilingual support, enterprise scalabilityCost-effective, open-source flexibility
Use CasesTechnical fields, codingEnterprise applicationsSpecialized tasks, customizable solutions

Final Thoughts

While we all anticipate GROK 3, GROK 2 remains a strong contender for AI-powered applications. Get started today, and when GROK 3 finally arrives, you’ll be ready to upgrade in no time.