The Grok API by xAI offers a powerful suite of RESTful endpoints for building advanced AI applications. Designed for compatibility with OpenAI and Anthropic formats, the Grok API supports chat, completion, function calling, embeddings, vision processing, and image generation—all with a massive token context window and real-time reasoning capabilities.
Whether you're creating chatbots, coding assistants, or multimodal search tools, understanding the Grok API endpoints is crucial for effective implementation.
These endpoints form the foundation of Grok 4’s integration capabilities:
Endpoint | Method | Description |
---|---|---|
/v1/chat/completions |
POST | Generates structured, chat-style responses with support for text, reasoning, and function calling |
/v1/completions |
POST | Generates standard text completions (non-chat) |
/v1/models |
GET | Lists all available Grok models |
/v1/models/{model_id} |
GET | Fetches detailed information about a specific model |
/v1/embeddings |
POST | Generates text embeddings for search, similarity, and retrieval tasks |
/v1/images/generate |
POST | Generates images from natural language prompts |
/v1/vision/analyze |
POST | Analyzes image content using vision models |
/v1/tokenize |
POST | Tokenizes input text for usage estimation |
/v1/health |
GET | Checks the health/status of the API service |
Base URL:
https://api.x.ai/v1/
For flexibility and ecosystem integration, Grok 4 is also accessible through popular API proxy platforms:
Endpoint: https://openrouter.ai/api/v1/chat/completions
Model ID: "x-ai/grok-4"
Endpoint: https://api.cometapi.com/v1/chat/completions
Model ID: "grok-4"
Endpoint: https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/grok
Enables enterprise-grade deployments through Cloudflare edge infrastructure.
Capability | Description |
---|---|
Chat Completions | Interactive AI conversations, memory, coding, and advanced logic |
Function Calling | Call external APIs, perform real-time tool use, automate workflows |
Vision & Image | Image understanding and generation (in rollout stages) |
Structured Outputs | Return results in machine-readable formats like JSON or tables |
Embeddings | Create vector representations for semantic search or recommendation engines |
Tokenization | Analyze token count to optimize cost and prevent request failures |
Model Discovery | Fetch supported models and their capabilities dynamically |
Here’s how to use the most common endpoint:
YOUR_API_KEY
with your valid API key
model
with your selected Grok model
Authentication: All endpoints require a Bearer token in the Authorization header.
Compatibility: Grok API is structured similarly to OpenAI and Anthropic APIs, making migration easy.
Rollout: Some capabilities (e.g., vision, image generation) may be in limited release or preview.
Rate Limits: Vary based on subscription tier—check your xAI dashboard or provider.
Category | Key Endpoint(s) |
---|---|
Chat & Completion | /v1/chat/completions , /v1/completions |
Model Management | /v1/models , /v1/models/{model_id} |
Embeddings | /v1/embeddings |
Vision/Image | /v1/images/generate , /v1/vision/analyze |
Token Management | /v1/tokenize |
Third-Party Proxies | OpenRouter, CometAPI, Cloudflare Gateway |
The Grok API provides a set of specialized endpoints for handling both text and image data:
/v1/chat/completions
: Primary endpoint for chat-based reasoning, dialogue, and agent tasks.
/v1/completions
: Used for traditional (non-chat) language generation.
/v1/embeddings
: Converts text into vector embeddings for search and semantic analysis.
/v1/tokenize
: Analyzes token usage for input optimization.
/v1/images/generate
: Creates images based on text prompts (image generation).
/v1/vision/analyze
: Interprets and analyzes uploaded images (vision model).
These endpoints enable both multimodal AI tasks and standard NLP workflows, making Grok 4 a versatile tool for developers.
Grok 4 supports advanced reasoning and structured output formatting through the /v1/chat/completions
endpoint.
To enable reasoning: Provide multi-turn messages with complex prompts. Grok can remember context and solve problems step-by-step.
To request structured outputs: Format your prompt to specify the desired structure (e.g., “Return this in JSON format”). Grok will respond in a predictable machine-readable format.
Example Prompt:
This capability is ideal for:
Report generation
AI agents that interface with APIs
To make a chat or image request using the Grok API:
Include your API key in the Authorization header:
For chat: https://api.x.ai/v1/chat/completions
For image generation: https://api.x.ai/v1/images/generate
Example chat payload:
Example image payload:
Use tools like curl
, Python requests
, or JavaScript fetch
to send the POST request with headers and body.
Grok 4's function calling feature lets you define external tools (e.g., APIs, code functions) and invoke them directly within the chat completion flow.
You include a list of callable functions (with parameters and schema) in the request.
Grok decides when to call them based on user input.
Once the function is invoked, your backend handles the request and returns a response to continue the chat.
Example use cases:
Real-time data fetching (weather, stock prices)
Database querying
Code execution or translation
Task automation (e.g., email, calendar, webhooks)
This feature makes Grok 4 agent-ready, bridging natural language and executable APIs.
Here’s how Grok 4 improves over Grok 3 and 3-mini in terms of endpoints:
Feature | Grok 3 / 3-mini | Grok 4 |
---|---|---|
Chat Completions | ✅ | ✅ (Improved reasoning) |
Image Generation | ❌ | ✅ (Rolling out) |
Vision Analysis | ❌ | ✅ (Rolling out) |
Function Calling | ❌ (Limited or none) | ✅ Native tool use |
Structured Output | ✅ (basic) | ✅ (JSON, tables, schema aware) |
3rd-Party Integration | Basic OpenRouter | Full support (Cloudflare, CometAPI, etc.) |
Context Window | 131,072 tokens | 256,000 tokens |
Grok 4 dramatically expands the breadth and depth of available endpoints, especially for multimodal tasks and agentic automation.
The Grok API endpoints provide a robust, flexible, and scalable interface for developers building cutting-edge AI applications. From natural language reasoning to image analysis and multimodal AI, Grok 4 supports a wide range of use cases with familiar API patterns and high context capacity.