API / send-a-first-api-request

API

Send your first Grok API request

Send your first Grok API request

Text goes to POST https://api.x.ai/v1/responses. The model id xAI's quickstart uses today is grok-4.6. Get a key at console.x.ai and load the account with credits first.

Key and SDK

  1. Create an API key on the API Keys page in Console.
  2. Export it. Keep it out of chat logs and public repos.
export XAI_API_KEY="your_api_key"
  1. Install one client:
pip install xai-sdk

Minimal call

curl https://api.x.ai/v1/responses \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "grok-4.6",
    "input": "Fix this function and explain the bug: function median(a){a.sort();return a[a.length/2]}"
  }'

Python with the official SDK:

import os
from xai_sdk import Client
from xai_sdk.chat import user

client = Client(api_key=os.getenv("XAI_API_KEY"))

chat = client.chat.create(model="grok-4.6")
chat.append(user("Fix this function and explain the bug: function median(a){a.sort();return a[a.length/2]}"))

print(chat.sample().content)

OpenAI-compatible clients point base_url at https://api.x.ai/v1 and call client.responses.create with model and input.

After it works

  • Multi-turn, reasoning, and structured output: Text Generation Guide on docs.x.ai.
  • Images: grok-imagine-image-2.0 (see the Imagine stills how-to).
  • Coding agents: Grok Build docs.

Pitfalls

  • /v1/chat/completions is marked legacy. New work uses /v1/responses.
  • grok-4.6 has a February 1, 2026 knowledge cutoff. Turn on web search or X search tools when the answer has to be current.
  • SuperGrok's weekly pool and Console API credits are separate bills. A key with no Console balance returns an error even if grok.com still works.