How to Use Grok 3 API: Complete Developer Guide (2026)
Set up and integrate xAI's Grok 3 API in your applications
Start Building with Hypereal
Access Kling, Flux, Sora, Veo & more through a single API. Free credits to start, scale to millions.
No credit card required • 100k+ developers • Enterprise ready
How to Use the Grok 3 API: Complete Developer Guide for 2026
xAI's Grok 3 is one of the most capable large language models available in 2026, known for its strong reasoning abilities, large context window, and a distinctive approach to content policies. The Grok 3 API gives developers programmatic access to this model for building applications, chatbots, analysis tools, and more.
This guide covers everything from getting your API key to making your first request, with code examples in Python, JavaScript, and cURL.
What Is the Grok 3 API?
The Grok 3 API is a RESTful API provided by xAI that follows the OpenAI-compatible chat completions format. If you have used the OpenAI API, the Grok API will feel immediately familiar since it uses the same request and response structure.
| Feature | Details |
|---|---|
| Provider | xAI |
| Base URL | https://api.x.ai/v1 |
| API Format | OpenAI-compatible (chat completions) |
| Models Available | grok-3, grok-3-mini, grok-3-fast |
| Context Window | 131,072 tokens |
| Max Output | 32,768 tokens |
| Multimodal | Text + image input |
| Streaming | Supported |
| Function Calling | Supported |
Step 1: Get Your API Key
- Visit the xAI Console at console.x.ai
- Sign up or log in with your account
- Navigate to the "API Keys" section
- Click "Create API Key"
- Copy the key immediately (it will not be shown again)
Your API key will look like:
xai-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Store it securely:
- Never commit it to version control
- Use environment variables
- Use a secrets manager for production
Set the key as an environment variable:
# macOS/Linux: Add to ~/.bashrc or ~/.zshrc
export XAI_API_KEY="xai-your-key-here"
# Windows PowerShell
$env:XAI_API_KEY = "xai-your-key-here"
# Or use a .env file (never commit this file)
echo 'XAI_API_KEY=xai-your-key-here' >> .env
Step 2: Make Your First Request
Using cURL
curl https://api.x.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-3",
"messages": [
{"role": "system", "content": "You are a helpful programming assistant."},
{"role": "user", "content": "Explain the difference between a stack and a queue."}
],
"temperature": 0.7,
"max_tokens": 1024
}'
Using Python
Install the OpenAI Python SDK (works with xAI since it is OpenAI-compatible):
pip install openai
from openai import OpenAI
import os
# Initialize the client with xAI's base URL
client = OpenAI(
api_key=os.environ.get("XAI_API_KEY"),
base_url="https://api.x.ai/v1",
)
# Make a chat completion request
response = client.chat.completions.create(
model="grok-3",
messages=[
{"role": "system", "content": "You are a helpful coding assistant."},
{"role": "user", "content": "Write a Python function to find the longest palindrome in a string."}
],
temperature=0,
max_tokens=2048,
)
print(response.choices[0].message.content)
Using JavaScript/TypeScript
npm install openai
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.XAI_API_KEY,
baseURL: 'https://api.x.ai/v1',
});
async function main() {
const response = await client.chat.completions.create({
model: 'grok-3',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Explain microservices vs monolithic architecture.' }
],
temperature: 0.7,
max_tokens: 2048,
});
console.log(response.choices[0].message.content);
}
main();
Step 3: Understanding the Available Models
xAI offers multiple Grok 3 variants:
| Model | Speed | Quality | Cost | Best For |
|---|---|---|---|---|
grok-3 |
Moderate | Highest | Highest | Complex reasoning, analysis |
grok-3-mini |
Fast | Good | Lower | General tasks, chatbots |
grok-3-fast |
Fastest | Good | Lowest | High-throughput applications |
# Using the fast model for quick responses
response = client.chat.completions.create(
model="grok-3-fast",
messages=[
{"role": "user", "content": "What is the time complexity of binary search?"}
],
)
Step 4: Streaming Responses
For a better user experience, use streaming to display tokens as they are generated:
# Python streaming example
stream = client.chat.completions.create(
model="grok-3",
messages=[
{"role": "user", "content": "Write a detailed guide to Docker networking."}
],
stream=True,
max_tokens=4096,
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="", flush=True)
print() # Newline at the end
// TypeScript streaming example
const stream = await client.chat.completions.create({
model: 'grok-3',
messages: [
{ role: 'user', content: 'Explain Kubernetes pods in detail.' }
],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || '';
process.stdout.write(content);
}
console.log();
Step 5: Function Calling (Tool Use)
Grok 3 supports function calling, allowing it to interact with external tools:
import json
tools = [
{
"type": "function",
"function": {
"name": "search_database",
"description": "Search the product database for items matching a query",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query"
},
"category": {
"type": "string",
"enum": ["electronics", "books", "clothing"],
"description": "Product category to filter by"
},
"max_results": {
"type": "integer",
"description": "Maximum number of results to return"
}
},
"required": ["query"]
}
}
}
]
response = client.chat.completions.create(
model="grok-3",
messages=[
{"role": "user", "content": "Find me the top 5 electronics under $50"}
],
tools=tools,
tool_choice="auto",
)
# Check if the model wants to call a function
message = response.choices[0].message
if message.tool_calls:
for tool_call in message.tool_calls:
function_name = tool_call.function.name
arguments = json.loads(tool_call.function.arguments)
print(f"Function: {function_name}")
print(f"Arguments: {arguments}")
# Execute the function and send results back
# result = search_database(**arguments)
# ... continue the conversation with the result
Step 6: Vision (Image Input)
Grok 3 can analyze images alongside text:
response = client.chat.completions.create(
model="grok-3",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's happening in this image? Describe it in detail."
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image.jpg"
}
}
]
}
],
max_tokens=1024,
)
print(response.choices[0].message.content)
Using a local image with base64 encoding:
import base64
def encode_image(image_path):
with open(image_path, "rb") as f:
return base64.standard_b64encode(f.read()).decode("utf-8")
base64_image = encode_image("screenshot.png")
response = client.chat.completions.create(
model="grok-3",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Describe what you see in this screenshot."},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}"
}
}
]
}
],
)
Step 7: Grok 3 API Pricing
| Model | Input (per 1M tokens) | Output (per 1M tokens) |
|---|---|---|
| grok-3 | $3.00 | $15.00 |
| grok-3-mini | $0.30 | $0.50 |
| grok-3-fast | $5.00 | $25.00 |
Estimating costs:
# Rough cost estimation
def estimate_cost(input_tokens, output_tokens, model="grok-3"):
pricing = {
"grok-3": {"input": 3.00, "output": 15.00},
"grok-3-mini": {"input": 0.30, "output": 0.50},
"grok-3-fast": {"input": 5.00, "output": 25.00},
}
rates = pricing[model]
input_cost = (input_tokens / 1_000_000) * rates["input"]
output_cost = (output_tokens / 1_000_000) * rates["output"]
return input_cost + output_cost
# Example: 1000 input tokens, 500 output tokens
cost = estimate_cost(1000, 500, "grok-3")
print(f"Estimated cost: ${cost:.6f}") # $0.010500
Error Handling Best Practices
from openai import OpenAI, APIError, RateLimitError, AuthenticationError
import time
client = OpenAI(
api_key=os.environ.get("XAI_API_KEY"),
base_url="https://api.x.ai/v1",
)
def call_grok(messages, max_retries=3):
for attempt in range(max_retries):
try:
response = client.chat.completions.create(
model="grok-3",
messages=messages,
max_tokens=2048,
)
return response.choices[0].message.content
except AuthenticationError:
print("Invalid API key. Check your XAI_API_KEY.")
raise
except RateLimitError:
wait_time = 2 ** attempt
print(f"Rate limited. Waiting {wait_time}s...")
time.sleep(wait_time)
except APIError as e:
print(f"API error: {e}")
if attempt == max_retries - 1:
raise
time.sleep(1)
raise Exception("Max retries exceeded")
Common Issues and Solutions
| Issue | Cause | Solution |
|---|---|---|
| 401 Unauthorized | Invalid or missing API key | Check XAI_API_KEY is set correctly |
| 429 Too Many Requests | Rate limit hit | Implement exponential backoff |
| 400 Bad Request | Malformed request body | Validate your JSON and parameters |
| 413 Payload Too Large | Input exceeds context limit | Reduce input tokens (max 131K) |
| Timeout | Long generation or network issue | Set timeout, use streaming |
| Empty response | max_tokens too low | Increase max_tokens parameter |
Wrapping Up
The Grok 3 API is a powerful and developer-friendly option for building AI applications in 2026. Its OpenAI-compatible format means you can switch from other providers with minimal code changes, and the range of models (grok-3, grok-3-mini, grok-3-fast) gives you flexibility to optimize for quality, speed, or cost.
For applications that go beyond text, such as projects requiring AI image generation, video creation, lip sync, or voice cloning, Hypereal AI provides unified API access to dozens of specialized AI models alongside LLMs, so you can build complete AI-powered workflows from a single platform.
Related Articles
Start Building Today
Get 35 free credits on signup. No credit card required. Generate your first image in under 5 minutes.
