Skip to main content
Lesson 4 of 14 · Grok API quickstart

The API base URL

Lesson 4 of 14 Updated July 30, 2026 Developer
On this page
  1. The Grok API entry point
  2. The base URL
  3. Compatibility with the OpenAI ecosystem
  4. The main endpoints
  5. Set the URL in your SDKs
  6. Test connectivity
  7. Limits and quotas
  8. Key takeaways

The Grok API entry point

Every request to the xAI API goes through a single base URL. Understanding that URL and the available endpoints is essential before you write your first line of code.

The base URL

The Grok API lives at this address:

https://api.x.ai/v1

This URL is the root for every endpoint. For example, to create a response, the full URL is https://api.x.ai/v1/responses.

Why /v1?

The /v1 prefix indicates the API version. Versioning lets xAI evolve the API without breaking existing applications. If a version 2 comes along, it will coexist with v1 during a transition period.

Compatibility with the OpenAI ecosystem

A fundamental feature of the xAI API is its compatibility with the OpenAI format. In practice, if you already have code that works with the OpenAI API, you only need to change two settings to use Grok:

  • The base URL: replace https://api.openai.com/v1 with https://api.x.ai/v1
  • The API key: replace your OpenAI key with your xAI key

That compatibility extends to most tools and libraries in the OpenAI ecosystem: LangChain, LlamaIndex, LiteLLM, and many others work with Grok without code changes.

The main endpoints

The API exposes several endpoints grouped by feature.

Text generation

  • POST /v1/responses: the main endpoint for generating responses (modern format)
  • POST /v1/chat/completions: the OpenAI-compatible endpoint (legacy format, still supported)

Media generation

  • POST /v1/images/generations: generate images
  • POST /v1/images/edits: edit existing images
  • POST /v1/videos/generations: generate videos
  • POST /v1/tts: speech synthesis (text-to-speech)

File management

  • POST /v1/files: upload a file
  • GET /v1/files: list uploaded files

Real time

  • wss://api.x.ai/v1/realtime: voice agent over WebSocket

Set the URL in your SDKs

When you use an OpenAI-compatible SDK, you have to specify the base URL explicitly:

from openai import OpenAI

client = OpenAI(
    api_key="xai-your-key",
    base_url="https://api.x.ai/v1"
)

With the native xAI SDK, the URL is preconfigured and you don’t need to set anything.

Test connectivity

Before you write any code, a direct call validates the whole chain (URL, key, network):

curl https://api.x.ai/v1/models \
  -H "Authorization: Bearer xai-your-key"

The response lists the models available with your key. It’s also the most reliable way to learn the exact model IDs available on your account, without depending on documentation that may change. Keep this habit: validate every new environment (machine, CI, server) with this same call before any deployment.

Limits and quotas

The API enforces rate limits for each team and each model. If you exceed them, you get an HTTP 429 error. The xAI console shows your current quotas and your usage in real time.

Key takeaways

  • The base URL is https://api.x.ai/v1 for every endpoint
  • The API is compatible with the OpenAI format: change the URL and the key, and that’s it
  • The main generation endpoint is POST /v1/responses
  • The legacy POST /v1/chat/completions endpoint remains available for compatibility