中文

Getting Started

The Hitchhiker's Guide to Grok

Welcome! In this guide, we'll walk you through the basics of using the xAI API.grok.cadn.net.cn


You need an xAI account to access xAI API. Sign up for an account here.grok.cadn.net.cn

Once you've created an account, you'll need to load it with credits to start using the API.grok.cadn.net.cn

Create an API key via the API Keys Page in the xAI API Console.grok.cadn.net.cn

After generating an API key, we need to save it somewhere safe! We recommend you export it as an environment variable in your terminal.grok.cadn.net.cn

export XAI_API_KEY="your_api_key"

With your xAI API key exported as an environment variable, you're ready to make your first API request.grok.cadn.net.cn

Let's test out the API using curl. Paste the following directly into your terminal.grok.cadn.net.cn

curl https://api.x.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
    "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-2-latest",
    "stream": false,
    "temperature": 0
}'

Our API is fully compatible with the OpenAI and Anthropic SDKs. For example, we can make the same request from Python or Javascript like so:grok.cadn.net.cn

# In your terminal, first run:
# pip install openai

import os
from openai import OpenAI

XAI_API_KEY = os.getenv("XAI_API_KEY")
client = OpenAI(
    api_key=XAI_API_KEY,
    base_url="https://api.x.ai/v1",
)

completion = client.chat.completions.create(
    model="grok-2-latest",
    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?"
        },
    ],
)

print(completion.choices[0].message.content)

For an in-depth guide about using Grok for text responses, check out our Chat Guide.grok.cadn.net.cn

Certain grok models can accept both text AND images as an input. For example:grok.cadn.net.cn

import os
from openai import OpenAI

XAI_API_KEY = os.getenv("XAI_API_KEY")
image_url = "https://science.nasa.gov/wp-content/uploads/2023/09/web-first-images-release.png"

client = OpenAI(
    api_key=XAI_API_KEY,
    base_url="https://api.x.ai/v1",
)

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "image_url",
                "image_url": {
                    "url": image_url,
                    "detail": "high",
                },
            },
            {
                "type": "text",
                "text": "What's in this image?",
            },
        ],
    },
]

completion = client.chat.completions.create(
    model="grok-2-vision-latest",
    messages=messages,
    temperature=0.01,
)
print(completion.choices[0].message.content)

And voila! Grok will tell you exactly what's in the image:grok.cadn.net.cn

This image is a photograph of a region in space, specifically a part of the Carina Nebula, captured by the James Webb Space Telescope. It showcases a stunning view of interstellar gas and dust, illuminated by young, hot stars. The bright points of light are stars, and the colorful clouds are composed of various gases and dust particles. The image highlights the intricate details and beauty of star formation within a nebula.grok.cadn.net.cn

To learn how to use Grok vision for more advanced use cases, check out our Image Understanding Guide.grok.cadn.net.cn


As you use your API key, you will be charged for the number of tokens used. For an overview, you can monitor your usage on the xAI Console Usage Page.grok.cadn.net.cn

If you want a more granular, per request usage tracking, the API response includes a usage object that provides detail on prompt (input) and completion (output) token usage.grok.cadn.net.cn

"usage": {
  "prompt_tokens": 41,
  "completion_tokens": 87,
  "total_tokens": 128,
  "prompt_tokens_details": {
    "text_tokens": 41,
    "audio_tokens": 0,
    "image_tokens": 0,
    "cached_tokens": 0
  }
}

If you send requests too frequently or with long prompts, you might run into rate limits and get an error response. For more information, read Consumption and Rate Limits.grok.cadn.net.cn


Now you have learned the basics of making an inference on xAI API. Check out Models page to start building with one of our latest models.grok.cadn.net.cn